Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nix-shell fails to parse NIX_PATH #8890

Open
farnoy opened this issue Aug 31, 2023 · 16 comments
Open

nix-shell fails to parse NIX_PATH #8890

farnoy opened this issue Aug 31, 2023 · 16 comments
Labels

Comments

@farnoy
Copy link
Member

farnoy commented Aug 31, 2023

Describe the bug

nix-shell isn't able to understand the NIX_PATH env var to locate <nixpkgs>.

Steps To Reproduce

  1. $ NIX_PATH="nixpkgs=/nix/store/066hr2pmppfzfsn1d6lb9485hniz5p53-source" nix-shell -p nix-info --run "nix-info -m"

Fails with:

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) ]; } ""
             |                         ^
(use '--show-trace' to show detailed location information)

Expected behavior

I expected it to work the same way as when executed this way:
nix-shell -I nixpkgs=/nix/store/066hr2pmppfzfsn1d6lb9485hniz5p53-source -p nix-info --run "nix-info -m"

nix-env --version output
nix-env (Nix) 2.13.5

Additional context

I think I only started seeing this after setting nix.channel.enable = false; in my NixOS config, but I don't think it should be relevant in any way since I'm specifying NIX_PATH explicitly in the examples above.

EDIT: My nix.conf includes nix-path = (blank) as a consequence of my NixOS setup. See comments below.

Priorities

Add 👍 to issues you find important.

@farnoy farnoy added the bug label Aug 31, 2023
@colonelpanic8
Copy link

I've been seeing the exact same thing.

Whats pretty interesting is that if you specify the path explicitly it works:

imalison in 🌐 ryzen-shine in nix on  master via C v12.3.0-gcc
➜ export NIX_PATH=nixpkgs=/nix/store/wr9zk0l6xw1bi0zjrrsj998b5wgykyz5-source
imalison in 🌐 ryzen-shine in nix on  master via C v12.3.0-gcc
➜ ./result/bin/nix eval --expr '<nixpkgs>' --impure
error:
       … while calling the 'findFile' builtin

         at «string»:1:1:

            1| <nixpkgs>
             | ^

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

       at «none»:0: (source not available)
imalison in 🌐 ryzen-shine in nix on  master via C v12.3.0-gcc
➜ ./result/bin/nix eval -I "$NIX_PATH" --expr '<nixpkgs>' --impure
/nix/store/wr9zk0l6xw1bi0zjrrsj998b5wgykyz5-sourcee 

@colonelpanic8
Copy link

@roberth @Ericson2314 I see that you recently refactored the searchPath stuff in nix. Do you have any idea whats going on here?

Also, to add to all the strangeness of this:

when I build from source in a dev shell according to the instructions I found here (https://nixos.org/manual/nix/unstable/contributing/hacking.html)

I get a DIFFERENT RESULT than I do when using the binary produced by nix build:

➜ ./outputs/out/bin/nix eval --expr '<nixpkgs>' --extra-experimental-features nix-command --impure
/nix/store/wr9zk0l6xw1bi0zjrrsj998b5wgykyz5-source
nix on  master [!?] via C v11.1.0-clang via ❄️  impure (nix-2.18.0pre20230901_dirty-env) 
➜ ./result/bin/nix eval --expr '<nixpkgs>' --extra-experimental-features nix-command --impure  
error:
       … while calling the 'findFile' builtin

         at «string»:1:1:

            1| <nixpkgs>
             | ^

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

       at «none»:0: (source not available)

I'm kind of at a loss, and I absolutely despise working in C++, so I don't want to start trying to debug this.

@colonelpanic8
Copy link

Ah, I see that @roberth already reported this issue as #8784

@colonelpanic8
Copy link

colonelpanic8 commented Sep 3, 2023

So my nix.conf looks like this:

# WARNING: this file is generated from the nix.* options in
# your NixOS configuration, typically
# /etc/nixos/configuration.nix.  Do not edit it!
allowed-users = *
auto-optimise-store = false
builders = 
cores = 0
extra-sandbox-paths = 
keep-derivations = true
keep-outputs = true
max-jobs = auto
nix-path = 
require-sigs = true
sandbox = true
sandbox-fallback = false
substituters = https://cache.nixos.org/
system-features = nixos-test benchmark big-parallel kvm
trusted-substituters = 
experimental-features = nix-command flakes

I suspect this is why I am seeing this issue, but the thing is that im pretty sure that in --impure mode, the environment variable should be taking precedence and its not. this is weird.

@colonelpanic8
Copy link

This appears to be where the environment variable is read:

auto var = getEnv("NIX_PATH");

but from what I can tell, this code does not seem to get executed somehow which seems really strange.

colonelpanic8 added a commit to colonelpanic8/dotfiles that referenced this issue Sep 3, 2023
@colonelpanic8
Copy link

I think I only started seeing this after setting nix.channel.enable = false;

For anyone that is seeing this after changing that in their config, you probably want something like what I did here:

colonelpanic8/dotfiles@2f02f72

This is because nixpkgs seems to somewhat bizzarely do this:

https://github.com/NixOS/nixpkgs/blob/48516a891d020801bc5304375739d2604400c741/nixos/modules/config/nix-channel.nix#L98

which ignores the cfg.nixPath value that is part of this same configuration.

@colonelpanic8
Copy link

Here are some sections of documentation that would indicate that this is a genuine bug:

  • --find-file
         Look up the given files in Nix’s search path (as specified  by  the  NIX_PATH  environment  variable).  If
         found,  print  the  corresponding  absolute  paths  on standard output. For instance, if NIX_PATH is nixp‐
         kgs=/home/alice/nixpkgs, then nix-instantiate --find-file nixpkgs/default.nix will print /home/alice/nixp‐
         kgs/default.nix.
         ```
         
 • NIX_PATH
     A colon-separated list of directories used to look up the location of Nix expressions using paths enclosed
     in angle brackets (i.e., <path>), e.g. /home/eelco/Dev:/etc/nixos. It can be extended using the -I option.

     If NIX_PATH is not set at all, Nix will fall back to the following list in impure and unrestricted evalua‐
     tion mode:

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

     If  NIX_PATH  is set to an empty string, resolving search paths will always fail.  For example, attempting
     to use <nixpkgs> will produce:

   error: file 'nixpkgs' was not found in the Nix search path
   ```

What should happen is arguably ambigious. In particular in the case where NIX_PATH is explicitly set to the empty string (should we clobber whatever is in nix settings in that case?

@farnoy
Copy link
Member Author

farnoy commented Sep 4, 2023

Thanks for investigating this and for providing a workaround.

It is true that I have nix-path = in my nix.conf and that likely contributes to this issue. I did not mention this in the original post because I did not know it existed before today.

@colonelpanic8
Copy link

colonelpanic8 commented Sep 4, 2023

Thanks for investigating this and for providing a workaround.

It is true that I have nix-path = in my nix.conf and that likely contributes to this issue. I did not mention this in the original post because I did not know it existed before today.

This gets set automatically when you disable channels. Still, imo nixs behavior here is wrong and inconsistent with it's documentation, hence the pull request that I made.

#8902

@roberth
Copy link
Member

roberth commented Sep 4, 2023

This gets set automatically when you disable channels.

I was unaware of this problem when I wrote that option, nix.channel.enable, in NixOS. I agree that this should be fixed here and not in NixOS, because otherwise nix-path would still have its default value, which to locations where stale channel data may reside.

@colonelpanic8
Copy link

I was unaware of this problem when I wrote that option, nix.channel.enable, in NixOS. I agree that this should be fixed here and not in NixOS, because otherwise nix-path would still have its default value, which to locations where stale channel data may reside.

Yes, I largely agree BUT, I do think that there is still something a bit strange about the channel disable configuration:

see this comment for more details: NixOS/nixpkgs#242098 (comment)

@alexfmpe
Copy link
Member

alexfmpe commented Nov 4, 2023

Think I'm also running into this.

I pin down my nixpkgs rather than use channels:

nix.channel.enable = false;
nix.nixPath = [("nixpkgs=" + /path/to/nixpkgs-pin)]

meaning I end up with NIX_PATH as nixpkgs=/nix/store/hash-of-nixpkgs-source

$NIX_PATH is seemingly being ignored by nix-shell, etc as $ nix-shell -I $(echo $NIX_PATH) works, but $ nix-shell errors from not finding <nixpkgs> which Nix tells me to add (using $NIX_PATH or -I), but it's clearly not accepting the former, only the later:

$ echo $NIX_PATH
nixpkgs=/nix/store/4jfc6vrkmq7z5pb651jh5b4kra5f1kwp-source
 
$ ls /nix/store/4jfc6vrkmq7z5pb651jh5b4kra5f1kwp-source
CONTRIBUTING.md  COPYING  default.nix  doc  flake.nix  lib  maintainers  nixos  pkgs  README.md
 
$ nix-shell -I $(echo $NIX_PATH)
 
[nix-shell]$ exit
 
$ nix-shell
error:
       … while calling the 'import' builtin
 
         at «string»:1:2:
 
            1| (import <nixpkgs> {}).bashInteractive
             |  ^
 
       … while calling the 'findFile' builtin
 
         at «string»:1:9:
 
            1| (import <nixpkgs> {}).bashInteractive
             |         ^
 
       error: file 'nixpkgs' was not found in the Nix search path (add it using $NIX_PATH or -I)
 
       at «none»:0: (source not available)
will use bash from your environment
 
[nix-shell]$ 

@mrtnvgr
Copy link

mrtnvgr commented Nov 16, 2023

I'm having this issue too, bump

@dukc
Copy link

dukc commented Nov 25, 2023

For anyone that is seeing this after changing that in their config, you probably want something like what I did here:

Just hit this issue, with the same reason - doing away with Nix channels. This workaround seems to be working to some extent - thanks. However, now the colon separation of nix paths doesn't apparently work, probably I'm missing something.

@dukc
Copy link

dukc commented Nov 25, 2023

However, now the colon separation of nix paths doesn't apparently work, probably I'm missing something.

Ohh never mind, it was just the nix.conf and -I nixpath flags each requiring a different syntax for multiple file paths. Space instead of a colon for the conf file, multiple -I arguments for the nix command invocations.

@nixos-discourse
Copy link

This issue has been mentioned on NixOS Discourse. There might be relevant details there:

https://discourse.nixos.org/t/nix-path-is-not-recognized/38404/6

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants