nix-conditionals

Apply idiomatic Nix conditionals using lib.mkIf, lib.mkMerge, lib.optionals, and lib.optionalString.

1|Updated Jun 2, 2025
One-click install
npx skills add https://github.com/aytordev/system --skill nix-conditionals-aytordev
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: nix-conditionals
Source: https://github.com/aytordev/system/tree/main/modules/common/ai-tools/skills/nix/conditionals
Command: npx skills add https://github.com/aytordev/system --skill nix-conditionals-aytordev

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

Nix configuration often suffers from verbose conditionals and brittle merges. This skill provides idiomatic patterns using lib.mkIf, lib.mkMerge, lib.optionals, and lib.optionalString to write robust, composable conditional blocks and safe list handling.

Core Features & Use Cases

  • mkIf - guard configuration blocks without breaking module merging
  • mkMerge - combine conditional blocks into a single definition
  • optionals - conditionally append list items safely
  • optionalString - render strings only when needed Real-world use cases include toggling features in NixOS configurations based on system attributes or user options.

Quick Start

Use these patterns in a Nix configuration to guard features and compose conditional lists with minimal boilerplate.

Frequently Asked Questions about nix-conditionals

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

FAQPage Schema
How do I safely toggle NixOS configuration features without breaking module merges?

Use lib.mkIf to guard configuration blocks, ensuring your conditional definitions remain composable and preventing evaluation conflicts during NixOS module merges.

What is the best way to conditionally append items to a list in Nixpkgs?

The best way to conditionally append list items in Nixpkgs is using the lib.optionals function. It safely evaluates a condition and returns the requested items or an empty list, preventing invalid type errors in your configuration.

How does mkMerge work when combining multiple conditional blocks in Nix?

lib.mkMerge combines multiple conditional blocks into a single Nix definition. It allows you to stack several mkIf statements or attribute sets together, resolving them safely into one unified module output without evaluation conflicts.

When do I need to use optionalString instead of optionals in Nix configurations?

Use lib.optionalString in Nix configurations when you need to render a specific string only if a condition is met, returning an empty string otherwise. Use lib.optionals exclusively for safely conditionally appending list items.

Can I use these Nix conditional patterns to manage configurations based on user options?

Yes, you can use these Nix conditional patterns to manage configurations based on user options. They are designed for developers and system engineers to dynamically toggle NixOS features according to varying system attributes and user-defined settings.