Nix – The Purely Functional Package Manager for Linux

Nix is a powerful, purely functional package management system designed for reliable and reproducible package management, released under the terms of the GNU LGPLv2.1. It is the primary package management system in NixOS, a lesser known Linux distribution.

Nix offers atomic upgrades and rollbacks, multiple versions of package installation, multi-user package management and effortless setup of build environments for a package, regardless of what programming languages and tools a developer is using.

Under Nix, packages are built from a functional package language called “Nix expressions”. This functional approach to package management guarantees that installing or upgrading one package cannot break other packages.

Nix also has multi-user support, which implies that normal (or non-privileged) system users can securely install packages and each user is identified by a profile (a collection of packages in the Nix store that appear in the user’s PATH).

In case one user has installed a package, if another user tries to install the same package, the package will not be built or downloaded a second time.

It currently supports Linux (i686, x86_64) and Mac OS X (x86_64). However, it is fairly portable, you can try it on most platforms that support POSIX threads and have a C++11 compiler.

In this article, we will show how to install (in multi user mode) and use Nix package manager in Linux. We will discuss some of the basic package management tasks in relation to the commonly used tools.

How to Install Nix Package Manager in Linux

We will install the latest version of Nix (v2.1.3 at the time of writing) in multi user mode. Fortunately, there is a ready prepared installation script that you can run from your shell as a normal user using following curl command on your system.

$ sh <(curl https://nixos.org/nix/install) --daemon

Running the above command will download the latest nix binary tarball, and you will land in the multi-user nix installation screen as shown in the screenshot.

Install Nix Package Manager in Linux

To view a detailed list of what will happen during the installation process, type y and press Enter. If you are satisfied and ready to continue, type y and press Enter.

Nix Installation Summary

The script will invoke sudo command many times as needed. You need to permit it to use sudo by answering y and hitting Enter.

Nix Installation Continues

The installer will then run a few tests and generate a Nix config report, create build users between the user IDs 30001 and 30032, and a group with the group ID 30000. Enter y to continue when prompted. It will set up the build groups for the different build users, make the basic directory structure of Nix.

It will modify the file /etc/bashrc, (and /etc/zshrc for the zsh) if they exist. Note that it first backs up the mentioned files with a .backup-before-nix extension and the installer also creates the file /etc/profile.d/nix.sh.

The installer will also set up the nix-daemon service and nix-daemon socket service, loads the systemd unit for nix-daemon and starts the two aforementioned services.

Once the installation is complete, you need to open a new terminal window to start using Nix. Alternatively, close and reopen your shell to apply the recent changes. Then source the file /etc/profile.d/nix.sh (because its not a shell startup file, opening a new shell will not source it).

$ source /etc/profile.d/nix.sh

Next, run the following command to download some paths from the official project website, required for Nix to operate. After all paths are downloaded and copied to the correct locations, you will see a system and nix installation type summary as shown in the screenshot.

$ nix-shell -p nix-info --run "nix-info -m"

Nix Installation and System Summary

How to Use Nix Package Manager in Linux

Under Nix, package management is done by the nix-env utility. It is used to install, upgrade, and remove/erase packages, and to query what packages are installed or are available for installation.

All packages are located in a Nix channel, which is a URL that points to a repository comprising both a collections of Nix expressions and a pointer to a binary cache.

The default channel is Nixpkgs and the list of subscribed channels are stored in ~/.nix-channels, you can list them using the following command (no output means no channels).

$ nix-channel --list

To add the Nix channel, use the following command.

$ nix-channel --add https://nixos.org/channels/nixpkgs-unstable

Before you install any packages, start by updating the Nix channel; this is similar to running apt update under the APT package manager.

$ nix-channel --update

Update Nix Channel

You can query what packages are available for installation using the following command.

To install a package, use the following command by specifying the package version, for example apache-tomcat-9.0.2.

$ nix-env -i apache-tomcat-9.0.2

Install a Package Using Nix

On the local system, Nix stores packages in the Nix store, which is by default the /nix/store directory, where each package has its own unique sub-directory. For instance, the apache-tomcat packages is stored in:

/nix/store/95gmgnxlrcpkhlm00fa5ax8kvd6189py-apache-tomcat-9.0.2

In this path, the random characters 95gmgnxlrcpkhlm00fa5ax8kvd6189py is a unique identifier for the package that takes on account all its dependencies.

You can list installed packages with the following command.

$ nix-env -q

List Installed Packages

To upgrade the apache-tomcat package, you can use the -u upgrade switch as shown.

$ nix-env -u apache-tomcat

If you want to remove/erase apache-tomcat, use the -e flag. Here, a package is not erased from the system immediately, it is only rendered unused. This is useful because you want to do a rollback, or it might be in the profiles of other users.

$ nix-env -e apache-tomcat

After removing a package, you can do some garbage collection with the nix-collect-garbage utility.

Summary

Nix is a purely functional package manager designed for reliable and reproducible package management. It provides an interesting concept of package management, very distinct to commonly used tools in Linux such as YUM, APT, and many others.

In this article, we showed how to install nix in multi user mode and discussed how to do package management with Nix. Share your thoughts with us or ask any questions via the comment form below. Lastly, in an upcoming article, we will explain more Nix package management commands. Until then, stay connected.

Aaron Kili is a Linux and F.O.S.S enthusiast, an upcoming Linux SysAdmin, web developer, and currently a content creator for TecMint who loves working with computers and strongly believes in sharing knowledge.

Your name can also be listed here. Got a tip? Submit it here to become an TecMint author.

7 Responses

Indeed. Let’s reinvent the wheel yet again, for the umpteenth time – after all, Linux has, how many different package managers? Tens? Scores? I am somewhat surprised this one does not come with a new terminal emulator, of which we have dozens as well.

Seriously, is this what Linux has become? A platform where the wheel is reinvented over, and over, and over again?