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

sudo nix-channel --list
nixos nixos-22.11 release nixos-22.11.656.52e3e80afff
nixos-hardware https://github.com/NixOS/nixos-hardware/archive/master.tar.gz

nix-shell -p nix-info --run "nix-info -m" --show-trace 
error: file 'nixpkgs' was not found in the Nix search path (add it using $NIX_PATH or -I)

       at «string»:1:25:

            1| {...}@args: with import <nixpkgs> args; (pkgs.runCommandCC or pkgs.runCommand) "shell" { buildInputs = [ (nix-info) ]; } ""
             |                         ^

       … while realising the context of a path

       at «string»:1:18:

            1| {...}@args: with import <nixpkgs> args; (pkgs.runCommandCC or pkgs.runCommand) "shell" { buildInputs = [ (nix-info) ]; } ""
             |                  ^

       … while evaluating anonymous lambda

       at «string»:1:1:

            1| {...}@args: with import <nixpkgs> args; (pkgs.runCommandCC or pkgs.runCommand) "shell" { buildInputs = [ (nix-info) ]; } ""
             | ^

       … from call site

how to analyze and debug this issue ?

Sounds like your NIX_PATH environment variable doesn’t have anything useful in it.

/home/usera/.nix-defexpr/channels:nixos-config=/home/usera/configuration/configuration.nix

how can I validate that all “needed” is in it?

nixpkgs=/nix/var/nix/profiles/per-user/root/channels/nixos
is missing after nixos rebuild but I’m not aware of changing $NIX_PATH in any way. How to figure out what is making the manipulation?

When do you see the error from the subject?

Does it appear when you use nix as the user? Or does it appear when you run some commands with sudo?

Though yeah, I’d expect the nixpkgs=/nix/var/nix/profiles/per-user/root/channels/nixos line to be there in general.

Though when you say “you are not aware”, did you check your config anyway with a quick git grep or similar for nixPath and NIX_PATH?

nix = {
        nixPath = [
            "nixos-config=${pathNixosConfig}"
            ];

nixPath looks to be not accumulative like other config elements but overwriting … ?


How to keep old/existing nixpkgs variable with the nixPath config?

As you set it at all, the default will be dropped.

Then only the user related location is probably added by your users shell init scripts.

You need to reconstruct the default manually.

the unexpected behavior is not documented, is it?
https://search.nixos.org/options?channel=22.11&show=nix.nixPath&from=0&size=50&sort=relevance&type=packages&query=nixpath

Which unexpected behaviour?

Expectation is: you set an option to a value, then the default will not be used or merged.

This behaviour is inherint to the module system. If it would behave differently, you wouldn’t be able to use an enable-option, as its default false would always conflict with the manually set true.

for sure, you can see all character only as one string - then it would be ok, if this string is totally replaced
but if you look at the path/variables in the string - as user you only expect the variable to be modified which you have (explicitly) declared …
(some logic e.g. to check for the nixpkgs variable could make sense if that variables is always mandatory - else error )

There is only one default value. This default value is a single list.

Within that list, there are strings, which are opaque to the module system. The only way it was able to merge was by appending or prepending.

Though as your value and the default have different priorities, no merging happens at all, but your manually set value has a lower priority value and therefore replaces the default which has a high priority value.

This is documented in the NixOS manual.