How does Nix finds <nixpkgs> when NIX_PATH is empty?

I know I can build a package with nix-build like so:

nix-build --no-out-link -E 'with import <nixpkgs> { }; go'

This will build the go package and output its store path.

I also understand that is a function that should search for nixpkgs channel based on the NIX_PATH variable. If I run nix-instantiate --parse -E '<nixpkgs>' I will get the following output:

(__findFile __nixPath "nixpkgs")

so I guess it’s trying to find a file inside NIX_PATH.

However, on my local machine NIX_PATH is empty. If I run a echo $NIX_PATH I get an empty result.

So my question is, how come nix-build --no-out-link -E 'with import <nixpkgs> { }; go' command works even though my NIX_PATH is empty?

2 Likes

As far as I remember there are some hardcoded locations that are used as fallback when the NIX_PATH is empty.

Here you can see how it is determined exactly.

2 Likes

This information should be in the manual on NIX_PATH. @zuzuleinen would you care to make a pull request adding your insight?

I would love to do it, but I still haven’t found the answer to my question. I’m still struggling to understand the code NoobZ pointed me to

If I understand the code correctly, it sets the default to the equivalent of NIX_PATH="$HOME/.nix-defexpr/channels:nixpkgs=/nix/var/nix/profiles/per-user/root/channels/nixpkgs:/nix/var/nix/profiles/per-user/root/channels".

@zuzuleinen so your addition could amount to something like:

If NIX_PATH is not set or empty, Nix will look up the following locations for a revision of nixpkgs:

  1. $HOME/.nix-defexpr/channels
  2. /nix/var/nix/profiles/per-user/root/channels/nixpkgs
  3. /nix/var/nix/profiles/per-user/root/channels.
1 Like

Thanks! will create a PR manual: update docs when NIX_PATH is empty by zuzuleinen · Pull Request #6865 · NixOS/nix · GitHub

2 Likes