Show all packages installed in NixOS

2018-02-20

I recently came across a great answer on Stack Overflow explaining how to show all packages installed on your system in NixOS. In this article I will add a little more explanation to the different commands used.

Showing installed packages

The difficulty of showing all installed packages on NixOS is that there are multiple ways to install packages:

  1. add a package to environment.systemPackages in /etc/nixos/configuration.nix
  2. install packages in your user profile with the command nix-env --install packagename
  3. install packages in root's profile with the command sudo nix-env --install packagename

There is currently no single command that will show packages installed in all three places. Instead, we need to use three different commands depending on where to check for packages.

Listing packages installed from /etc/nixos/configuration.nix

One way to list packages installed from configuration.nix is with the nixos-option command:

$ nixos-option environment.systemPackages
Value:
[ "/nix/store/s9lv238g70xcjk4bm0vxzhy89xi05d60-xterm-330" ...

This lists a lot of information about the environment.systemPackages option. This includes its current value, its default value, which modules add values to it, etc.

Unfortunately, this output is pretty long. We can clean it up a little bit with the following commands. This isn't perfect, but it will do in a pinch:

$ nixos-option environment.systemPackages | head -2 | tail -1 | \
    sed -e 's/ /\n/g' | cut -d- -f2- | sort | uniq

Listing everything installed from /etc/nixos/configuration.nix

The problem with the above command is that it doesn't list all transitive dependencies.

One way to list all transitive dependencies is with the nix-store command:

$ nix-store --query --requisites /run/current-system
/nix/store/27b2hylj1sv682iqijq3g7mbcfiy1rfw-glibc-2.26-131
/nix/store/003d91yhs72g120rkshp2d2hqsvi2368-gcc-6.4.0-lib
...

This lists every derivation built when doing nixos-rebuild switch. The output of this command is also a little hard to read, but we can clean it up:

$ nix-store --query --requisites /run/current-system | cut -d- -f2- | sort | uniq
acl-2.2.52
acl-2.2.52-bin
acl-2.2.52-man
acpi-1.7
adwaita-icon-theme-3.24.0
...

This command really lists every derivation installed, not just packages. For example, you should also see things like man pages, config files in /etc, etc.

Listing packages installed in a user profile

The above commands do not show the packages installed into a user profile with nix-env --install. For that, we will need to use the nix-env --query command.

The following shows how to list packages installed in your local user profile:

$ nix-env --query
highlight-1.0.0.1
jq-1.5
libreoffice-5.2.6.2

Running the same command as root will list packages installed in root's profile:

$ sudo nix-env --query
tcpdump-4.9.2

Conclusion

In order to list packages installed on your system, you need to remember the nixos-option, nix-store --query, and nix-env --query commands. It would be nice if there were a single command that would output all of this different information, but for now, there is not.

For now, if you are ever in doubt, you should first try nix-env --query. If that doesn't have what you're looking for, try pointing nix-store --query --requisites at /run/current-system.

tags: nixos