Nix – A Powerful Package Manager For Linux And Unix
A while ago, we have written about Linuxbrew, a common package manager for Linux and Mac OS X. The distinguishing feature of Linuxbrew is that you can install packages without root or sudo privilege. Today, in this guide, we will discuss about yet another similar package manager. Meet Nix, a purely functional and reliable package manager for Linux and other Unix variants. Unlike other package managers, it provides side-by-side installation of multiple versions of same package, atomic and rollback support, per-user package management, easy setup of package development environments, and much more. Nix package manager was originally developed for NixOS, a GNU/Linux distribution with a unique approach to package and configuration management. NixOS was initially started as a research project by Eelco Dolstra in 2003.
Nix package manager is bit different from other Linux package managers in-terms of functionality and features. Concerning about the features, we can list the following:
- Multi-user package management system. That means, each user in the system can have a different set of installed packages.
- The users can install packages without root or sudo user privileges.
- Nix package manager stores packages in the Nix store, usually the directory /nix/store. Each package has its own unique subdirectory. Say for example, the package gzip will be stored under a unique directory /nix/store/c5y9w08xkiz6kvnscpjv3205gyr6iybz-gzip-1.8/ along with all required dependencies and libraries.
- Different users can have different versions of same package installed at the same time. It’s good for developers who wants to test their applications built with different versions of packages and dependencies.
- Since all packages are installed in unique directories, one user can’t interfere (like remove, update or upgrade packages) with packages installed by another user. Also, upgrading or updating a package doesn’t affect the other version of the same package or its dependencies.
- Rollback packages to previous versions is possible.
- Atomic upgrade support. It means that when you upgrade a package, it doesn’t overwrite the existing version. Each new version will be stored under new unique directory. Since package aren’t overwritten, the old versions are still available even after an upgrade.
- Unused packages can be safely removed.
- DevOps-friendly package manager. Nix helps users to build packages from sources, if they’re not available in the repositories yet. So, Nix is extremely useful for developers as it makes it easy to automatically set up the build environment for a package.
- Nix package manager can be used in GNU/Linux and Mac OS X.
Now, we will see how to install Nix Package manager in Unix-like operating systems.
Install Nix package manager In Linux
Installing Nix package manager is trivial.
To install it on your Linux box, run as normal user:
$ curl https://nixos.org/nix/install | sh
% Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 2354 100 2354 0 0 833 0 0:00:02 0:00:02 --:--:-- 833 downloading Nix 1.11.9 binary tarball for x86_64-linux from 'https://nixos.org/releases/nix/nix-1.11.9/nix-1.11.9-x86_64-linux.tar.bz2' to '/tmp/nix-binary-tarball-unpack.SoCHJhv4Vz'... % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 27.3M 100 27.3M 0 0 525k 0 0:00:53 0:00:53 --:--:-- 248k performing a single-user installation of Nix... directory /nix does not exist; creating it by running ‘mkdir -m 0755 /nix && chown sk /nix’ using sudo [sudo] password for sk: copying Nix to /nix/store............................... initialising Nix database... creating /home/sk/.nix-profile installing ‘nix-1.11.9’ building path(s) ‘/nix/store/7dv1lghxz40rbvv9ffg7fq2as972a4r7-user-environment’ created 6 symlinks in user environment downloading Nix expressions from ‘https://d3g5gsiof5omrk.cloudfront.net/nixpkgs/nixpkgs-17.09pre108299.ec9a23332f/nixexprs.tar.xz’... downloading ‘https://d3g5gsiof5omrk.cloudfront.net/nixpkgs/nixpkgs-17.09pre108299.ec9a23332f/nixexprs.tar.xz’... [7625/8656 KiB, 403.0 KiB/s] unpacking channels... created 2 symlinks in user environment modifying /home/sk/.bash_profile... Installation finished! To ensure that the necessary environment variables are set, either log in again, or type . /home/sk/.nix-profile/etc/profile.d/nix.sh in your shell.
After installing Nix, log out and log in back to your system to verify that the necessary environment variables are properly set. Alternatively, run the following command from the Terminal to ensure it.
$ . /home/sk/.nix-profile/etc/profile.d/nix.sh
Please note that there is a dot (.) at the beginning. If you don’t see any output, then you’re done! Nix package manager has been successfully installed on your system. Also, add the above line to your profile PATH to avoid running the above command every time after logging in to your system.
To do so, edit ~/.profile or ~/.bashrc (or similar):
$ vi ~/.bashrc
Add the following line at the end.
Save and close the file. Reboot your system. Now, you don’t have to run the above command each time after logging in to your system.
The following tutorial explains how to use Nix package manager to perform general package management operations such as installing, removing, updating, upgrading, and building packages.