Skip to content

Instantly share code, notes, and snippets.

@chriselsner
Last active September 19, 2024 00:46
Show Gist options
  • Save chriselsner/3ebe962a4c4bd1f14d39897fc5619732 to your computer and use it in GitHub Desktop.
Save chriselsner/3ebe962a4c4bd1f14d39897fc5619732 to your computer and use it in GitHub Desktop.
Nix on macOS Catalina

Nix on macOS Catalina

I'm writing this gist for my own records but it might help someone else too.

Installing Nix

Support for Catalina has improved a lot since the update was first rolled out.

Note: See the NixOS manual for discussion of the --darwin-use-unencrypted-nix-store-volume option.

Single-users

  1. Run

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

Multi-user

  1. Run

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

Uninstalling Nix

Single-user

  1. Remove the mount from /etc/fstab by running

    sudo vifs

    and deleting the line LABEL=Nix\040Store /nix apfs rw,nobrowse.

  2. Delete the APFS volume using diskutil

    diskutil apfs deleteVolume <volumeDevice>

Note: volumeDevice can be found by running

diskutil apfs list
  1. Remove the synthetic empty directory for mounting at /nix by running

    sudo vim /etc/synthetic.conf

    and deleting the line nix.

  2. Reboot the system for changes to take effect.

  3. Run

    echo "removing nix files"
    sudo rm -rf /nix
    rm -rf ~/.nix-profile
    rm -rf ~/.nix-defexpr
    rm -rf ~/.nix-channels
    rm -rf ~/.nixpkgs
    rm -rf ~/.config/nixpkgs
    rm -rf ~/.cache/nix

    to remove all remaining Nix artifacts from the system.

Note: There may also be references to Nix in the shell configurations files under your home directory (e.g. ~/.profile, ~/.bash_profile, ~/.zshenv, etc.) which you may remove.

Multi-user

  1. Kill the daemon process by running

    sudo launchctl unload /Library/LaunchDaemons/org.nixos.nix-daemon.plist
  2. Remove the mount from /etc/fstab by running

    sudo vifs

    and deleting the line LABEL=Nix\040Store /nix apfs rw,nobrowse.

  3. Delete the APFS volume using diskutil

    diskutil apfs deleteVolume <volumeDevice>

Note: volumeDevice can be found by running

diskutil apfs list
  1. Remove the synthetic empty directory for mounting at /nix by running

    sudo vim /etc/synthetic.conf

    and deleting the line nix.

  2. Reboot the system for changes to take effect.

  3. Run

    echo "removing daemon"
    sudo rm /Library/LaunchDaemons/org.nixos.nix-daemon.plist
    
    echo "removing daemon created users and groups"
    USERS=$(sudo dscl . list /Users | grep nixbld)
    
    for USER in $USERS; do
        sudo /usr/bin/dscl . -delete "/Users/$USER"
        sudo /usr/bin/dscl . -delete /Groups/staff GroupMembership $USER;
    done
    
    sudo /usr/bin/dscl . -delete "/Groups/nixbld"
    
    echo "reverting system shell configurations"
    sudo mv /etc/profile.backup-before-nix /etc/profile
    sudo mv /etc/bashrc.backup-before-nix /etc/bashrc
    sudo mv /etc/zshrc.backup-before-nix /etc/zshrc
    
    echo "removing nix files"
    sudo rm -rf /nix
    sudo rm -rf /etc/nix
    sudo rm -rf /etc/profile/nix.sh
    sudo rm -rf /var/root/.nix-profile
    sudo rm -rf /var/root/.nix-defexpr
    sudo rm -rf /var/root/.nix-channels
    sudo rm -rf /var/root/.cache/nix
    rm -rf ~/.nix-profile
    rm -rf ~/.nix-defexpr
    rm -rf ~/.nix-channels
    rm -rf ~/.nixpkgs
    rm -rf ~/.config/nixpkgs
    rm -rf ~/.cache/nix

    to remove all remaining Nix artifacts from the system.

@siviae
Copy link

siviae commented Dec 14, 2021

Thank you! I think it is worth mentioning that script at step 6 does not work in Zsh (due to variable substitution in for loop).
To support Zsh one should change
for USER in $USERS; do
to
for USER in $(echo "$USERS"); do

@jost-s
Copy link

jost-s commented Jan 13, 2022

Two more updates on this:

  1. Single user install is no longer supported and fails
  2. unencrypted nix store volume is no longer needed
Warning: the flag --darwin-use-unencrypted-nix-store-volume
         is no longer needed and will be removed in the future.

Error: --no-daemon installs are no-longer supported on Darwin/macOS!

@fxcl
Copy link

fxcl commented Jan 26, 2022

yes, latest version for nix, single user install is no longer supported for macos.
you can use curl -L -o install-nix https://releases.nixos.org/nix/nix-2.3.16/install sh install-nix --darwin-use-unencrypted-nix-store-volume

@siddarthkay
Copy link

siddarthkay commented Feb 11, 2022

for me this was useful back when i needed nix v2.3
sh <(curl -L https://releases.nixos.org/nix/nix-2.3.16/install) --darwin-use-unencrypted-nix-store-volume

@carlgieringer
Copy link

Here are NixOS's official uninstall instructions for macOS: https://nixos.org/manual/nix/stable/installation/installing-binary.html#macos

@jost-s
Copy link

jost-s commented May 15, 2022

Here are NixOS's official uninstall instructions for macOS: https://nixos.org/manual/nix/stable/installation/installing-binary.html#macos

Thanks for sharing it, @carlgieringer!

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