Installing OS-level packages using Nix
Install any package with Nix. Learn how it works and how to transition there from apt-get.
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:
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.
Multiple versions: Different applications can use different versions of the same package without any conflict.
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 Arkane Cloud. 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):
You can remove packages with the -e
option:
And you can update packages with the -u
option:
You can find a list of available Nix packages including instructions for installation on their search: https://search.nixos.org/packages
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:
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.
You can also use this in our CI pipelines with a syntax similar to this: nix-shell --run "your command"
.
Creating your own packages
You can also use .nix files to create your own packages or even install things with other package managers that are themselves a nix package.
You can use this for example to install Java quarkus by adding the following shell.nix file:
Finding legacy packages
Sometimes it can be difficult to find package versions for older legacy versions of some frameworks like php because they are not maintained in the core Nix repository. We found this community maintained search engine to very helpful in these cases: https://lazamar.co.uk/nix-versions/?channel=nixpkgs-unstable&package=
Use with caution, some packages may be maintained better than others.
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.
Last updated