nix-conditionals

Implements conditional Nix configuration using mkIf, optionals, optionalString, and mkMerge.

1|Updated Aug 21, 2023
One-click install
npx skills add https://github.com/jmuchovej/homelab --skill nix-conditionals-jmuchovej
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: nix-conditionals
Source: https://github.com/jmuchovej/homelab/tree/main/src/modules/ai-tools/skills/nix-conditionals
Command: npx skills add https://github.com/jmuchovej/homelab --skill nix-conditionals-jmuchovej

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve? Writing conditional logic in Nix configurations often leads to verbose if-then-else expressions that are hard to read and merge. This Skill provides the idiomatic lib function patterns for conditional configuration blocks, list items, and strings. ## Core Features & Use Cases - Conditional Config Blocks: Use lib.mkIf to enable entire configuration sections only when a flag is set. - Conditional List Items: Use lib.optionals to append packages or entries to lists based on conditions like platform or feature flags. - Conditional Strings: Use lib.optionalString to append shell snippets or text conditionally. - Combining Conditions: Use lib.mkMerge to merge multiple conditional blocks into one config. - Use Case: When writing a Home Manager module, conditionally install ripgrep and fd only when a custom enable-tools option is true, and add Linux-only packages using pkgs.stdenv.isLinux. ## Quick Start Show me how to conditionally enable git and vim in my Nix configuration using mkIf and mkMerge.

Frequently Asked Questions about nix-conditionals

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

FAQPage Schema
How do I write conditional configuration in Nix?

Use lib.mkIf to wrap a configuration block with a condition, such as config = lib.mkIf cfg.enable { programs.git.enable = true; }. The block is only applied when the condition is true, and it merges cleanly with other definitions.

How to conditionally add packages to a list in Nix?

Use lib.optionals with list concatenation: home.packages = [ pkgs.coreutils ] ++ lib.optionals cfg.enable-tools [ pkgs.ripgrep pkgs.fd ]. The items are included only when the condition evaluates to true.

When should I use mkIf vs if-then-else in Nix?

Use lib.mkIf for conditional configuration blocks because it participates in the module system's merge semantics. Reserve if-then-else for simple value selection, such as theme = if isDark then "dark" else "light".

How do I combine multiple conditional blocks in Nix config?

Use lib.mkMerge with a list of blocks, mixing unconditional attrsets with lib.mkIf-wrapped conditional ones. Each mkIf block applies only when its condition holds, while unconditional blocks always apply.

Can I conditionally append a string in Nix configuration?

Yes, use lib.optionalString with string concatenation. For example, programs.bash.initExtra = ''...'' + lib.optionalString cfg.enable-aliases ''alias ll='ls -la''' appends the alias block only when the condition is true.