agentleFS
gurinderugurinderu/craft/skills/nixos/SKILL.md

nixos

Skill1 starsChanged 41 days agoMITView on GitHub

NixOS and nix-darwin system configuration — module shape, typed options with lib.mkOption, composing modules, nixosConfigurations, secrets (agenix/sops-nix), and cross-platform via nix-darwin + Home Manager. Use when writing a system module or config. Triggers: nixosConfiguration, nixos module, home-manager, nix-darwin, agenix, sops-nix, systemd service.

2 other projects use a version of this.

Adopted by

Most recently taken up by

The file

We split this skill into the rules it gives an agent

4 rules. About 0 are common, 1 is distinctive, and 3 are very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈1

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 3

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Very specific:

    Every NixOS module is a function that returns an attrset with options and/or config:

    line 23
  2. Very specific:

    Never shadow argument names with let bindings (e.g.

    line 74
  3. Very specific:

    Use specialArgs to thread inputs (e.g.

    line 131
  4. Distinctive:

    Use an encrypted secret manager that decrypts at activation time:

    line 158

≈ “Common” is an estimate: at least 1 in 5 of the 408,460 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

NixOS Modules

NixOS configuration is a module system: every .nix file that contributes to a system is a module. Modules declare options and set values; the evaluator merges them, type-checks every option, and produces a single system configuration.

When to Use

  • Writing or debugging a NixOS module (service, option, overlay, system config)
  • Composing nixosConfigurations.<host> in a flake
  • Managing secrets in a NixOS system (agenix, sops-nix)
  • Cross-platform configs: NixOS on Linux, nix-darwin on macOS, Home Manager on both
  • Wiring a systemd service or system-level unit

The module shape

Every NixOS module is a function that returns an attrset with options and/or config:

{ config, lib, pkgs, ... }:

{
  options.services.myApp = {
    enable = lib.mkEnableOption "my application";

    port = lib.mkOption {
      type    = lib.types.port;
      default = 8080;
      description = "TCP port the application listens on.";
    };

    dataDir = lib.mkOption {
      type    = lib.types.path;
      default = "/var/lib/my-app";
      description = "Directory for persistent application data.";
    };
  };

  config = lib.mkIf config.services.myApp.enable {
    systemd.services.my-app = {
      description = "My Application";
      wantedBy    = [ "multi-user.target" ];
      after       = [ "network.target" ];

      serviceConfig = {
        ExecStart = "${pkgs.my-app}/bin/my-app --port ${toString config.services.myApp.port}";
        User       = "my-app";
        StateDirectory = "my-app";
      };
    };

    users.users.my-app = {
      isSystemUser = true;
      group        = "my-app";
    };
    users.groups.my-app = {};
  };
}

Key points:

  • The argument list { config, lib, pkgs, ... } uses ... to accept additional inputs (e.g. inputs, modulesPath) without breaking when they are absent.
  • config (the argument) is the merged system configuration; config (the return key) is this module's contribution.
  • lib.mkIf gates the entire config block on the option being enabled — avoids unconditional side-effects.
  • Never shadow argument names with let bindings (e.g. let config = ... hides the system config and breaks every config.* reference inside the let body).

Declaring typed options with lib.mkOption

lib.mkOption {
  type        = lib.types.str;          # the type — enforced at eval time
  default     = "hello";                # optional; absence makes the option mandatory
  example     = "world";                # shown in nixos-option output
  description = "Human-readable docs."; # shown in the manual
}

Common types

TypeNotes
lib.types.booltrue / false
lib.types.intany integer
lib.types.portinteger 0–65535
lib.types.strany string
lib.types.pathfilesystem path
lib.types.packagea derivation
lib.types.listOf lib.types.strlist of strings
lib.types.attrsOf lib.types.intattribute set of ints
lib.types.nullOr lib.types.strstring or null
lib.types.enum [ "a" "b" ]one of a fixed set
lib.types.submodule { options = …; }nested structured option

lib.mkEnableOption "description" is shorthand for a bool option defaulting to false — prefer it over a raw lib.mkOption for toggle-style options.

Composing modules

Modules are composed by listing them in nixosSystem:

nixosConfigurations.myhost = nixpkgs.lib.nixosSystem {
  system = "x86_64-linux";
  modules = [
    ./hardware-configuration.nix   # generated by nixos-generate-config
    ./modules/my-app.nix           # custom module
    ./users/alice.nix              # per-user module
    {                              # inline anonymous module
      networking.hostName = "myhost";
      time.timeZone       = "Europe/Berlin";
    }
  ];
};

The evaluator merges all modules: lists are concatenated, booleans are OR'd (with lib.mkForce available to override), and type errors surface at nix build time.

Passing extra arguments to modules

Use specialArgs to thread inputs (e.g. flake inputs) into every module:

nixosConfigurations.myhost = nixpkgs.lib.nixosSystem {
  system = "x86_64-linux";
  specialArgs = { inherit inputs; };   # makes `inputs` available as a module arg
  modules = [ ./configuration.nix ];
};

Inside ./configuration.nix:

{ config, lib, pkgs, inputs, ... }:   # `inputs` now in scope
{
  # use inputs.agenix.packages.${pkgs.system}.agenix, etc.
}

Secrets must never land in the Nix store

The Nix store at /nix/store is world-readable. Any string you put into a derivation — including a systemd ExecStart argument or an environment.etc file — becomes readable by every user on the machine.

Rule: never put a secret string into a module option or derivation.

Use an encrypted secret manager that decrypts at activation time:

agenix

agenix encrypts secrets with age keys; the private key lives on the host (typically an SSH host key). Secrets are decrypted into /run/agenix/ (a tmpfs) at activation.

# flake.nix — add the input
inputs.agenix.url = "github:ryantm/agenix";
inputs.agenix.inputs.nixpkgs.follows = "nixpkgs";

# nixosConfigurations.myhost
modules = [
  inputs.agenix.nixosModules.default
  {
    age.secrets.db-password = {
      file  = ./secrets/db-password.age;   # encrypted file in the repo
      owner = "my-app";                    # who may read the decrypted file
    };

    systemd.services.my-app.serviceConfig = {
      # Point at the decrypted path — never embed the value
      EnvironmentFile = config.age.secrets.db-password.path;
    };
  }
];

Encrypt secrets with the agenix CLI and the host's public key (or any age recipient). The encrypted .age file is safe to commit; the plaintext never touches the store.

sops-nix

sops-nix uses Mozilla SOPS with age or PGP keys. Secrets are decrypted at activation into /run/secrets/.

inputs.sops-nix.url = "github:Mic92/sops-nix";
inputs.sops-nix.inputs.nixpkgs.follows = "nixpkgs";

modules = [
  inputs.sops-nix.nixosModules.sops
  {
    sops.defaultSopsFile = ./secrets/secrets.yaml;  # SOPS-encrypted file
    sops.age.sshKeyPaths = [ "/etc/ssh/ssh_host_ed25519_key" ];

    sops.secrets.db-password = {
      owner = "my-app";
    };

    systemd.services.my-app.serviceConfig = {
      EnvironmentFile = config.sops.secrets.db-password.path;
    };
  }
];

Both tools follow the same pattern: the secret value is never a Nix string; the module receives a path to the decrypted file on tmpfs and passes that path to the service.

Cross-platform: nix-darwin and Home Manager

NixOS modules only run on Linux. For macOS use nix-darwin, which mirrors the NixOS module system for macOS system configuration.

# flake.nix
inputs.nix-darwin.url = "github:LnL7/nix-darwin";
inputs.nix-darwin.inputs.nixpkgs.follows = "nixpkgs";

darwinConfigurations.macbook = inputs.nix-darwin.lib.darwinSystem {
  system = "aarch64-darwin";
  modules = [
    ./modules/common.nix   # shared options work on both platforms
    ./modules/darwin.nix   # macOS-specific
  ];
};

Inside a shared module, guard platform-specific options with pkgs.stdenv.isLinux / pkgs.stdenv.isDarwin:

{ config, lib, pkgs, ... }:

{
  config = lib.mkMerge [
    {
      # common to both platforms
      environment.systemPackages = [ pkgs.ripgrep pkgs.fd ];
    }
    (lib.mkIf pkgs.stdenv.isLinux {
      # Linux / NixOS only
      services.openssh.enable = true;
    })
    (lib.mkIf pkgs.stdenv.isDarwin {
      # macOS / nix-darwin only
      system.defaults.dock.autohide = true;
    })
  ];
}

Home Manager handles per-user dotfiles and programs on both platforms — see home-manager.md.

Boundaries

  • flake.nix inputs, nixosConfigurations wiring in outputs, flake.lock pinning → nix-flakes. That skill owns the flake schema; this skill owns what goes inside nixosSystem { modules = […]; }.
  • Derivations, overlays, callPackage, mkDerivation, overrides → nix-packaging.
  • Dev shells, direnv, per-project toolchains → nix-dev-env.
  • The craft:review workflow's Nix profile consumes the modules rules from this skill when reviewing NixOS configuration changes: option types, lib.mkIf discipline, and the secrets-not-in-store rule are its primary checklist items for module diffs.

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.