nix-conditionals

Replace if-then-else templates with lib.mkIf, lib.optionals, and lib.mkMerge in Nix configurations.

340|18|Updated May 11, 2023
One-click install
npx skills add https://github.com/khaneliman/khanelinix --skill nix-conditionals
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: nix-conditionals
Source: https://github.com/khaneliman/khanelinix/tree/main/modules/common/ai-tools/skills/conditionals
Command: npx skills add https://github.com/khaneliman/khanelinix --skill nix-conditionals

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

This skill helps replace verbose if-then-else blocks in Nix configurations with reusable lib.mkIf, lib.optionals, lib.optionalString, and lib.mkMerge patterns, reducing duplication and improving maintainability.

Core Features & Use Cases

  • Use lib.mkIf to conditionally enable config blocks
  • Use lib.optionals to include conditional list items
  • Use lib.optionalString to conditionally inject strings
  • Use lib.mkMerge to combine conditional blocks
  • Apply in large Nix configurations across environments to tailor settings for different machines or users.

Quick Start

Add a small Nix snippet using lib.mkIf and lib.optionals in your configuration and run your regular rebuild to verify conditional blocks.

Frequently Asked Questions about nix-conditionals

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I replace verbose if-then-else blocks in NixOS configurations?

Replace verbose if-then-else blocks in NixOS configurations by using lib.mkIf to conditionally enable config blocks and lib.optionals to include conditional list items, reducing duplication and improving maintainability.

What is the best way to combine conditional configuration blocks across different Nix environments?

The best way to combine conditional configuration blocks across different Nix environments is using lib.mkMerge to merge multiple conditionally enabled blocks together into a single robust NixOS setup.

How does lib.mkIf work for tailoring Nix packages and services?

lib.mkIf works for tailoring Nix packages and services by evaluating a boolean condition to conditionally enable entire configuration blocks, allowing you to customize settings across different machines, users, and hardware capabilities.

How do I conditionally inject strings into Nix expressions without complex logic?

Conditionally inject strings into Nix expressions by using lib.optionalString, which returns the string if the condition is true and an empty string otherwise, eliminating the need for complex if-then-else template logic.

Do I need a specific Nix workflow to use lib conditional patterns?

Yes, you need a Nix-enabled workflow and access to standard Nix lib functions to implement these conditional patterns and run your regular NixOS rebuild to verify the tailored configuration blocks.

When should I use lib.optionals instead of standard if-then-else in Nix?

Use lib.optionals instead of standard if-then-else in Nix when you need to conditionally include items in a list based on a boolean condition, ensuring reusable and maintainable configuration patterns across your Nixpkgs setup.