Jump to content

KDE Linux/Install software not available in Discover/Nix

From KDE Community Wiki

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

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

Installation

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

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.

Persistent package installations

Main help: https://nix.dev/manual/nix/latest/command-ref/nix-env.html

Use nix-env -iA or nix-env --install --attr[1][2] to install packages to /nix/store and make them available in your user's search path:

$ nix-env -f '<nixpkgs>' -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[3]:

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

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

Using nix profile

Main help: https://nix.dev/manual/nix/latest/command-ref/new-cli/nix3-profile.html

The nix <command> interface is still technically considered experimental[4], 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.

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[5], 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[6]:

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

External links

References

  1. https://nix.dev/manual/nix/latest/command-ref/nix-env/install
  2. The --attr / -A option is a speed optimization; if you want to go down the rabbit hole, read about it here.
  3. https://nix.dev/manual/nix/latest/command-ref/nix-env/uninstall.html
  4. More context on the "experimental" status of nix profile can be found in this RFC
  5. Source: https://nixos.wiki/wiki/Nix_command#Switching_between_nix-env_and_nix_profile
  6. See this NixOS forum post for details.