Gangmax Blog

自由之思想,独立之精神

Nix

| Comments

Nix is a powerful package manager for Linux and other Unix systems that makes package management reliable and reproducible. I install Nix to use boot.

Nix can be installed both on Linux and MacOS. In my case I use Ubuntu 16.04. For MacOS the instructions should be the same.

Installation

Run the following command to install Nix:

1
2
3
# You will be asked for sudo password since the command will create "/nix" directory
# in which the installed artifacts are saved.
curl https://nixos.org/nix/install | sh

After the installation finished, your “~/.bash_profile” file has the following added content:

1
if [ -e /home/gang/.nix-profile/etc/profile.d/nix.sh ]; then . /home/gang/.nix-profile/etc/profile.d/nix.sh; fi # added by Nix installer

If you are using “zsh” like me, copy the content to the end of your “~/.zshrc” file to make it take effect.

How to use

Then you can use Nix. For examples, with the following commands:

1
2
3
4
5
6
# List the available packages.
nix-env -qa
# Install the "boot" package.
nix-env --install boot
# Uninstall the "boot" package.
nix-env --uninstall boot

Note that, “nix-env” operations such as upgrades (-u) and uninstall (-e) never actually delete packages from the system. All they do (as shown above) is to create a new user environment that no longer contains symlinks to the “deleted” packages(from here).

Of course, since disk space is not infinite, unused packages should be removed at some point. You can do this by running the Nix garbage collector. It will remove from the Nix store any package not used (directly or indirectly) by any generation of any profile.

Note however that as long as old generations reference a package, it will not be deleted. After all, we wouldn’t be able to do a rollback otherwise. So in order for garbage collection to be effective, you should also delete (some) old generations. Of course, this should only be done if you are certain that you will not need to roll back.

To delete generations of your current profile, you can:

1
2
3
4
5
6
7
# To delete all old (non-current) generations of your current profile:
nix-env --delete-generations old
# Instead of old you can also specify a list of generations, e.g.:
nix-env --delete-generations 10 11 14
# To delete all generations older than a specified number of days
# (except the current generation), use the d suffix. For example:
nix-env --delete-generations 14d

After removing appropriate old generations you can run the garbage collector as follows:

1
2
3
nix-store --gc
# If you are feeling uncertain, you can also first view what files would be deleted: 
nix-store --gc --print-dead

There is also a convenient little utility nix-collect-garbage, which when invoked with the -d (–delete-old) switch deletes all old generations of all profiles in /nix/var/nix/profiles. So:

1
nix-collect-garbage -d

is a quick and easy way to clean up your system.

Uninstallation

From here.

Run the following commands to remove Nix completely from your computer:

1
2
3
4
5
6
7
# 1. As a normal user:
rm -r ~/.nix-channels ~/.nix-defexpr ~/.nix-profile
# 2. As a sudo user:
sudo rm -r /nix
# 3. Edit your "~/.bash_profile" or "~/.zshrc" file by removing the line
# containing "added by Nix installer", as list at the begining of this
# page.

Comments