Add option to "nix shell" command to set env variables
Is your feature request related to a problem? Please describe. From the "nix shell" manpages, we have the capability to alter the env variables of upcoming new shell via various options (--unset, --ignore-environment, --keep). That's fine if we want to narrow the environment variables, but what if we want to set/expand it ?
Describe the solution you'd like An additional option to set an env variable in the created shell, for example a "--set var=value" option . This would be usefull when nix shell is executed from a script shebang where the workaround described below doesn't work.
Describe alternatives you've considered If not executed via a script shebang, we can set the env var while running the command:
$ VAR1=value1 VAR2=value2 nix shell nixpkgs#tree $ which tree; echo $VAR1 /nix/store/y9x314ap02i1s01ggkh4nmm54ks2ly5d-tree-2.1.1/bin/tree value1
However this doesn't work vai script shebang.
Additional context If you have other workarounds or if I missed an already existing option, please notify me,
Priorities Due to the available workaround, it reduces the criticity of this request. However, such capability might be a good thing to improve Nix shebang usage
Add :+1: to issues you find important.
You could nix run a script that performs "nix shell" functions.
--unset, --ignore-environment, --keep
Not sure if these should have been a Nix responsibility.
You could
nix runa script that performs "nix shell" functions.--unset, --ignore-environment, --keep
Not sure if these should have been a Nix responsibility.
Thanks for your answer. The "nix run" might not help but you're right, I could also wrap into scripts to set the variables before calling the "nix shell".
My guess was that if the Nix commands allow to alter the env variables by undeclaring some existing vars, Nix command should also allow modify/declare some new variables. But from your second point, you're even wondering if it should be nix responsability to alter those env variables, so I would say to at least keep what already exist
Team discussion:
- This would be a useful feature. It can be implemented in
MixEnvironment. Probablynix runshould inherit fromMixEnvironmentas well, that way all environment-related flags would be available innix runas well.
Not sure if these should have been a Nix responsibility.
In retrospect maybe, but I think it's fine to add it where we have --unset etc.
This issue has been mentioned on NixOS Discourse. There might be relevant details there:
https://discourse.nixos.org/t/2024-06-19-nix-team-meeting-minutes-154/47265/1