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.

 

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.

 

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

 

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"

 

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

 

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

$ nix-env -qa

 

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"

 

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

 

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

 

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

 

Дали Ви помогна овој одговор? 2 Корисниците го најдоа ова како корисно (72 Гласови)