Nix-store -q : return error

Hi All

I am reading NixPills , and trying commands.

In chapter :3.5. Querying the store

nix-store -q --references which psql
error: path ‘/home/postgres/.nix-profile/bin/psql’ is not in the Nix store

postgresql is installed :

[postgres@nixos-1903:~]$ which psql
/home/postgres/.nix-profile/bin/psql

But if i launch nix-shell with a reference at postgresql_10 the command nix-store works.

[postgres@nixos-1903:~]$ nix-shell -p postgresql_10

[nix-shell:~]$ nix-store -q --references which psql
/nix/store/0l3kl1z6zsh3x8d4372x2c1kzsdxnj86-tzdata-2019a
/nix/store/681354n3k44r8z90m35hm8945vsp95h1-glibc-2.27
/nix/store/cinw572b38aln37glr0zb8lxwrgaffl4-bash-4.4-p23

the command seem don’t work with .nix-profile …

.nix-profile → /nix/var/nix/profiles/per-user/postgres/profile

Thank.
Pierre

nix-store -q expects a path in the /nix/store. As you can see, when you installed a program in your profile, which returns the path to your symlinked profile.
In the nix-shell the direct path to the /nix/store is put into your PATH variable.

So you should resolve psql to its location in the /nix/store:

nix-store -q --references $(readlink -f $(which psql))
2 Likes

Hello John,

Thank you for your clear explanation.

Pierre

If you just want it for an arbitrary package which is not installed, you can also do

nix-store -q --references $(nix-build --no-out-link '<nixpkgs>' -A postgresql_10)

Please mark an answer as the solution when your question is answered :slight_smile:

1 Like

FWIW I actually tested on my machine and nix-store -q --references (which fish) gives me the same output as nix-store -q --references $(readlink $(which fish)). And the manpage for nix-store tells me that the paths may be symlinks from outside the nix store into it (and after testing it appears to support multiple levels of symlink indirection).

Does NixOS 19.03 use an older version of Nix that doesn’t have this behavior?

nix-store -q --references (which fish) gives me the same output as nix-store -q --references $(readlink $(which fish))

I can confirm.

If you just want it for an arbitrary package which is not installed, you can also do

nix-store -q --references $(nix-build --no-out-link '<nixpkgs>' -A postgresql_10)

However that builds the package, right? I’m looking for a solution that doesn’t build the package in the first place. I’ve tried only retrieving the derivation, but that – as far as I can tell – only resolves the input derivations, but not actual packages.

nix-store -q --references $(nix-instantiate '<nixpkgs>' -A hello)

Is there some way to get the identical output as with a built package, only based on the derivation?

As far as I understand - no, since the final set of run-time dependencies depends on which store paths occur in the build of a derivation. You can see this by e.g. adding a superfluous dependency to buildInputs of the hello derivation. Say that you add zlib to buildInputs, it will not be a runtime dependency, the references will still be something like:

$ nix-store -q --references result
/nix/store/iykxb0bmfjmi7s53kfg6pjbfpd8jmza6-glibc-2.27
/nix/store/cwqm1sjhds50r3yl4wy46zsyicycghsy-hello-2.10

Since the zlib store path will not occur anywhere in /nix/store/cwqm1sjhds50r3yl4wy46zsyicycghsy-hello-2.10.

tl;dr: building the derivation is necessary to find the actual run-time dependencies.

1 Like

Except that if it’s in the binary cache, the dependency information is already available from there:

$ nix-store --store https://cache.nixos.org -q --references $(nix eval --raw nixpkgs.hello)
/nix/store/681354n3k44r8z90m35hm8945vsp95h1-glibc-2.27
/nix/store/83vqfmpq19g0rkgjf0sa319x919p0vvg-hello-2.10

(nix eval --raw is currently the most convenient way to get the output path of a derivation without building it, but may break in the future because the nix command is still considered unstable.)

4 Likes