Skip to content

Nix

The Nix package manager is an advanced tool for advanced users that can be used to acquire virtually any free software package you can imagine from its large, community-maintained repository.

It can be installed on KDE Linux for a single-user system. Nix stores software and dependencies under /nix/store, and keeps your search path updated automatically as tools are added or removed.

Installation

The Nix package manager can be installed for the current user like so:

sh <(curl --proto '=https' --tlsv1.2 -L https://nixos.org/nix/install) --no-daemon

Log out and back in again, then test:

nix-shell -p mc emacs-nox  # you are now inside a shell with mc and emacs

Note that when you quit this shell, mc and emacs are no longer in your shell’s search path. See the next section to make such tools available persistently.

See https://nixos.org/download/#nix-install-linux for further details.

Persistent package installations

Use nix-env -iA or nix-env --install --attr to install packages to /nix/store and make them available in your user’s search path:

$ nix-env -f '' -iA mc
⋮
building '/nix/store/1q2sh2f178jx9fl37gd9ks85ysfvpwd9-user-environment.drv'...

This creates a new “environment” which contains all the state changes necessary to make the mc package available, and these can be rolled forward or backward. This is the ''real'' appeal of the Nix package management system, but beyond the scope of this guide.

Verify the mc command is now available for your user:

$ which mc
/home/username/.nix-profile/bin/mc

Remove packages with nix-env --uninstall or nix-env -e:

$ nix-env -e mc
uninstalling 'mc-4.8.33'
building '/nix/store/rqbghiyds6f7a49qiwi9gmq7bnkgn9qi-user-environment.drv'...

See the section about [[#garbage-collection|garbage collection]] below for how to reclaim space used by packages that you're no longer using.

See https://nix.dev/manual/nix/latest/command-ref/nix-env.html for further details.

Using nix profile

The nix <command> interface is still technically considered experimental (more context on the “experimental” status of nix profile can be found in this RFC), but in practice it’s widely-used, and more ergnomic. Tab-completion and the --help options for the various nix subcommands will also work out of the box on a fresh installation. Like nix-env, nix profile will install packages to /nix/store and make them available in your search path, persisting between login sessions.

Here’s a trivial example using nix profile to install the code search tool ag:

$ mkdir -p ~/.config/nix
$ cat >> ~/.config/nix/nix.conf <<EOF
experimental-features = nix-command flakes
EOF

$ nix search nixpkgs silver searcher
⋮
* legacyPackages.x86_64-linux.silver-searcher (2.2.0)
  Code-searching tool similar to ack, but faster

$ nix profile add nixpkgs#silver-searcher
$ which ag
/home/username/.nix-profile/bin/ag

To list installed packages or remove them:

$ nix profile list
⋮
Name:               silver-searcher
Flake attribute:    legacyPackages.x86_64-linux.silver-searcher
Original flake URL: flake:nixpkgs
Locked flake URL:   github:NixOS/nixpkgs/f08e6b11a5ed43637a8ac444dd44118bc7d273b9?narHash=sha256-7vUo0qWCl/rip%2Bfzr6lcMlz9I0tN/8m7d5Bla/rS2kk%3D
Store paths:        /nix/store/l120si3zsshmf3jr7dkn5dvcj0n586xb-silver-searcher-2.2.0

$ nix profile remove silver-searcher

See the section about garbage collection below for how to reclaim space used by packages that you're no longer using.

See https://nix.dev/manual/nix/latest/command-ref/new-cli/nix3-profile.html for further details.

Reinstalling Nix

If you've enabled the “experimental” nix profile feature, you'll need to disable that and delete the state files associated with your user's profile before reinstalling, otherwise you'll get an error message like this:

error: profile '/home/username/.local/state/nix/profiles/profile' is incompatible with 'nix-env'; please use 'nix profile' instead
Warning: this will forget any previously-installed packages!

Comment out the experimental-features line in your Nix configuration file, and then delete the profile state directory:

$ sed -i'' 's/^experimental-features = nix-command flakes$/#&/' ~/.config/nix/nix.conf
$ rm -rf ~/.local/state/nix/profiles/profile

Now you can repeat the installation steps.

Garbage-collecting to reclaim disk space

Note that nix-env -e and nix profile remove both remove commands provided by a package from your user's current evironment, but the software actually remains installed in /nix/store until it is garbage collected.

You can force that garbage collection, to reclaim disk space, with one of the following commands:

nix-store --gc           # routine garbage collection
nix-collect-garbage -d   # remove "unreachable" packages from the store

See also


Article contributed by Ernstki under the CC-BY-4.0 license.