Unable to make nix shell work

I recently upgraded nix to the 2.4 release and tried to use the new experimental nix commands after modifying nix.conf.
However, I have been unable to make nix shell work properly. The command succeeds and opens a new shell, however that shell is almost identical to the one I started on. In particular, it does not contain the packages I requested…

I tried running the examples shown in the nix shell help page: opening a new shell does not seem to work, but executing a command in a single line does.

For example

nix shell nixpkgs#hello

breaks, while

nix shell nixpkgs#hello -c hello

works properly.

Is this intended? Am I misunderstanding something?

(by the way, before the upgrade nix run behaved in the same way, only with it being called nix run I was more confident that it was intended…)

1 Like

What shell are you using and what is the output before and after using nix shell of printenv SHLVL?

The shell is bash, and SHLVL is 1 before and 2 after

First form worked fine for me. There’s probably something in one of your shell rc/profile files (perhaps hard-setting the path too late, or returning early).

My bash profile only has the line added by the nix installer, my bashrc contains direnv and a few aliases, I suppose it may be something in the profile defaults from the distro (which are sourced at the start of bashrc).

Any idea on how to debug this?

When is too late for the path, anyway? Understandably the distro sets the path, but that is the first line executed in the bashrc…
Does the nix code for preparing the shell execute before or after the shell rc/profiles?

I’m not aware of anything more elegant than hacking an echo $PATH and a set -x (trace) in at the top of your rc and then running nix shell command.

I don’t have any special knowledge here and haven’t really been using the new-style commands much. nix/run.cc at 440d1ac718f9ca4a896942c45740feae3d9b1963 · NixOS/nix · GitHub makes me think hard-setting the PATH anywhere in your scripts will discard the paths nix shell appends.

do you get the same with nix develop

nix develop nixpkgs#hello gives me a shell with gcc, patchelf, tar, make and other stuff inside the PATH, which matches my expectations of the build environment

The difference, if I understand the nix code, is that nix shell sets the environment with setenv and then expects the PATH to propagate to the shell/command, while nix develop sets the environment by creating a temporary rc file which contains the global bashrc + the custom nix environment.

Why is it so different?

nix shell is meant to “enhance” or “augment” the current environment, while nix develop is meant to recreate an environment that comes close to the actual build environment.