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
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
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
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
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
Update Nix Channel

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

$ nix-env -qa
Query Packages Available for Installation
Query Packages Available for Installation

In this example, we will install the Apache Tomcat server using the previous command in conjunction with grep to find the package is available to install as shown.

$ nix-env -qa | grep "apache-tomcat"
Find a Package for Installation
Find a Package for Installation

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
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
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.

$ nix-collect-garbage

How to Remove Nix Package Manager in Linux

To uninstall Nix, remove all nix related files at one go.

$ sudo rm -rf /etc/profile/nix.sh /etc/nix /nix ~root/.nix-profile ~root/.nix-defexpr ~root/.nix-channels ~/.nix-profile ~/.nix-defexpr ~/.nix-channels

On systems with systemd, run the following commands to stop the all nix related services and disable them.

$ sudo systemctl stop nix-daemon.socket
$ sudo systemctl stop nix-daemon.service
$ sudo systemctl disable nix-daemon.socket
$ sudo systemctl disable nix-daemon.service
$ sudo systemctl daemon-reload

In addition, you need to remove any references to Nix in these files: /etc/profile, /etc/bashrc, and /etc/zshrc.

For more information, see the man pages of the above utilities we have looked at.

$ man nix-channel
$ man nix-env

You can find the Nix Package Manager documentation in the project website: https://nixos.org/nix/.

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.

Hey TecMint readers,

Exciting news! Every month, our top blog commenters will have the chance to win fantastic rewards, like free Linux eBooks such as RHCE, RHCSA, LFCS, Learn Linux, and Awk, each worth $20!

Learn more about the contest and stand a chance to win by sharing your thoughts below!

Aaron Kili
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.

Each tutorial at TecMint is created by a team of experienced Linux system administrators so that it meets our high-quality standards.

Join the TecMint Weekly Newsletter (More Than 156,129 Linux Enthusiasts Have Subscribed)
Was this article helpful? Please add a comment or buy me a coffee to show your appreciation.

8 Comments

Leave a Reply
  1. 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?

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

      Linux’s biggest strength of freedom to do your own thing is also its biggest weakness. Anybody who can code thinks they can build a better wheel or a better mouse trap.

      Reply

Got Something to Say? Join the Discussion...

Thank you for taking the time to share your thoughts with us. We appreciate your decision to leave a comment and value your contribution to the discussion. It's important to note that we moderate all comments in accordance with our comment policy to ensure a respectful and constructive conversation.

Rest assured that your email address will remain private and will not be published or shared with anyone. We prioritize the privacy and security of our users.