ariya.io About Talks Articles

Nix Package Manager on Ubuntu or Debian

3 min read

Even though Ubuntu/Debian is equipped with its legendary powerful package manager, dpkg, in some cases, it is still beneficial to take advantage of Nix, a purely functional package manager.

The complete manual of Nix does a fantastic job on explaining how to install and use it. But for the impatients among you, here is a quick overview. Note that this also works well when using Ubuntu/Debian under WSL (Windows Subsystem for Linux, both the original and the newest WSL 2.

Nix on Debian

First, create the /nix directory owned by you (this is the common single-user installation):

$ sudo mkdir /nix
$ sudo chown ariya /nix

And then, run the installation script:

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

Note that if you use WSL 1, likely you will encounter some error such as:

SQLite database '/nix/var/nix/db/db.sqlite' is busy

This a known issue, the workaround is to create a new file ~/.config/nix/nix.conf with the following content

sandbox = false
use-sqlite-wal = false

and repeat the previous step.

If nothing goes wrong, the script will perform the installation. Grab a cup of tea while waiting for it!

downloading Nix 2.3.4 binary tarball for x86_64-linux
performing a single-user installation of Nix...
copying Nix to /nix/store......................................
replacing old 'nix-2.3.4'
installing 'nix-2.3.4'
unpacking channels...

Note that the last step (unpacking channels) can run for a very long time (no idea why, hope it will be fixed at some point). Just be patient.

To check whether Nix is successfully installed, we use the Hello, world tradition:

$ nix-env -i hello
installing 'hello-2.10'
these paths will be fetched (6.62 MiB download, 31.61 MiB unpacked):
/nix/store/9l6d9k9f0i9pnkfjkvsm7xicpzn4cv2c-libidn2-2.3.0
/nix/store/df15mgn0zsm6za1bkrbjd7ax1f75ycgf-hello-2.10
/nix/store/nwsn18fysga1n5s0bj4jp4wfwvlbx8b1-glibc-2.30
/nix/store/pgj5vsdly7n4rc8jax3x3sill06l44qp-libunistring-0.9.10
$ which hello
/home/ariya/.nix-profile/bin/hello
$ hello
Hello, world!

In the above illustration, hello is a test package that does nothing but to display the famous message. It looks simple, and yet it is very useful!

To get the feeling of packages available at your disposal (almost 29 thousands of them):

$ nix-env -qa  > nix-packages.list
$ wc -l nix-packages.list
28974 nix-packages.list
$ less nix-package.list

While it is not a substitute for a large collection of existing Debian/Ubuntu packages, very often what you get from Nix is more up-to-date. For instance, if you are stuck with a typical Ubuntu 18.04 LTS, it offers git 2.17.1, tmux 2.6.3, jq 1.5, curl 7.58, and Neovim 0.2.2. But, with Nix on that same Ubuntu system, at the time of this writing, you can enjoy git 2.26.2, tmux 3.1b, jq 1.6, curl 7.69, and and Neovim 0.4.3.

The way I use Nix however is not merely as a mechanism to get fresher software and other utilities. Rather, the functional nature of Nix leads to the possibility of multiple working environment, with each distinctive set of applications and tools, and the ability of switching cleanly between them. Those who use nvm (for Node.js) or virtualenv (for Python) probably can appreciate this. Now, imagine nvm/virtualenv but not only for Node.js/Python, and rather applied to an arbitrary set of packages. I have covered this in details before in my previous blog post, Isolated Development Environment using Nix. That blog post talked about Nix on macOS but obviously the experience is very suited for Nix on Debian, Ubuntu, or any other Linux distributions for that matter.

I hope this will inspire you to explore Nix in depth!

Related posts:

♡ this article? Explore more articles and follow me Twitter.

Share this on Twitter Facebook

comments powered by Disqus