Hello everyone! Recently, I have noticed more and more people around me talking about Nix. As a tech newbie, although I don’t have much experience in coding, I deeply feel the inconvenience of using HomeBrew as a package manager on macOS in my daily use.

So, I decided to give Nix a try for package management! A big step for a tech newbie!

I am using a Mac mini M1, and I believe machines with similar Apple silicon can also benefit from my experience. There is very limited Chinese content online about installing Nix on macOS, so I decided to share some of my experiences, hoping to help more friends!

Switching zsh to bash

On macOS, the default shell is zsh. Here, I suggest 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, let’s install Nix: the package manager (MacOS)

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! 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:

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.

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

Then rerun the installation script.

So, welcome to the Nix family!

Here I briefly introduce two ways of usage.

  • If you want to install packages to the current user environment, use nix-env. For instance, to install python3:
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:
nix-shell -p python3

Successful output will be:

[nix-shell:~]$

Some commonly used commands:

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

Using Nix Flakes

Nix Flakes is a new feature of Nix that provides a more modern and modular way to manage Nix packages. Here are the steps to enable and use Nix Flakes:

Enabling Nix Flakes

First, you need to enable Flakes support in the Nix configuration. Edit or create the ~/.config/nix file and add the following content:

mkdir ~/.config/nix
echo "experimental-features = nix-command flakes" > ~/.config/nix/nix.conf

Using nix profile

After enabling Flakes, you can use the nix profile command to manage packages. For example, to install the hello package:

nix profile install nixpkgs#hello

This will install the hello package into the current user’s environment.

To view the list of installed packages, use the following command:

nix profile list

To uninstall a package, use the following command:

nix profile remove nixpkgs#hello

With these commands, you can easily manage packages installed using Nix Flakes.

To find packages you want to install, you can search for them on 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