Search
K

Installing OS-level packages using Nix

Introduction

The Nix Package Manager is a powerful tool for managing packages on Unix-like operating systems. Nix allows users to install software in a predictable, reproducible, and reliable manner. It's different from traditional package managers like apt-get, yum, and pacman in that it allows multiple versions of software to coexist on the same system without conflict.

How Nix Works

Nix works by installing packages into the Nix store, which is typically located at /nix/store. Each package in the store has a unique path that includes its name, version, and a hash of its dependencies.
This approach brings several advantages:
  1. 1.
    Atomic upgrades and rollbacks: Changes to the system are made atomically. If the installation of a package fails, the system remains in its previous state. Also, you can roll back to a previous configuration if required.
  2. 2.
    Multiple versions: Different applications can use different versions of the same package without any conflict.
  3. 3.
    Reproducible environment: Given a Nix expression, Nix will always build the same environment, regardless of the current state of the system.
In addition, once a package is installed, Nix automatically makes its commands available from the terminal by adding its installation location to the PATH environment variable. This means that you can start using the commands provided by the package immediately after installation, just like you're used to with other package managers.

Using Nix

Nix comes preinstalled with Codesphere. You can install packages with the nix-env command. For instance, to install hello, a program that produces a friendly greeting, you'd run (according to the package instructions):
nix-env -iA nixpkgs.hello
You can remove packages with the -e option:
nix-env -e hello
And you can update packages with the -u option:
nix-env -uA nixpkgs.hello
You can find a list of available Nix packages including instructions for installation on https://nixos.org/nixos/packages.html

Package Names and Versions in Nix

Please note that package names in Nix may differ from those in other package managers like apt-get. The exact names and versions of packages available in Nix can be viewed in the Nix package index on the mentioned NixOS website.
Some packages in Nix may be named differently or have different versions than in other package managers, as the Nix community maintains the packages independently.

Creating Project-Specific Environments with Nix

Unlike traditional package managers that install development tools and libraries system-wide, Nix allows for the creation of project-specific environments with the necessary tools and libraries. This can be achieved with the nix-shell command.
To create a project-specific environment, you need to create a shell.nix file in your project directory that lists the required dependencies. Here's a simple example:
{ pkgs ? import <nixpkgs> {} }:
pkgs.mkShell {
buildInputs = [ pkgs.python3 pkgs.python3Packages.numpy ];
}
You can then run nix-shell in your project directory to provide a shell with Python and NumPy. In this environment, you can be sure that you have the correct versions of your dependencies, regardless of the state of the rest of your system.

Transitioning from apt-get to Nix

If you're used to using apt-get or another traditional package manager, there are a few key differences you should be aware of when switching to Nix.
  • Installation location: As mentioned above, Nix installs packages in the Nix store, not in common directories like /usr/bin.
  • Root privileges: You don't need to use sudo to install packages with Nix. This is because Nix installs packages to your user's Nix store, not system-wide.
  • Multiple versions: Nix allows you to have multiple versions of a package installed at once. This is different from apt-get, which only allows one version to be installed at a time.
  • System configuration: Nix goes beyond package management and can manage your system configuration. This is a more advanced feature and goes beyond the scope of this introduction.