14

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?

toraritte
  • 6,300
  • 3
  • 46
  • 67
Bruno Bieth
  • 2,317
  • 20
  • 31

2 Answers2

17

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.

rofrol
  • 14,438
  • 7
  • 79
  • 77
Robert Hensing
  • 6,708
  • 18
  • 23
  • Thanks. Your second paragraph isn't very clear to me: "built on top of the functional concepts of Nix" is also true for NixOS. "built inside the functional part of Nix" sounds fuzzy since on nixos.org Nix is defined as "The Purely Functional Package Manager". What I find confusing is that Nix being a "Package Manager" should provide a way to query packages, whether they were installed via nix-env or NixOS. – Bruno Bieth Dec 26 '17 at 11:11
  • I've edited my question with the last paragraph. I'm giving you +1 but not accepting this as I feel that there could be more explanations :) – Bruno Bieth Dec 26 '17 at 11:33
  • I have expanded the confusing paragraph and added some commands that may help you – Robert Hensing Dec 28 '17 at 15:05
  • Thanks again, that's helpful! – Bruno Bieth Dec 29 '17 at 08:34
0

to find all installed packages on nixos environment you can execute the command below:$ ls -l /nix/store/ total 19740 dr-xr-xr-x 4 root root 4096 Jan 1 1970 004ynvgrpg6dyaxfgd7hghc275qa8bmj-sharutils-4.15.2 dr-xr-xr-x 4 root root 4096 Jan 1 1970 03l71r03nyqvxnr65xd7976l4ng9i0fq-kerberos-env-1.15.2 dr-xr-xr-x 11 root root 4096 Jan 1 1970 05pdyq0yg6gc686mh6nn3n358v4jq1m5-source dr-xr-xr-x 5 root root 4096 Jan 1 1970 06daifrzcw4q45kdyakakys0rlr2lqna-nghttp2-1.24.0-dev dr-xr-xr-x 2 root root 4096 Jan 1 1970 07m5p4y1nb1db1h6czwisxin1nfl6dwh-unit-network-local-commands.service dr-xr-xr-x 3 root root 4096 Jan 1 1970 08f26dlqfvbbaysngb0cmg78ql5sxwq7-aws-sdk-cpp-1.3.22 -r-xr-xr-x 1 root root 183 Jan 1 1970 09iw4bj3g5509gb7yk0i2lyd34pml6sa-audit-disable dr-xr-xr-x 2 root root 4096 Jan 1 1970 09lnzcgzy0ph21db93gzqfss9ncb8jji-dbus-1 dr-xr-xr-x 3 root root 4096 Jan 1 1970 0a40bzy2f4s3bpsavr14zza20xdsjfiv-glibc-2.26-131-bin dr-xr-xr-x 5 root root 4096 Jan 1 1970 0by3320bykagqglisg2jsfrp1pcwq2iz-lzo-2.10 dr-xr-xr-x 2 root root 4096 Jan 1 1970 0da66x03kxcjwa5xbal1b9c9jw4qm7i3-unit-nginx.service ........................................ ........................................

  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jul 18 '23 at 21:16