NIX_PATH is not recognized

I have the following config in order not to use nix channels in NixOS:

 nix.channel.enable = false;
 nix.nixPath = [
    "nixpkgs=${pkgs.path}"
 ];

When I run echo $NIX_PATH it outputs
nixpkgs=/nix/store/2dmmmy6plg4sczhspsa23zkzz7in0nmh-11zbgb8j7wnnccbbjcq0q556h28g7p4r-source so everything should work properly (or at least this is what I thought).
In spite of this, every time i run any nix command like nix-shell it gives me:

error: file 'nixpkgs' was not found in the Nix search path (add it using $NIX_PATH or -I)

The strange things is that it goes away when i run something like nix-shell -I $NIX_PATH, what’s wrong with this approach?
Thank you in advance

1 Like

I feel like I ran into this too and nix.channel.enable = true (not terribly intuitive) was needed and I needed to also add and update a channel. Worth a shot? I should note I’m on a flake based system so I had to pass inputs and all that jazz.

It looks like a bug tho (since this is how it’s implemented), should I report it to nixpkgs?.

@GiulioCocconi that only is for the default value, it should still be overwritten if you set nixPath
The reason nix.channel.enable = true, is required is this https://github.com/NixOS/nixpkgs/blob/b8dd8be3c790215716e7c12b247f45ca525867e2/nixos/modules/config/nix-channel.nix#L94-L96
You could manually copy this over and then you can set it false.

Not sure if the old nix commands rely on the channel files to be available though, so I personally just leave it enabled.

Even copying it’s still not working (NIX_PATH env var is defined correctly as I firstly said)

@GiulioCocconi

It looks like a bug tho (since this is how it’s implemented), should I report it to nixpkgs?.

This is a known issue. There appear to be at least four issues open for this in nix already (see nix issues 8784, 8890, 8902, and 9574). That last issue mentions at least two PRs to address this issue, one of which has been open since February 2023, the other since September 2023.

I haven’t read through all discussion, but it looks like the root of the issue is lack of clarity on what the interaction between all the various ways to set nix paths should be (there’s at least the environment variable NIX_PATH, the nix.conf option nix-path, the command line parameter --nix-path, and the command line parameter -I). Draft PR 9066 has a table in its description which looks like it has all the ways this option can be set, so hopefully people are making progress on fixing this issue.

In the meantime, if your only concern is your ability to use lookup paths like <nixpkgs> (or commands which use this lookup path, like nix-shell) you should be able to work around the issue by setting the following option in your configuration.nix (in addition to what you listed in your original post):

nix.settings.nix-path = config.nix.nixPath;

Just for your information, the problem is that nix determines how to resolve lookup paths (like <nixpkgs>) by looking at least the following sources, from highest to lowest priority:

  1. The value of nix-path in nix.conf.
  2. The value of NIX_PATH.
  3. A hardcoded default, equivalent to NIX_PATH=~/.nix-defexpr/channels:nixpkgs=/nix/var/nix/profiles/per-user/root/channels/nixpkgs:/nix/var/nix/profiles/per-user/root/channels.

Note the command line parameter -I adds to this, which is why your nix-shell -I $NIX_PATH invocation works.

2 Likes

Yes, this is a bug and we don’t have tests to be sure when it’s fixed. The table in #9066 is the first step towards reasonable test coverage of all possible combinations.

I took responsibility for it, but I’m very time-constrained at the moment and have a long queue of stuff to work off. Sorry, but unless someone picks it up and goes faster (ideally piecemeal, because the overall situation in the code is quite hairy in my opinion), it’s not worth holding your breath.

1 Like

Maybe we should include it in the error messages?

1 Like