NixOS / nix

Nix, the purely functional package manager
https://nixos.org/
GNU Lesser General Public License v2.1
12.77k stars 1.52k forks source link

Inconsistent behavior when appending a path #6573

Open Atry opened 2 years ago

Atry commented 2 years ago

Describe the bug

When a path is appended, sometimes the path is converted to a nix store string while sometimes it is converted to the string of the original path. The behavior is inconsistent and confusing.

Steps To Reproduce

$ nix repl
Welcome to Nix 2.8.0. Type :? for help.

nix-repl> :lf git+https://github.com/NixOS/nixos-homepage.git
Added 14 variables.

nix-repl> inputs.nixpkgs.outPath + /pkgs/build-support/mkshell/default.nix  
error (ignored): error: end of string reached
error: getting status of '/pkgs/build-support/mkshell/default.nix': No such file or directory
[1 copied (110.7 MiB), 19.3 MiB DL]
nix-repl> inputs.nixpkgs + /pkgs/build-support/mkshell/default.nix
"/nix/store/jvjjrzjc5bgvwc5nzc1pqxn2wn35vx97-source/pkgs/build-support/mkshell/default.nix"
nix-repl> inputs.nixpkgs + /pkgs/build-support + /mkshell/default.nix
error (ignored): error: end of string reached
error: getting status of '/mkshell/default.nix': No such file or directory
[1 copied (110.7 MiB), 19.3 MiB DL]

Expected behavior

All the following expression should return the same value

nix-env --version output

nix-env (Nix) 2.8.0

Atry commented 2 years ago

The behavior could be surprising sometimes:

nix-repl> pkgs = import <nixpkgs> {}

nix-repl> pkgs.bash.outPath + /bin/bash
"/nix/store/s7r5cnjj4zy41g2nql3vf2ivr09wfm5p-bash-5.1-p16/nix/store/4jd7mslcg2c1w95z29crzkvrglf0qf7a-bash"

nix-repl> pkgs.bash + /bin/bash  
"/nix/store/s7r5cnjj4zy41g2nql3vf2ivr09wfm5p-bash-5.1-p16/bin/bash"
Atry commented 2 years ago

Also path interpolation behavior is inconsistent, too

nix-repl> :lf git+https://github.com/NixOS/nixos-homepage.git
Added 14 variables.

nix-repl> /${inputs.nixpkgs}
error: a string that refers to a store path cannot be appended to a path

       at «string»:1:1:

            1| /${inputs.nixpkgs}
             | ^

nix-repl> /${<nixpkgs>}
/nix/var/nix/profiles/per-user/root/channels/nixpkgs
con-f-use commented 5 months ago

Your inputs.nixpkgs.outPath + /pkgs/build-support/mkshell/default.nix example doesn't make sense to me because it's appending an absolute path to another absolute path. Which is not a thing any language worth its salt would allow.

The second example inputs.nixpkgs + /pkgs/build-support/mkshell/default.nix seemed inconsistent to me at first glace, until I realize inputs.nixpkgs is not a path. But prepending a string to an absolute path, is also something a sane language should error on.