Repository navigation
Description
Related problem
For clarity, this is not a "Save the rainforests" ecological post, despite the title ;-)
I've been meaning to write this up for a few days (since commenting on #9276), but some comments in #15170 pretty much warrant a more complete issue.
(Supremist said) Is there any reason to put ENV_CONVERSIONS and config into $env variable?
(@fdncred replied) I'm not sure. @NotTheDr01ds any thoughts?
So yes ... err, lots of thoughts :-)
Let's start with the purpose of the "environment". IMHO, it's for:
- Global system configuration
- And/or items that should be exported to child processes / inherited from parent processes.
When we start a subprocess in Nushell, the child inherits a lot of variables that just aren't useful:
CMD_DURATION_MS,LAST_EXIT_CODE(as originally mentioned in Avoiding CMD_DURATION_MS and LAST_EXIT_CODE env vars #9276)- Prompt stuff - Should probably be in Config, but no reason to export that I can think of. We specifically overwrite these at startup to make sure that we don't import a Bash
PROMPT_COMMAND, for instance, but most of these are (unnecessarily) inherited by Bash, PowerShell, or any other subprocess. NU_LIB_DIRSandNU_PLUGIN_DIRS(nice to have a non-const alternative still, but shouldn't be exported to subprocesses, IMHO) - Again, we overwrite these at startup so they can't be inherited).FILE_PWD,CURRENT_FILE,PROCESS_PATH: Bash and PowerShell (among others) have similar constructs, but they aren't exported since they aren't useful to child processes.- Even
NU_VERSION, which I think has been discussed in the past, but before my time.
Describe the solution you'd like
tl;dr Get these out of the environment so that child processes environment isn't unnecessarily polluted.
However, I really would prefer that we not just wholesale "move" these from being environment variables to local variables as I like having a clean local-scope as much as possible.
-
ENV_CONVERSIONSisn't exported since it is a record, so it's probably okay to leave in the environment? -
$env.configalso isn't exported, but it's still odd living in the environment. It could probably be a "special local" -
CMD_DURATION_MSandLAST_EXIT_CODEcould (probably) be replaced with commands instead of variables. Starship just passes this information into itself as an argument anyway, since it can't count on any other shell having that information in the environment. -
FILE_PWD,CURRENT_FILE, andPROCESS_PATHcould probably be local variables since they only need to survive in the current script/source/module scope. -
Prompt related stuff, as mentioned above, should probably go into config record.
-
NU_VERSIONis already represented by theversioncommand. Is there really going to be a subprocess that wants to specifically check which Nushell version called it? What is it going to do when called from a different shell? In what use case does it need to know? And if so, shouldn't it just pass that information via a flag using theversionoutput? -
NU_LIB_DIRSandNU_PLUGIN_DIRSalready exist in the local environment as consts, so it wouldn't be possible to move them as is. I know some folks want to do away with the$envversions anyway, but I still like having a non-const option.
Describe alternatives you've considered
No response
Additional context and details
No response
Activity
[-]Avoid polluting the environment[/-][+]Stop polluting the environment[/+]132ikl commentedon Apr 9, 2025
On
NU_VERSIONsomeone used that recently to verify that they had accidentally launched nushell 0.102 binary from a nushell 0.100 binary, causing job control to not work despiteversionreporting 0.102. This is an absurd set of edge cases and probably doesn't justify having an envvar still, but it is a reason to keep it as an envvarfdncred commentedon Apr 9, 2025
PR for
NU_VERSION#10177 may lend some context.fdncred commentedon Apr 9, 2025
I'd also like to see all nushell env vars/system vars start with
NU_to disambiguateSupremist commentedon Apr 9, 2025
I don't think it is a good practice to keep those in
$envalso. We need to think about what$envactually means in nushell. I really think it should have a single meaning: the representation of environment variables, which are inherited from parent and will be passed to child processes (except more strongly typed, that is a good thing). So the only reason to add something to it would be, if the child processes want to use that added variable. It should be documented and treated with care, almost like a form of public api.Also think about it this way: a reasonable use case for a neshell user is to get all the environment variables. That task is not really trivial right now. It may look something like this:
$env | transpose key value | filter { not ($in.key in [config, ENV_CONVERSIONS])}Or using external tools it is not preaty either:
cmd /C 'set'- is imposible to parse properly if some varible contains a newlineenv -0- is easier to parse, butENV_COVERSIONSstill need to be applied manualy to all the valuesI can understand the reasoning behind not willing to add new global variables for
configandENV_CONVERSIONS. We can makeENV_CONVERSIONSa part ofconfigor add a single special variable for them in order to not polute the global namespace.weirdan commentedon Apr 9, 2025
This should probably go to the config?
Except with a different name, because
$configis too generic and too widely used as a local variable (I think).I usually expect to find it in
$nu.version, and am surprised every time it's not there.NotTheDr01ds commentedon Apr 9, 2025
Of all of them, I struggled the most with
NU_VERSION. We could keep it, but do we really need it?@fdncred Thanks! I figured you would have the background handy on that one. And I figured it was something like that. I definitely appreciate the intent behind the decision and would probably have advocated for it to be an environment variable back then as well.
@132ikl Can you provide more details on that case? If an older
nulaunched a newernu, then wouldn't$env.NU_VERSIONstill be for for the newernu? No matter what, we reset$env.NU_VERSIONat startup, so it's only going to be accurate for the$nu.current-exe. E.g.:I just can't think of a way these could get out of sync?
I'm just struggling for a good use-case for it being an environment variable. AFAICT, the subprocess would:
versionwould work there.So, for instance, something like Starship or Zoxide.
The way that Starship would handle this, if it needed to, would probably be the same way it handles
LAST_EXIT_CODE. Rather than check the exported value, it passes it in as an argument:It has these flags because it runs in more shells than just Nushell, of course, so it just goes that route anyway. If it were checking the version, then it would do something like:
NotTheDr01ds commentedon Apr 9, 2025
From @weirdan:
And @Supremist:
I agree.
weirdan commentedon Apr 10, 2025
FWIW, it's already being used in the wild: https://docs.astral.sh/uv/configuration/environment/#nu_version
NotTheDr01ds commentedon Apr 11, 2025
Interesting. That's probably a really bad idea since it would fail if the user ran a Bash script inside Nushell.
uvwould still think the parent process was Nushell even though it was really Bash.I'm not quite sure what "difference" they are adapting for by detecting Nushell this way, but I would think they'd be better off checking if the parent process name is
nuornu.exe.vspefs commentedon Dec 21, 2025
$env.CMD_DURATION_MSand$env.LAST_EXIT_CODEhurts any caching related to env vars. For example, if a build system (correctly) considers env vars while caching a job, then under Nu it would almost never hit, because these 2 variables are almost always different.