Skip to content

Stop polluting the environment #15533

Open

Description

@NotTheDr01ds

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_DIRS and NU_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_CONVERSIONS isn't exported since it is a record, so it's probably okay to leave in the environment?

  • $env.config also isn't exported, but it's still odd living in the environment. It could probably be a "special local"

  • CMD_DURATION_MS and LAST_EXIT_CODE could (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, and PROCESS_PATH could 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_VERSION is already represented by the version command. 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 the version output?

  • NU_LIB_DIRS and NU_PLUGIN_DIRS already 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 $env versions anyway, but I still like having a non-const option.

Describe alternatives you've considered

No response

Additional context and details

No response

Activity

  1. added
    category:enhancement
    status:needs-triage
    on Apr 9, 2025
  2. changed the title [-]Avoid polluting the environment[/-] [+]Stop polluting the environment[/+] on Apr 9, 2025
  3. 132ikl commented on Apr 9, 2025

    @132ikl
    Member

    On NU_VERSION someone 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 despite version reporting 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 envvar

  4. fdncred commented on Apr 9, 2025

    @fdncred
    Contributor

    PR for NU_VERSION #10177 may lend some context.

  5. fdncred commented on Apr 9, 2025

    @fdncred
    Contributor

    I'd also like to see all nushell env vars/system vars start with NU_ to disambiguate

  6. Supremist commented on Apr 9, 2025

    @Supremist
    • ENV_CONVERSIONS isn't exported since it is a record, so it's probably okay to leave in the environment?
    • $env.config also isn't exported, but it's still odd living in the environment. It could probably be a "special local"

    I don't think it is a good practice to keep those in $env also. We need to think about what $env actually 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:

    • On Windows cmd /C 'set' - is imposible to parse properly if some varible contains a newline
    • On Unix env -0 - is easier to parse, but ENV_COVERSIONS still need to be applied manualy to all the values

    I can understand the reasoning behind not willing to add new global variables for config and ENV_CONVERSIONS. We can make ENV_CONVERSIONS a part of config or add a single special variable for them in order to not polute the global namespace.

  7. weirdan commented on Apr 9, 2025

    @weirdan
    Contributor

    ENV_CONVERSIONS isn't exported since it is a record, so it's probably okay to leave in the environment?

    This should probably go to the config?

    $env.config also isn't exported, but it's still odd living in the environment. It could probably be a "special local"

    Except with a different name, because $config is too generic and too widely used as a local variable (I think).

    NU_VERSION is already represented by the version command.

    I usually expect to find it in $nu.version, and am surprised every time it's not there.

  8. NotTheDr01ds commented on Apr 9, 2025

    @NotTheDr01ds
    ContributorAuthor

    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 nu launched a newer nu, then wouldn't $env.NU_VERSION still be for for the newer nu? No matter what, we reset $env.NU_VERSION at startup, so it's only going to be accurate for the $nu.current-exe. E.g.:

    ~/oldnu/nu-0.99.1-x86_64-unknown-linux-gnu/nu -n
    version | get version
    # => 0.99.1
    $env.NU_VERSION
    # => 0.99.1
    nu  # tip
    version | get version
    # => 0.103.1
    $env.NU_VERSION
    # => 0.103.1

    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:

    1. Presumably be a program (script or binary) that needs to make some type of decision based on which version of Nushell it's running in.
    2. Have to expect to be running inside Nushell, since (as noted in Add NU_VERSION environment variable on startup #10177) this isn't a standard thing.
    3. Is presumably not a Nushell script or source since version would 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:

    starship init nu
    # => ...
    # =>   PROMPT_COMMAND_RIGHT: {||
    # =>         (
    # =>             ^/home/ntd/.nix-profile/bin/starship prompt
    # =>                 --right
    # =>                 --cmd-duration $env.CMD_DURATION_MS
    # =>                 $"--status=($env.LAST_EXIT_CODE)"
    # =>                 --terminal-width (term size).columns
    # =>         )
    # =>     }

    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:

    --shell-version (version | get version)
  9. NotTheDr01ds commented on Apr 9, 2025

    @NotTheDr01ds
    ContributorAuthor

    From @weirdan:

    ENV_CONVERSIONS isn't exported since it is a record, so it's probably okay to leave in the environment?

    This should probably go to the config?

    And @Supremist:

    We can make ENV_CONVERSIONS a part of config

    I agree.

  10. added
    A:environment
    on Apr 10, 2025
  11. weirdan commented on Apr 10, 2025

    @weirdan
    Contributor

    NU_VERSION is already represented by the version command. Is there really going to be a subprocess that wants to specifically check which Nushell version called it?

    FWIW, it's already being used in the wild: https://docs.astral.sh/uv/configuration/environment/#nu_version

  12. NotTheDr01ds commented on Apr 11, 2025

    @NotTheDr01ds
    ContributorAuthor

    FWIW, it's already being used in the wild: https://docs.astral.sh/uv/configuration/environment/#nu_version

    Interesting. That's probably a really bad idea since it would fail if the user ran a Bash script inside Nushell. uv would 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 nu or nu.exe.

  13. vspefs commented on Dec 21, 2025

    @vspefs

    $env.CMD_DURATION_MS and $env.LAST_EXIT_CODE hurts 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A:environmentcategory:enhancementstatus:needs-triage

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions