I.M Blogging

Nix on Debian

A guide on the best way to use the Nix package manager on Debian.

Nix?

If you haven’t heard, NixOS is a declarative and reproducible Linux distro that prides itself on extreme stability while also maintaining close to bleeding edge packaging. Its got a whole ecosystem of components capable of doing some really amazing stuff. While all of this is great, one of the coolest parts of Nix is the over 100,000 packages that it has available through Nixpkgs. The largest by any degree.

repology stats of package repositories

The best part, the package manager can be used on unrelated distros. Allowing almost any Linux system access to Nixpkgs trivially.

Why Debian?

Cause I ❤️ Debian.

The Setup

For this guide/tutorial I’m going to be using a Debian 12 (bookworm) system to install Nix onto, though anything since 11 should work fine. If you’re using a Debian based system all these steps should also work.

Installation

There are two main ways to install Nix. First you could use the native Debian package, the second would be the official install script. I’ll be going over the native package since I think that’s the best way to get Nix on Debian.

Debian Nix

Debian actually packages Nix (since bullseye) in it’s stable branch making it easy to install the binaries though regular old apt. Though I do recommend using the nix-setup-systemd package, which does a lot of the heavy lifting automatically.

sudo apt install nix-setup-systemd

Now that Nix is on your system you’ve got to add yourself to the nix-users group to be able to interact with the package manager.

sudo usermod -aG nix-users $(whoami)

After you’ve added yourself to nix-users make sure to login again for the changes to take affect (sudo su $(whoami)). Then you have to add a channel that Nix will pull its packages from. The easiest one to use is nixpkgs-unstable.

nix-channel --add https://nixos.org/channels/nixpkgs-unstable nixpkgs && \
nix-channel --update

Finally add ~/.nix-profile/bin to the PATH, and add ~/.nix-profile/share to XDG_DATA_DIRS if you want your desktop environment to be able to find anything that you install. I recommend doing this in your ~/.profile, but ~/.bashrc or ~/.zshrc also work. Below is what I use to make sure PATH and XDG_DATA_DIRS are set on my system.

### set PATH so it includes user's nix bin if it exists
if [ -d "$HOME/.nix-profile/bin" ] ; then
    PATH="$HOME/.nix-profile/bin:$PATH"
fi

### set XDG_DATA_DIR so it includes user's nix share if it exists
if [ -d "$HOME/.nix-profile/share" ] ; then
    XDG_DATA_DIRS="$HOME/.nix-profile/share:$XDG_DATA_DIRS"
fi

After all of that you can pretty much install anything from Nixpkgs.

Further Configuration

Nix does have extra features and config options that are not enabled by default. The most popular ones to change are allowing non-free packages, and new CLI along with flakes.

non-free packages

Nix does not allow you to install the non-free packages from its archive unless you explicitly tell it to. There are different ways to do this depending on whether you want it temporally, or allowing non-free by default. adding { allowUnfree = true; } to you’re Nixpkg config (~/.config/nixpkgs/config.nix) is the simplest way to enable non-free packages. Though if you only need it for a temporary shell or a build just export NIXPKGS_ALLOW_UNFREE=1 when you need to use non-free software.

experimental features

Nix have some features that are still in the works which is why they are not on by default. Nix will check ~/.config/nix/nix.conf for which features you’ve enabled, and you probably just want nix-command and flakes to start off with.

experimental-features = nix-command flakes

Both ~/.config/nixpkgs/config.nix and ~/.config/nix/nix.conf may not be created by default. If that is the case, create the appropriate directory and files.

Basic usage

Now that you have a working Nix install, you can use it to easily and safely install tens of thousands of new packages onto your machine. For installing a package just find it on https://search.nixos.org/packages which will show you the package name and some commands to install, though they generally look like this.

nix-env -iA nixpkgs.PACKAGENAME

To uninstall packages just use nix-env -e

nix-env -e PACKAGENAME

Updating

First Nix needs to update the channel(s) that you’ve added with nix-channel --update, after which you can call nix-env -u to update everything, or nix-env -u PACKAGENAME to update a specific package.

nix-channel --update && nix-env -u

I also recommend that you use nix-collect-garbage when you update, as Nix keeps each generation of packages that you’ve installed/updated/uninstalled, so there is always a working version of them to rollback to. This eats up a lot of space, especially if you make a lot of changes to the system in quick succession.

I usually run something like this when I update my packages.

nix-channel --update && nix-env -u && nix-collect-garbage -d

Broken Things

While Nix will add tonnes of stable packages to you system with ease, there is one downside to this all. GUI applications are more of a crap shoot of if they’ll work, while anything with a CLI has worked 100% of the time I’ve tried. This is something that I’ve still working on, so expect an update when I find out how to fix this. In the mean time I recommend using flatpak as that’s how I usually get GUI apps.

TL-DR

Run this to install.

sudo apt install -y nix-setup-systemd && \
sudo usermod -aG nix-users $(whoami) && \
newgrp nix-users 

Then run this.

nix-channel --add https://nixos.org/channels/nixpkgs-unstable nixpkgs && \
nix-channel --update && \
newgrp -

Add ~/.nix-profile/bin to your PATH and ~/.nix-profile/share to XDG_DATA_DIRS, use nix-env -iA nixpkgs.PACKAGENAME to install stuff that you find on https://search.nixos.org/packages.


Tags: