macOS configuration with nix-darwin
Declare all macOS system settings, packages, and dotfiles in the flake instead of running imperative installers or mutating local preference files by hand.
Repo facts (from AGENTS.md or CLAUDE.md): the flake directory path, the shared nix-darwin module path, the shared Home Manager module path, the per-host override directory, and how local secrets are stored.
Place each setting in the right layer
- Put macOS
system.defaults, PAM settings,/etcfiles, and GUI Homebrew casks in thenix-darwinmodule. - Put CLI packages, dotfiles, shell aliases,
home.sessionVariables, and userlaunchd.agentsin the Home Manager module. - Put user
PATHadditions inprograms.zsh.initContentorzprofileinstead of relying only onhome.sessionPathin~/.zshenv. macOS/etc/zprofilerunspath_helperafter~/.zshenv, andlogin -pfpreserves__HM_SESS_VARS_SOURCED. - Put machine-specific hardware mappings, computer names, and host-only packages in the host module.
- Prefer Nix packages for CLI tools and language toolchains. Prefer Homebrew casks for GUI
.appbundles that auto-update or need macOS code-signing entitlements.
Evaluate and build
- Stage any new file before evaluating the flake. Nix flakes ignore untracked Git files and fail with a missing-file error:
bash
git -C <flake-dir> add --intent-to-add <new-file> - Check attribute names and option paths with
nix evalbefore building the full system closure:bashnix eval <flake-dir>#darwinConfigurations.<host>.pkgs.<pkg>.version - Build the host system closure without root privileges to catch module type errors and missing options early:
bash
nix build --no-link --print-out-paths <flake-dir>#darwinConfigurations.<host>.system - If a macOS preference key has no typed option under
system.defaults.<domain>, place it undersystem.defaults.CustomUserPreferences.<domain>.
Activate from an agent shell
An agent background shell has no controlling terminal app, so pam_tid.so skips Touch ID and sudo darwin-rebuild switch hangs waiting for a password on standard input.
- For Home Manager changes (CLI packages, dotfiles, environment variables, and user
launchdagents), build and run the user activation package directly withoutsudo:
Use full paths such asbashHOST="$(scutil --get LocalHostName)" SYS_PATH="$(nix build --no-link --print-out-paths "<flake-dir>#darwinConfigurations.${HOST}.system")" ln -sfn "$(readlink -f "$SYS_PATH/etc/profiles/per-user/$USER")" "$HOME/.local/state/nix/profiles/profile" HM_ACTIVATE="$(nix build --no-link --print-out-paths "<flake-dir>#darwinConfigurations.${HOST}.config.home-manager.users.${USER}.home.activationPackage")" "$HM_ACTIVATE/activate"/usr/bin/open,/usr/bin/pgrep,/usr/bin/defaults, and/usr/bin/securityinsidehome.activationscripts, which run with a minimalPATH. - For
system.defaultschanges that belong to the current user (such ascom.apple.dock,com.apple.finder, orcom.apple.screencapture), write the same plist key withdefaults writeand restart the affected service (killall Dock,killall Finder, orkillall SystemUIServer) so the change takes effect immediately. - For Homebrew casks, add the cask to
homebrew.casksand runbrew install --cask <cask>so the app installs right away. Pass--adoptif the.appbundle already exists in/Applications. - For root-only changes (
/etc, PAM, system daemons, or.pkgcasks that prompt forsudo), ask the operator to runsudo darwin-rebuild switchin their terminal afternix buildsucceeds.
Secrets and Homebrew drift
- Never write tokens or credentials into
.nixfiles. Everything copied into/nix/storeis world-readable on the machine and tracked in Git. - Store secrets in the macOS Keychain with
security add-generic-password -U -a "$USER" -s "<service>" -w "<secret>". Read them at Home Manager activation or shell startup withsecurity find-generic-password -a "$USER" -s "<service>" -w. - Set
homebrew.onActivation.cleanup = "zap"when you want activation to remove undeclared Homebrew formulae and casks. Checkbrew list --formulaandbrew list --caskbefore enabling it for the first time. - If an external installer such as Lix or the Determinate Nix Installer manages the Nix daemon (
nix.enable = false), put custom daemon settings inenvironment.etc."nix/nix.custom.conf"instead ofnix.settings.
Verify
nix build --no-link <flake-dir>#darwinConfigurations.<host>.systemexits 0 with no deprecation warnings.- User activation (
$HM_ACTIVATE/activate) exits 0. - Installed binaries resolve on
PATH, or changed preferences appear indefaults read. git diffcontains no plaintext tokens or machine-private credentials.


