Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't `nix-env -q` find my installed packages? (NixOS channels, profiles and packages)

Tags:

nixos

I recently installed NixOS and I ended up with 3 profiles:

  • bruno (a user profile),
  • default (used by root), and
  • system (used by NixOS).

I found it convenient to use a stable channel for the system profile and an unstable channel for me (bruno profile):

~> nix-channel --list
unstable https://nixos.org/channels/nixos-unstable
~> sudo nix-channel --list
nixos https://nixos.org/channels/nixos-17.09

I have then declaratively installed a few packages through /etc/nixos/configuration.nix:

environment.systemPackages = with pkgs; [
   firefox
   chromium
   htop
   # ...
];

And some imperatively: nix-env --install firefox.

Now listing my installed packages, I'd expect to also see the ones provided by the system profile (as they are available in my user profile):

~> htop --version
htop 2.0.2 - (C) 2004-2016 Hisham Muhammad
Released under the GNU GPL.

~> nix-env -q | grep htop
~> nix-env -q | grep firefox
firefox-57.0

Only Firefox is listed. Let's try using the root profile:

~> sudo nix-env -q | grep htop

Same thing, it's actually completely empty. Maybe using the system profile:

~> sudo nix-env -p /nix/var/nix/profiles/system -q

Still nothing.

Coming from traditional package managers (Debian, Red Hat), I find confusing that Nix being defined as "The Purely Functional Package Manager" does not seem to provide a tool to query packages universally - nix-env is mentioned throughout the manuals and feels like Debian apt's alter ego.

Is there such a tool, or is this a non-problem, that is, people are generally fine with not having a list of all packages present across profiles/environments?

like image 580
Bruno Bieth Avatar asked Dec 23 '17 15:12

Bruno Bieth


People also ask

Should you use nix-env?

Generally you should avoid using nix-env for package installation as it doesn't leverage the power of nix. vulcao: What's the difference between those installation methods? Is it like installing to /Applications vs ~/Applications on macOS?


1 Answers

nix-env -q will only report packages that are installed into imperative 'environments', like those created by nix-env -i.

nix-env is a tool for imperative package management that is a thin layer over the otherwise declarative and immutable Nix system. The profiles mechanism provides a means for mutability and nix-env creates manifest.nix in the profile to record the set of packages that are in the environment.

A NixOS system only uses the profiles but does not provide a manifest.nix file. This makes sense, because a NixOS system gets rebuilt from scratch every time, which makes it nicely declarative.

As a consequence nix-env can not query a NixOS profile.

So, nix-env -q queries your imperatively installed user packages. sudo nix-env -q shows those imperatively installed by root, which does not include your NixOS packages, because they are part of your system, declaratively. Pointing nix-env -q -p at your NixOS system gives an empty list, because it's not an environment created by nix-env.

The reason your user can use system commands is not because the system is part of the (Nix) environment in the user profile, but because your (UNIX) environment variables point to both profiles.

$ which firefox
/home/user/.nix-profile/bin/firefox
$ which cp
/run/current-system/sw/bin/cp
$ echo $PATH
[...]

To figure out what is installed on your system, you may run the following commands:

  • nix-env -q to figure out what is installed in an imperative user environment.
  • nixos-option environment.systemPackages to query the set of packages that will appear in /run/current-system/sw when your configuration is built and activated. To quote its documentation, "These packages are automatically available to all users."
  • nix-store -q --requisites /run/current-system ~/.nix-profile the combined closure of dependencies of the current system and your user profile
  • nix-store -q --references /run/current-system direct dependencies of the current system

See --query section or nix-store --help for more options.

like image 78
Robert Hensing Avatar answered Oct 11 '22 05:10

Robert Hensing