Skip to content

nix-darwin

Getting started

This guide walks you through setting up macOS with nix-darwin to configure native settings and preinstall essential apps and configurations.

References


Setup

  1. Make directory

    Terminal window
    mkdir ~/.nix-config && cd ~/.nix-config
  2. Add nix files

    flake.nix
    {
    hostname = builtins.getEnv "HOSTNAME";
    user = builtins.getEnv "USER";
    home = builtins.getEnv "HOME";
    description = user + " darwin system";
    inputs = {
    # Use `github:NixOS/nixpkgs/nixpkgs-24.11-darwin` to use Nixpkgs 24.11.
    nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
    # Use `github:LnL7/nix-darwin/nix-darwin-24.11` to use Nixpkgs 24.11.
    nix-darwin.url = "github:LnL7/nix-darwin/master";
    nix-darwin.inputs.nixpkgs.follows = "nixpkgs";
    };
    outputs = inputs@{ self, nix-darwin, nixpkgs }: {
    darwinConfigurations."${hostname}" = nix-darwin.lib.darwinSystem {
    modules = [ ./configuration.nix ];
    specialArgs = { hostname, user, home };
    };
    };
    }
    configuration.nix
    { config, pkgs, ... }:
    let
    home = config.specialArgs.home;
    in
    {
    imports = [
    ./system.nix
    ];
    # Nix configuration
    system.stateVersion = 6;
    nix.enable = false;
    nix.settings.experimental-features = [ "nix-command" "flakes" ];
    nixpkgs.hostPlatform = "x86_64-darwin";
    # System packages
    # environment.systemPackages = with pkgs; [
    # ffmpeg
    # jq
    # bat
    # gh
    # pipx
    # ];
    # # zsh
    # programs.zsh = {
    # enable = true;
    # enableCompletion = true;
    # enableFastSyntaxHighlighting = true;
    # shellInit = ''
    # source ${home}/.nix-config/.dotfiles/.zshrc
    # '';
    # };
    }
    system.nix
    { config, ... }:
    let
    home = config.specialArgs.home;
    in
    {
    # Global
    system.defaults.NSGlobalDomain.AppleInterfaceStyle = "Dark";
    system.defaults.NSGlobalDomain.AppleShowAllExtensions = true;
    system.defaults.trackpad.TrackpadRightClick = true;
    system.defaults.NSGlobalDomain."com.apple.trackpad.enableSecondaryClick" = true;
    system.defaults.NSGlobalDomain."com.apple.trackpad.trackpadCornerClickBehavior" = 1;
    # Finder
    system.defaults.finder.AppleShowAllFiles = true;
    system.defaults.finder.AppleShowAllExtensions = true;
    system.defaults.finder._FXSortFoldersFirst = true;
    system.defaults.finder._FXSortFoldersFirstOnDesktop = true;
    system.defaults.finder.FXDefaultSearchScope = "SCcf";
    system.defaults.finder.FXEnableExtensionChangeWarning = false;
    system.defaults.finder.FXPreferredViewStyle = "Nlsv";
    system.defaults.finder.QuitMenuItem = true;
    system.defaults.finder.ShowRemovableMediaOnDesktop = false;
    system.defaults.finder.ShowExternalHardDrivesOnDesktop = true;
    system.defaults.finder.ShowHardDrivesOnDesktop = false;
    system.defaults.finder.ShowMountedServersOnDesktop = false;
    system.defaults.finder.ShowPathbar = true;
    system.defaults.finder.ShowStatusBar = true;
    system.defaults.finder.NewWindowTarget = "Other";
    system.defaults.finder.NewWindowTargetPath = "file:///${home}/Downloads";
    # Dock
    system.defaults.dock.tilesize = 64;
    system.defaults.dock.largesize = 16;
    system.defaults.dock.orientation = "right";
    system.defaults.dock.mineffect = "genie";
    system.defaults.dock.minimize-to-application = true;
    system.defaults.dock.autohide = false;
    system.defaults.dock.launchanim = true;
    system.defaults.dock.show-process-indicators = true;
    system.defaults.dock.show-recents = false;
    system.defaults.dock.persistent-others = null;
    system.defaults.dock.persistent-apps = [
    "Applications/Google Chrome.app"
    "/System/Applications/Utilities/Terminal.app"
    "/Applications/Visual Studio Code.app"
    "/System/Applications/System Settings.app"
    ];
    # Desktop & Stage manager
    system.defaults.WindowManager.StandardHideDesktopIcons = false;
    system.defaults.WindowManager.HideDesktop = true;
    system.defaults.WindowManager.EnableStandardClickToShowDesktop = false;
    system.defaults.WindowManager.GloballyEnabled = false;
    system.defaults.spaces.spans-displays = false;
    # Miscellaneous
    security.pam.services.sudo_local.touchIdAuth = true;
    system.defaults.screencapture.location = "~/Documents/screenshots";
    system.defaults.controlcenter.Bluetooth = true;
    # macOS security updates
    system.defaults.CustomUserPreferences."com.apple.SoftwareUpdate".CriticalUpdateInstall = 1;
    # app store updates
    system.defaults.CustomUserPreferences."com.apple.commerce".AutoUpdate = true;
    system.activationScripts.postUserActivation.text = ''
    # Create dirs
    mkdir -p ~/Documents/screenshots ~/Documents/dev
    # Keep: Map bottom right corner to right-click
    defaults write com.apple.driver.AppleBluetoothMultitouch.trackpad TrackpadCornerSecondaryClick -int 2
    defaults write com.apple.driver.AppleBluetoothMultitouch.trackpad TrackpadRightClick -int 0
    # "disable" Writing of .DS_Store files on network or USB volumes
    defaults write com.apple.desktopservices DSDontWriteNetworkStores -bool true
    defaults write com.apple.desktopservices DSDontWriteUSBStores -bool true
    # Set list view as default for all folders
    defaults write com.apple.finder "SearchRecentsSavedViewStyle" -string "Nlsv"
    defaults write com.apple.finder "FXPreferredViewStyle" -string "Nlsv"
    # "disable" Sidebar: Recent Tags
    defaults write com.apple.finder ShowRecentTags -bool false
    # Restart Finder to apply changes
    killall Finder || true
    '';
    }
  3. Install nix

    Terminal window
    curl --proto '=https' --tlsv1.2 -sSf -L https://install.determinate.systems/nix | sh -s -- install
  4. Install nix-darwin

    Terminal window
    nix run nix-darwin/master#darwin-rebuild -- switch --flake .#ok-mac-pro
  5. Build

    Terminal window
    darwin-rebuild switch --flake .#ok-mac-pro

Uninstall

  1. Remove unused profiles

    Terminal window
    nix-collect-garbage --delete-old
  2. Uninstall nix-darwin

    Terminal window
    nix --extra-experimental-features "nix-command flakes" run nix-darwin#darwin-uninstaller
  3. Uninstall nix

    Terminal window
    /nix/nix-installer uninstall
  4. Remove nix artifacts


    1. Edit system shell configs

      Edit /etc/zshrc, /etc/bashrc and /etc/bash.bashrc to remove the lines sourcing nix-daemon.sh.

    2. Remove shell config backups

      If these files haven’t been altered since installing Nix you can simply put the backups back in place.

      Terminal window
      sudo mv /etc/zshrc.backup-before-nix /etc/zshrc
      sudo mv /etc/bashrc.backup-before-nix /etc/bashrc
      sudo mv /etc/bash.bashrc.backup-before-nix /etc/bash.bashrc
    3. Stop and remove the Nix daemon services

      Terminal window
      sudo launchctl unload /Library/LaunchDaemons/org.nixos.nix-daemon.plist
      sudo rm /Library/LaunchDaemons/org.nixos.nix-daemon.plist
      sudo launchctl unload /Library/LaunchDaemons/org.nixos.darwin-store.plist
      sudo rm /Library/LaunchDaemons/org.nixos.darwin-store.plist
    4. Remove the nixbld group and the _nixbuildN users

      Terminal window
      sudo dscl . -delete /Groups/nixbld
      for u in $(sudo dscl . -list /Users | grep _nixbld); do
      sudo dscl . -delete /Users/$u
      done
    5. Edit fstab

      Edit fstab using sudo vifs to remove the line mounting the Nix Store volume on /nix, which looks like UUID=<uuid> /nix apfs rw,noauto,nobrowse,suid,owners or LABEL=Nix\040Store /nix apfs rw,nobrowse. This will prevent automatic mounting of the Nix Store volume.

    6. Edit synthetic.conf

      Edit /etc/synthetic.conf to remove the nix line. If this is the only line in the file you can remove it entirely, sudo rm /etc/synthetic.conf. This will prevent the creation of the empty /nix directory to provide a mountpoint for the Nix Store volume.

    7. Remove the files Nix added to your system

      Terminal window
      sudo rm -rf /etc/nix ~/.nix-profile ~/.nix-defexpr ~/.nix-channels ~/.nix-profile ~/.nix-defexpr ~/.nix-channels ~/.cache/nix ~/.local/state/nix /System/Volumes/Data/private/var/root/.nix-defexpr /System/Volumes/Data/private/var/root/.cache/nix /private/var/root/.nix-defexpr /private/var/root/.cache/nix
    8. Remove the Nix Store volume

      Terminal window
      sudo diskutil apfs deleteVolume /nix
    9. Verify

      Look for a “Nix Store” volume in the output of the following command.

      Terminal window
      diskutil list

Manual configurations

Defaults that aren’t configurable with nix-darwin.

Notifications

  • Allow notifications when the screen is locked > false

Desktop & Dock

  • Default web browser

Lock Screen

  • When Switching User > Login window shows > Name and password

Trackpad

  • Secondary click > Click in Bottom Right Corner

Finder sidebar

Favorites

  • Recents
  • AirDrop
  • Applications
  • Desktop
  • Documents
  • Downloads
  • Movies
  • Music
  • Pictures
  • $USER

iCloud

  • iCloud Drive
  • Shared

Locations

  • Device
  • Hard disks
  • External disks
  • CDs, DVDs, and iOS Devices
  • Cloud Storage
  • Bonjour computers
  • Connected servers

Tags

  • Recent Tags

View Options

  • Always open in list view
  • Browse in list view
  • Group By: None
  • Sort By: Date Modified

Show Columns:

  • Date Modified
  • Date Created
  • Date Last Opened
  • Date Added
  • Size
  • Kind
  • Version
  • Comments
  • Tags

Misc

  • Use relative dates
  • Calculate all sizes
  • Show icon preview

Troublshooting

Reset finder preferences

Terminal window
rm ~/Library/Preferences/com.apple.finder.plist
defaults delete com.apple.finder
killall Finder
sudo reboot