Uninstall nix on MacOS

要在 Mac OS 上卸载 nix,请完成以下步骤::

Step 1: 编辑 /etc/zshrc/etc/bashrc 并删除以下行 sourcing nix-daemon.sh

#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

如果 zshrcbashrc 这两个文件在安装 Nix 后没有改变,你可以简单地将它们作为备份。 最新的安装程序还会运行初步检查以进行验证。

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

Step 2: 使用 sudo vifs 编辑 fstab 以删除在 /nix 上安装 Nix 存储卷的行,如下所示,LABEL=Nix\040stORE /nix apfs rw,nobrowse。 这将防止自动挂载 Nix 存储卷

Step 3: 编辑 /etc/synthetic.conf 以删除 nix 行。 如果这是文件中唯一可以将其完全删除的行,sudo rm /etc/synthetic.conf

Step 4: 停止并删除 Nix 守护程序服务:

sudo launchctl unload /Library/LaunchDaemon/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

这会停止nix服务并阻止其启动

Step 5: 删除 Nix 添加到系统的文件

sudo rm -rf /etc/nix /var/root/.nix-profile /var/root/.nix-defexpr /var/root/.nix-channels ~/.nix-profile ~/.nix-defexpr ~/.nix-channels ~/.nixpkgs ~/.config/nixpkgs ~/.cache/nix

这将删除Nix可能创建的任何数据,但存储区之后会被删除

Step 6: 删除nixbld group和_nixbuildN users::

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

Step 7: 删除Nix存储卷

sudo diskutil apfs deleteVolume /nix
sudo rm -rf /nix/

这将删除添加到nix存储的所有内容。

Step 8: 重新启动系统

问题:#

Uninstalling /nix from macOS requires running sudo rm -rf /nix, but it always fails with the error “Resource busy ” However, if you attempt to kill the process using it, you’ll find that the parent process will immediately relaunch the troublesome process. Consequently, nix cannot be uninstalled.

Steps To Reproduce

  1. Try to remove /nix: sudo rm -rf /nix
  2. See the error: “Resource busy.”
  3. Identify the process using /nix: sudo lsof +D /nix
  4. Kill that process: sudo kill [PID]
  5. Repeat forever.

Alternatively you can just use Disk Utilities

First ERASESecond REMOVEThird EMPTY TRASHBIN

Bring up Disk Utilities then go and remove

After deleting you can rm -rf ~/nixrunning $ lsof +D /nix will show nothing, meaning it has been removed