Open In App

Nix – The Purely Functional Package Manager for Linux

Improve
Improve
Like Article
Like
Save
Share
Report

Nix is a purely functional package manager for Linux, that serves to provide a purely functional approach to any system’s software package management. Due to its functional and declarative approach, it is lauded for its capacity to: 

  • Support the installation of multiple versions of a given package.
  • Enable atomic upgrades and rollbacks of a given package.

Drawbacks of Traditional Package Managers and how Nix Solves these Problems

  • Dependency resolution: Traditional package managers often struggle with resolving dependencies, leading to issues such as dependency hell. In contrast, Nix uses a functional approach to package management, which allows it to automatically resolve dependencies and avoid conflicts.
  • Reproducibility: Traditional package managers can be difficult to use for reproducible builds, as they do not always provide a clear way to specify the exact versions of packages that should be used. Nix, on the other hand, allows users to specify exact versions of packages, making it easier to reproduce builds.
  • Rollback: Traditional package managers do not have an easy way to roll back to a previous version of a package. Nix, on the other hand, allows users to switch between different versions of packages easily, making it easier to roll back if necessary.
  • Multi-user support: Traditional package managers are not designed to support multiple users on a single system, which can lead to conflicts and permission issues. Nix, on the other hand, has built-in support for multiple users, allowing them to have separate package environments without affecting each other.
  • Complexity: Traditional package managers can be complex to use, particularly for users who are not familiar with command-line interfaces. Nix, on the other hand, has a simpler user interface and is easier for new users to learn.

Features of Nix

One of the key features of Nix is its support for declarative configuration. This means that users can specify the packages and dependencies that they want to install on their system, and Nix will automatically handle the process of installing and configuring those packages. This makes it easier for users to manage their system and ensures that all necessary dependencies are installed and configured correctly.

In addition to its declarative configuration support, Nix also offers atomic upgrades and rollbacks. This means that when a package is upgraded or installed, Nix creates a new configuration for the system that includes the updated package. If the upgrade causes problems in the system, users can easily roll back to the previous configuration using Nix. This helps to reduce the risk of breakages and makes it easier to recover from problems.

Another key feature of Nix is its support for multiple versions of packages on the same system. This means that users can install and use multiple versions of the same package on their system, which can be useful for testing or development purposes. Nix makes it easy to switch between different versions of a package and ensures that all necessary dependencies are installed and configured correctly.

Installation and Usage of Nix

Nix can be installed on Linux distributions using the following procedure:

Open the shell, and use curl to run the installation script:

For single-user installation:

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

For multi-user installation:

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

 

The installation script will then proceed to download and install Nix on your system. This process may take several minutes to complete. To ensure that the necessary environment variables are set, either log in again, or type

./home/your_user_name/.nix-profile/etc/profile.d/nix.sh

You can then use the nix-env command to manage packages on your system.

Managing Profiles

Nix uses profiles to manage packages. To list your available profiles, use the command nix-env –list-generations. To switch to a different profile, use the command  nix-env –switch-generation <generation>. For example:

nix-env --list-generations
nix-env --switch-generation 2
nix-env --switch-generation 3

 

Managing Channels

Nix uses channels to manage package repositories. To list your available channels, use the command nix-channel –list. To add a new channel, use the command nix-channel –add <channel-url>.

For example:

 nix-channel --list

 

Searching for a Package

To search for a package, you can use the nix-env -qa command followed by the package name. For example:

nix-env -qa firefox

 

Installing a package

To install a package, you can use the nix-env -i command followed by the package name. For example:

nix-env -i firefox

 

Upgrading packages

To upgrade a package, use the command nix-env -u <package-name>. 

For example:

 nix-env -u firefox

 

As the firefox package had just been installed, there was no upgrade.

Rolling back packages

 To roll back to a previous version of a package, use the command nix-env –rollback. This will roll back to the previous generation of packages. For example:

 nix-env --rollback

 

Uninstalling packages

To uninstall a package, use the command nix-env -e followed by the package name. For example:

nix-env -e firefox

 

Garbage removal

To remove old or unused packages, use the command nix-collect-garbage:

nix-collect-garbage

 

Uninstalling Nix

Nix can be uninstalled from a Linux system using the following command:

sudo apt-get remove nix

 

To summarize, Nix is a purely functional package manager with various features, such as atomic upgrades/rollbacks and support for multiple installations. It can be easily installed on a Linux system by downloading the Nix installation script and running it on the system. Once Nix is installed, users can use the nix-env command to manage packages on their system.


Last Updated : 05 Feb, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads