Skip to content

Avoiding CMD_DURATION_MS and LAST_EXIT_CODE env vars #9276

Closed as duplicate

Description

@kennylevinsen

Related problem

nushell sets two environment variables for use with custom prompts:

  • CMD_DURATION_MS
  • LAST_EXIT_CODE

As a side-effect, this litters the environment for all programs run from nushell, unlike other shells where a local variable is used that prompt functions can check.

Describe the solution you'd like

Pass this information through either:

  1. Globally accessible non-environment variables.
  2. Provide a built-in command to obtain this information in PROMPT_* functions when/if needed.
  3. Provide this information as argument to the PROMPT_* functions.

I am leaning towards 2.

Describe alternatives you've considered

ENV_CONVERSIONS hacks to filter out the value crossed my mind, but the idea seemed ugly.

Additional context and details

No response

Activity

  1. added
    category:enhancement
    status:needs-triage
    on May 24, 2023
  2. fdncred commented on May 24, 2023

    @fdncred
    Contributor

    Or write your own custom env command like I have. You'd have to change it to filter out whatever you don't want.

    # get the environment details
    def "env details" [] {
      let e = ($env | reject config | transpose key value)
      $e | each { |r|
        let is_envc = ($r.key == ENV_CONVERSIONS)
        let is_closure = ($r.value | describe | str contains 'closure')
        let is_list = ($r.value | describe | str contains 'list')
        if $is_envc {
          echo [[key value]; 
            [($r.key) ($r.value | transpose key value | each { |ec| 
              let to_string = ($ec.value | get to_string | view source $in | nu-highlight)
              let from_string = ($ec.value | get from_string | view source $in | nu-highlight)
              echo ({'ENV_CONVERSIONS': {($ec.key): { 'to_string': ($to_string) 'from_string': ($from_string)}}})
            })]
          ]
        } else if $is_closure {
          let closure_value = (view source ($env | get $r.key) | nu-highlight)
          echo [[key value]; [($r.key) ($closure_value)]]
        } else if $is_list {
          let list_value = ($env | get $r.key | split row (char esep))
          echo [[key value]; [($r.key) ($list_value)]]
        } else {
          echo [[key value]; [($r.key) ($r.value)]]
        }
      }
    }
    
    def env [] { env details | flatten | table -e }
    
  3. kennylevinsen commented on May 24, 2023

    @kennylevinsen
    Author

    The intent would be to exclude these variables in the environment of spawned programs, which an env command would unfortunately not help with.

  4. fdncred commented on May 24, 2023

    @fdncred
    Contributor

    I wonder if hiding within a scope is a better way to do this. We'd have to play a bit though. The idea being, prior to launch do a hide or hide-env of whatever you don't want. I did some quick testing and couldn't get it to work either.

    image

  5. removed
    status:needs-triage
    on Jul 9, 2023
  6. NotTheDr01ds commented on Apr 2, 2025

    @NotTheDr01ds
    Contributor

    The following works, as long as I'm not running Tmux (and I'm not quite sure why Tmux is different yet):

    do {
      [ LAST_EXIT_CODE CMD_DURATION_MS ]
      | append ($env | columns | where $it starts-with NU_)
      | append ($env | columns | where $it starts-with PROMPT_ )
      | append ($env | columns | where $it starts-with TRANSIENT_ )
      | hide-env ...$in
      bash
    }

    But it does demonstrate just how many environment variables we are exporting (a.k.a. polluting) into the child process.

    I'm not sure there's any good justification for most of these to be environment variables. They aren't in other shells, so third-party apps like Starship just call itself using something like (copied from the init):

    starship prompt
      --cmd-duration $env.CMD_DURATION_MS
      $"--status=($env.LAST_EXIT_CODE)"
      --terminal-width (term size).columns

    Since they can't count on a shell having environment variables, they just get the value the easiest way and pass it as an argument. So (in theory) we could convert these to commands as mentioned in the original issue (item 2).

  7. added
    A:environment
    on Apr 3, 2025
  8. kronberger-droid commented on Nov 14, 2026

    @kronberger-droid
    Contributor

    Both variables are still exported to child processes of the REPL on 0.117.
    #15533 was opened from this issue and tracks them together with the prompt and NU_* variables.

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:enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions