nix icon indicating copy to clipboard operation
nix copied to clipboard

nix develop impure by default, contrary to documentation

Open charles-dyfis-net opened this issue 4 years ago • 5 comments

Describe the bug

The man page spawned by nix develop --help documents an option --impure to allow access to environment contents not listed in a flake's devShell. No --pure is available.

However, the default behavior is impure: nix run github:NixOS/nix# develop leaves $HOME/.nix-profile/bin in the PATH, meaning the default behavior is --impure.

Steps To Reproduce

As of this writing, nix run github:NixOS/nix# -- --version emits nix (Nix) 2.9.0pre20220422_7f814d6

  1. Using any flake-enabled version of Nix, in an environment that already has $HOME/.nix-profile/bin in the PATH, run:
    testCmd() { printf '%s\n' "${PATH//:/$'\n'}"; }
    nix run github:NixOS/nix# -- develop github:NixOS/nix# -c bash -c "$(declare -f testCmd); testCmd"
    
    Observe that the final lines of output include $HOME/.nix-profile/bin, /nix/var/nix/profiles/default/bin, and /run/current-system/sw/bin.
  2. Compare to nix-shell --pure:
    testCmd() { printf '%s\n' "${PATH//:/$'\n'}"; }
    nix-shell --pure --run "$(declare -f testCmd); testCmd"
    
    ...properly omits system- and user-specific paths.

Expected behavior

Either behavior should be pure by default (user-account and system-provided locations should not be in the PATH inside a nix develop shell), or a --pure flag should be provided and --impure should be documented as off-by-default.

nix-env --version output

Observed between Nix 2.4 and 2.9.0pre20220422, inclusive. Picking one of the (many) revisions against which this has been observed:

nix-env (Nix) 2.5.0pre20211206_d1aaa7e

charles-dyfis-net avatar Apr 23 '22 16:04 charles-dyfis-net

Thanks for the report. The situation can be confusing. Sadly it is difficult to provide a completely satisfactory solution to this problem.

First I’ll directly address your points and hopefully clarify a few things. The purity option you are looking for is “-i”/“—ignore-environment”. The “—impure” option you refer to is in the evaluation options section, meaning that it impacts how the Nix code is evaluated, not necessarily about the purity/impurity of the shell you are given. The use of the letter i here is unfortunate, perhaps that needs to be improved. Perhaps we change the wording to use “pure-eval”/“impure-eval” to clarify this. Or better docs.

Next is the UX issue. There is an easy confusion between purity of eval and of the resulting shell. The default at the moment is an impure shell, because it seems that is what people want more often. They want their other tools to be in PATH ( “why can’t I use git, ls, or mkdir?!” ) and so some exceptions are made. Another example is HOME. This is in line with the idea that they are using “nix develop” to develop, not debug a build. Then there are other issues like people using other SHELL, or wanting to preserve tab-completion, and so on.

It should be better clarified what options are available since no single solution meets everyone’s use case, though we try to make the simplest commands meet most of them: (somewhat in order of pure to impure environment)

  1. nix build
  2. env -i bash --norc --noprofile ; nix develop -i
  3. nix develop -i
  4. nix develop
  5. nix shell (only adjusts PATH)
  6. nix shell nixpkgs#bashInteractive --command bash ; . <(nix print-dev-env)
  7. . <(nix print-dev-env)

Then when you bring in other complications like people wanting to use direnv/lorri/editors/IDEs or that their SHELL configuration/bashrc is still respected and that each SHELL has its own quirks; well, it is not always clear what the best option is.

tomberek avatar Apr 23 '22 19:04 tomberek

Per above, this is not in fact a bug; closing ticket. Apologies for the noise.

charles-dyfis-net avatar Apr 23 '22 20:04 charles-dyfis-net

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

https://discourse.nixos.org/t/nix-develop-fails-with-command-bash-norc/31896/7

nixos-discourse avatar Aug 19 '23 03:08 nixos-discourse

The UX here is absolutely horrifying... There is a ton of legacy documentation talking about nix shell --pure. And then you introduce nix develop as a replacement for it. And when you search for something about purity, you find --impure and incorrectly conclude that nix develop is pure by default. But it isn't.

I understand that nix is kind of niche, and flakes are technically experimental, but that is not an excuse for doing things this way, when there is already so much stuff built around the concept of pure/impure. Just make flakes and new nix CLI stable, god damn it. It's all really frustrating.

I want to love nix. But we need to get our s together with these things.

nikableh avatar Mar 30 '25 21:03 nikableh

@synalice You are right with your remarks. The '--pure' for nix develop is called --ignore-environment

--ignore-environment / -i Clear the entire environment (except those specified with --keep).

qknight avatar May 15 '25 16:05 qknight