The voices of Nix are increasingly heard around me. However, as a tech novice, I have limited experience in coding. Nonetheless, in my daily use, I can feel how inconvenient HomeBrew as a package manager is under macOS.

Therefore, I decided to switch to Nix for package management! A big step for a tech newbie!!

The author’s machine is a Mac mini M1, and machines with similar Apple silicon should also be able to learn from this experience. There is very limited Chinese content online about installing Nix on macOS, so I will share some of the author’s experiences to help more friends!

Switching zsh to bash

On macOS, machines default to using zsh. Here, the author suggests first changing to bash, which can be done in two ways:

  • Execute chsh -s /bin/bash in the terminal. After completion, close the terminal and reopen it to see the successful change.
  • Open “System Preferences” > “Users & Groups” > Right-click on the user under “Admin” > “Advanced Options” > “Login Shell” and change it to /bin/bash.

Installing Nix: the package manager

Next, we install Nix: the package manager (MacOS)

1
sh <(curl -L https://nixos.org/nix/install)

During the installation process, as an automated script, it will carefully inform you of each step it performs, which is considerate! It will also tell you what has been executed. If you are unsure about the automated script, you can look closely at the output to make your judgment.

After installation, close the terminal and then open it again!

After reopening the terminal, verify a successful installation with the following command:

1
nix-shell --version

If successful, the version number will be displayed.

In case of installation failure, or if you wish to reinstall Nix, please rerun the installation script and follow the instructions provided. You may need to delete or back up and remove /etc/bashrc and /etc/bashrc.backup-before-nix as indicated by the output of the script.

1
rm /etc/bashrc /etc/bashrc.backup-before-nix

Then rerun the installation script.

So, welcome to the Nix family!

Here the author briefly introduces two ways of usage.

  • If you want to install packages to the current user environment, use nix-env. For instance, to install python3:
1
nix-env -iA nixpkgs.python3
  • If you wish to test the required package in a temporary environment (exit nix-shell will render it unusable), use nix-shell. For example, to use python3:
1
nix-shell -p python3

Successful output will be:

1
[nix-shell:~]$

Some commonly used commands:

  • Upgrade packages in the actual user environment:
1
nix-env --upgrade
  • Clear out unneeded packages (this should remove what you fetched inside nix-shell):
1
nix-collect-garbage -d
  • View the list of installed packages:
1
nix-env --query --installed
  • Uninstall packages you no longer need:
1
nix-env --uninstall <package-name>

To find packages you want to install, you can search for them on https://search.nixos.org.

References:

https://www.howtogeek.com/444596/how-to-change-the-default-shell-to-bash-in-macos-catalina/

https://nixos.org/download#nix-install-macos