Skip to content

CLI Commands Reference

Quick reference for common Nix and nix-darwin commands.



Terminal window
# Apply configuration changes
darwin-rebuild switch --flake .#hostname
# Build without applying
darwin-rebuild build --flake .#hostname
# Test build (dry run)
darwin-rebuild build --flake .#hostname --dry-run
# Build with trace (for errors)
darwin-rebuild switch --flake .#hostname --show-trace
# List generations
darwin-rebuild --list-generations
# Switch to generation
darwin-rebuild switch --switch-generation 5
# Rollback to previous
darwin-rebuild switch --rollback
# Delete specific generations
darwin-rebuild delete-generations 1 2 3
# Delete old generations (30+ days)
darwin-rebuild delete-generations +30
Terminal window
# Show current system
ls -l /run/current-system
# System configuration
darwin-option -r
# Specific option
darwin-option programs.zsh.enable
# Show all options
darwin-option --list

Terminal window
# Search nixpkgs
nix search nixpkgs python
# Search with JSON output
nix search nixpkgs --json python | jq
# Browse online
# https://search.nixos.org/packages
Terminal window
# Add to configuration file, then:
darwin-rebuild switch --flake .#hostname
# Or install to user profile
nix profile install nixpkgs#package-name
# List installed (in profile)
nix profile list
# Remove from profile
nix profile remove package-name
Terminal window
# Show package metadata
nix eval nixpkgs#python3.meta.description
# Show version
nix eval nixpkgs#python3.version
# Show package info
nix-env -qa --description python3

Terminal window
# Update all inputs
nix flake update
# Update specific input
nix flake lock --update-input nixpkgs
# Show flake info
nix flake show
# Show flake metadata
nix flake metadata
# Check flake
nix flake check
# Archive flake
nix flake archive --to file://backup.tar.gz
Terminal window
# Build default package
nix build
# Build specific output
nix build .#ai-clis
# Build system
nix build .#darwinConfigurations.hostname.system
# Build and run
nix run .#package-name
Terminal window
# Enter dev shell
nix develop
# Run command in dev shell
nix develop --command bash

Terminal window
# List all store paths
nix-store --query --requisites /run/current-system
# Show references (runtime deps)
nix-store --query --references /nix/store/path
# Show referrers (what depends on this)
nix-store --query --referrers /nix/store/path
# Dependency tree
nix-store --query --tree /run/current-system
# Closure size
nix path-info -S /run/current-system
# All paths sorted by size
nix path-info -rS /run/current-system | sort -nk2
Terminal window
# Garbage collection
nix-collect-garbage
# Delete old generations (30+ days)
nix-collect-garbage --delete-older-than 30d
# Delete everything not in use
nix-collect-garbage -d
# Optimize store (deduplicate)
nix-store --optimise
# Verify store
nix-store --verify --check-contents
# Repair store
nix-store --verify --check-contents --repair
Terminal window
# Diff two store paths
nix store diff-closures /run/current-system ./result
# Compare generations
nix store diff-closures \
/nix/var/nix/profiles/system-5-link \
/nix/var/nix/profiles/system-6-link

Terminal window
# Build package
nix build .#package-name
# Build with logs
nix build .#package-name --print-build-logs
# Build with trace
nix build .#package-name --show-trace
# Keep failed builds
nix build .#package-name --keep-failed
# Dry run (what would build)
nix build .#package-name --dry-run
# Force rebuild
nix build .#package-name --rebuild
Terminal window
# Evaluate expression
nix eval .#package-name.version
# Evaluate with JSON
nix eval --json .#package-name.meta
# Parse Nix file
nix-instantiate --parse file.nix
# Evaluate Nix file
nix-instantiate --eval file.nix
# Show derivation
nix derivation show .#package-name
Terminal window
# Enter shell with package
nix shell nixpkgs#python3
# Run command with package
nix run nixpkgs#hello
# Develop with package
nix develop

Terminal window
# Verbose output
darwin-rebuild switch --flake .#hostname --verbose
# Show trace
darwin-rebuild switch --flake .#hostname --show-trace
# Debug evaluation
nix eval --show-trace .#darwinConfigurations.hostname
# Check what's wrong
nix flake check --show-trace
Terminal window
# Repair store
nix-store --verify --check-contents --repair
# Re-download from cache
nix-store --verify --check-contents
# Delete and rebuild
rm -rf result
nix build .#package-name
Terminal window
# Build without cache (force local)
nix build .#package-name --option substitute false
# Use specific cache
nix build .#package-name \
--option substituters "https://cache.nixos.org"
# Check if path in cache
nix path-info --store https://cache.nixos.org /nix/store/path

Terminal window
# Update and rebuild
nix flake update && darwin-rebuild switch --flake .#hostname
# Test before applying
darwin-rebuild build --flake .#hostname
nix store diff-closures /run/current-system ./result
# Rollback if broken
darwin-rebuild switch --rollback
# Clean up old stuff
nix-collect-garbage --delete-older-than 30d
# Search for package
nix search nixpkgs package-name
# Check configuration
nix flake check

Update system:

Terminal window
cd ~/Config
nix flake update
darwin-rebuild build --flake .#hostname
nix store diff-closures /run/current-system ./result
darwin-rebuild switch --flake .#hostname

Add package:

# Edit hosts/hostname.nix
environment.systemPackages = [ pkgs.new-package ];
Terminal window
darwin-rebuild switch --flake .#hostname

Test package:

Terminal window
nix build .#package-name
./result/bin/package-name

Recover from error:

Terminal window
darwin-rebuild switch --rollback
# Or
darwin-rebuild switch --switch-generation 5

Terminal window
# Nix configuration
NIX_PATH="nixpkgs=/path/to/nixpkgs"
# Build cores
NIX_BUILD_CORES=8
# Store directory (usually default)
NIX_STORE_DIR="/nix/store"
# Remote builders
NIX_REMOTE_SYSTEMS="user@builder"
# Show all options
nix show-config



Bookmark this page for quick command reference!