macOS (Darwin) Deployment
Complete guide to deploying this Nix configuration on macOS.
Table of Contents
Section titled “Table of Contents”- Overview
- Prerequisites
- Initial Deployment
- System Updates
- Managing Generations
- Rollback and Recovery
- Multi-Machine Setup
- Troubleshooting
Overview
Section titled “Overview”This guide covers deploying and managing your Nix configuration on macOS using nix-darwin.
Status: ✅ Fully implemented and tested
Prerequisites
Section titled “Prerequisites”- macOS 11 (Big Sur) or later
- Apple Silicon (M1/M2/M3) or Intel
- Administrator access
- 10 GB free disk space
See Installation Guide for initial setup.
Initial Deployment
Section titled “Initial Deployment”First-Time Setup
Section titled “First-Time Setup”# 1. Install Nixcurl -fsSL https://install.determinate.systems/nix | sh -s -- install --determinate
# 2. Install Homebrew/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
# 3. Clone configgit clone https://github.com/yourusername/Config.git ~/Configcd ~/Config
# 4. Customize for your machinecp hosts/wikigen-mac.nix hosts/your-mac.nixnano hosts/your-mac.nix # Update username
# 5. First activation (requires sudo)sudo nix run nix-darwin -- switch --flake .#your-mac
Verify Deployment
Section titled “Verify Deployment”# Check system generationdarwin-rebuild --list-generations
# View current systemls -l /run/current-system
# Test commandswhich darwin-rebuildnix --version
System Updates
Section titled “System Updates”Daily Update Workflow
Section titled “Daily Update Workflow”cd ~/Config
# 1. Update flake inputsnix flake update
# 2. Test builddarwin-rebuild build --flake .#your-mac
# 3. Check changesnix store diff-closures /run/current-system ./result
# 4. Apply if gooddarwin-rebuild switch --flake .#your-mac
Selective Updates
Section titled “Selective Updates”# Update specific inputnix flake lock --update-input nixpkgs
# Update darwin onlynix flake lock --update-input nix-darwin
# Update home-manager onlynix flake lock --update-input home-manager
Update and Rebuild
Section titled “Update and Rebuild”# One command: update + rebuildnix flake update && darwin-rebuild switch --flake .#your-mac
Managing Generations
Section titled “Managing Generations”List Generations
Section titled “List Generations”# Show all generationsdarwin-rebuild --list-generations
# Example output:# 1 2024-01-15 10:30:45# 2 2024-01-20 14:22:10# 3 2024-01-25 09:15:33 (current)
Switch Generations
Section titled “Switch Generations”# Switch to specific generationdarwin-rebuild switch --switch-generation 2
# Rollback to previousdarwin-rebuild switch --rollback
Delete Old Generations
Section titled “Delete Old Generations”# Delete generations older than 30 daysnix-collect-garbage --delete-older-than 30d
# Delete all old generationsnix-collect-garbage -d
# Keep last N generationsdarwin-rebuild --list-generations | head -n -3 | awk '{print $1}' | xargs darwin-rebuild delete-generations
Rollback and Recovery
Section titled “Rollback and Recovery”Quick Rollback
Section titled “Quick Rollback”# Rollback to previous generationdarwin-rebuild switch --rollback
Manual Recovery
Section titled “Manual Recovery”# If system is broken, boot into recovery
# 1. List available generationsls -l /nix/var/nix/profiles/system-*-link
# 2. Activate specific generation/nix/var/nix/profiles/system-2-link/activate
# 3. Or rollback/nix/var/nix/profiles/system/bin/darwin-rebuild switch --rollback
Backup Configuration
Section titled “Backup Configuration”# Backup current generationcp -r /run/current-system ~/system-backup-$(date +%Y%m%d)
# Export configurationnix flake archive --to file://~/config-backup.tar.gz
Multi-Machine Setup
Section titled “Multi-Machine Setup”Add New macOS Machine
Section titled “Add New macOS Machine”-
Create host config
Terminal window cp hosts/wikigen-mac.nix hosts/new-mac.nix -
Update configuration
hosts/new-mac.nix {system.primaryUser = "newuser";users.users.newuser = {name = "newuser";home = "/Users/newuser";};} -
Add to flake.nix
darwinConfigurations.new-mac = darwin.lib.darwinSystem {system = "aarch64-darwin"; # or x86_64-darwinmodules = [./nix/modules/common.nix./nix/modules/darwin-base.nix./hosts/new-mac.nix# ... home-manager config];}; -
Deploy
Terminal window # On new machinegit clone https://github.com/yourusername/Config.git ~/Configcd ~/Configsudo nix run nix-darwin -- switch --flake .#new-mac
Shared Configuration
Section titled “Shared Configuration”# Share profiles across machines# In both host configs:imports = [ ../nix/profiles/developer.nix ../nix/profiles/cloud-cli.nix];
# Machine-specific differencesenvironment.systemPackages = lib.optionals (config.networking.hostName == "work-mac") [ pkgs.work-specific-tool ];
Troubleshooting
Section titled “Troubleshooting”Build Fails
Section titled “Build Fails”# Verbose outputdarwin-rebuild switch --flake .#your-mac --show-trace
# Check for errorsnix flake check
# Test specific packagenix build .#package-name
Activation Fails
Section titled “Activation Fails”# Check activation scriptcat /run/current-system/activate
# Run manually/run/current-system/activate
# Check logstail -f /var/log/system.log
Rollback Broken System
Section titled “Rollback Broken System”# If current generation is brokendarwin-rebuild switch --rollback
# If darwin-rebuild is broken/nix/var/nix/profiles/system/bin/darwin-rebuild switch --rollback
# If everything is broken, boot to recovery and:# 1. Activate previous generation manually/nix/var/nix/profiles/system-1-link/activate
Permission Issues
Section titled “Permission Issues”# Fix Nix store permissionssudo chown -R root:nixbld /nix
# Fix user permissionssudo chown -R $USER ~/.nix-profile
# Fix flake.locksudo chown $USER:staff flake.lock
Advanced Topics
Section titled “Advanced Topics”Remote Deployment
Section titled “Remote Deployment”# Build on local machine, deploy to remotedarwin-rebuild switch --flake .#remote-mac \ --target-host user@remote-mac.local \ --use-remote-sudo
Binary Cache Setup
Section titled “Binary Cache Setup”# Use custom binary cachedarwin-rebuild switch --flake .#your-mac \ --option substituters "https://cache.nixos.org https://your-cache.com" \ --option trusted-public-keys "your-cache-key"
Declarative User Management
Section titled “Declarative User Management”# Full user management in Nixusers.users.developer = { uid = 501; home = "/Users/developer"; shell = pkgs.zsh; description = "Developer Account";};
Daily Commands
Section titled “Daily Commands”# Update and rebuildnix flake update && darwin-rebuild switch --flake .#your-mac
# Test before applyingdarwin-rebuild build --flake .#your-macnix store diff-closures /run/current-system ./result
# Garbage collectnix-collect-garbage --delete-older-than 30d
# List generationsdarwin-rebuild --list-generations
# Rollbackdarwin-rebuild switch --rollback
Next Steps
Section titled “Next Steps”- NixOS Deployment - Linux deployment (planned)
- Cloud Deployment - EC2/GCE deployment (planned)
- Multi-Machine Example - Add another host
- Troubleshooting - Fix issues
Related Documentation
Section titled “Related Documentation”- Installation Guide - Initial setup
- First Steps - Post-install guide
- Structure Guide - Config architecture
External References
Section titled “External References”- nix-darwin Manual - Official docs
- Nix Manual - Nix documentation
- Determinate Nix - Installer docs
Ready to deploy? Start with Initial Deployment!