Skip to content

Instantly share code, notes, and snippets.

@toonn
Last active April 21, 2023 14:23
Show Gist options
  • Star 22 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save toonn/94a38643d8fa5c9e06100cdfa81ba163 to your computer and use it in GitHub Desktop.
Save toonn/94a38643d8fa5c9e06100cdfa81ba163 to your computer and use it in GitHub Desktop.
Tutorial for installing Nix on macOS

Installing Nix on your Mac

We will perform a multi-user install, putting the Nix store on an APFS volume without FileVault encryption. Not that if your machine has a T2 chip the volume will still be encrypted at rest.

  1. Run the installer:

    $ sh <(curl -L https://nixos.org/nix/install) --darwin-use-unencrypted-nix-store-volume --daemon
    

    The installer is interactive but you probably want to answer affirmatively (y) to all of the questions.

  2. Restart your terminal so the proper files are sourced and test your installation:

    $ nix-shell -p nix-info --run "nix-info -m"
    

Uninstalling

  1. Edit /etc/zshrc and /etc/bashrc to remove the lines sourcing nix-daemon.sh, which should look like this:

    # Nix
    if [ -e '/nix/var/nix/profiles/default/etc/profile.d/nix-daemon.sh' ]; then
      . '/nix/var/nix/profiles/default/etc/profile.d/nix-daemon.sh'
    fi
    # End Nix
    

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

    $ sudo mv /etc/zshrc.backup-before-nix /etc/zshrc
    $ sudo mv /etc/bashrc.backup-before-nix /etc/bashrc
    

    This will stop shells from sourcing the file and bringing everything you installed using Nix in scope.

  2. Edit fstab using sudo vifs to remove the line mounting the Nix Store volume on /nix, which looks like this, LABEL=Nix\040Store /nix apfs rw,nobrowse. This will prevent automatic mounting of the Nix Store volume.

  3. 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.

  4. Stop and remove the Nix daemon service:

    $ 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.activate-system.plist
    $ sudo rm /Library/LaunchDaemons/org.nixos.activate-system.plist
    

    This stops the Nix daemon and prevents it from being started next time you boot the system.

  5. Remove the files Nix added to your system:

    $ sudo rm -rf /etc/nix /var/root/.nix-profile /var/root/.nix-defexpr /var/root/.nix-channels ~/.nix-profile ~/.nix-defexpr ~/.nix-channels
    

    This gets rid of any data Nix may have created except for the store which is removed later.

  6. Remove the nixbld group and the _nixbuildN users:

    $ sudo dscl . delete /Groups/nixbld
    $ for i in $(seq 1 32); do sudo dscl . -delete /Users/_nixbld$i; done
    

    This will remove all the build users that no longer serve a purpose.

  7. Remove the Nix Store volume:

    $ sudo diskutil apfs deleteVolume /nix
    

    This will remove the Nix Store volume and everything that was added to the store.

The change to /etc/synthetic.conf will only take effect after a reboot but you shouldn't have any traces of Nix left on your system.

@fabienengels
Copy link

Thank you ! :)

@toonn
Copy link
Author

toonn commented Mar 30, 2023

@fabienengels, you're welcome. Though note that this was basically merged into the Nix manual so the install section there should be complete and up-to-date, whereas this Gist will likely slowly get out of date.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment