Skip to content

Add an apply API for Bash environment changes #52

Open

Description

@TyceHerrman

Problem

bash-env-nushell directly runs the separately installed bash-env-json executable and parses its JSON output into a Nushell record. That output includes changed values, null for unset variables, and changes to PATH or PWD. The current pattern:

bash-env ./environment.sh | load-env

does not fully translate the delta into Nushell semantics:

  • null becomes nothing instead of removing the variable.
  • Bash PATH is a string, while Nushell normally uses a list.
  • Nushell rejects assigning PWD through load-env; it must be applied with cd.

Proposal

bash-env ./environment.sh | bash-env apply

Keep bash-env unchanged for capture and inspection. bash-env apply would consume its record and:

  1. map null/nothing to hide-env --ignore-errors;
  2. convert PATH using Nushell's environment conversion, with the platform separator as fallback;
  3. apply PWD with cd;
  4. pass remaining values to load-env.

This likely requires an exported def --env. Accepting a record also lets users inspect or transform a delta before applying it.

Relationship to Nushell core

Nushell could improve its generic APIs:

Those changes could simplify the adapter, but bash-env-nushell can implement the mappings now. PWD will probably remain an adapter-level translation to cd.

The README already lists unsetting as future work.

Acceptance criteria

  • Existing bash-env output remains unchanged.
  • Unset variables are absent from $env.
  • PATH remains a Nushell path list.
  • PWD changes the current directory.
  • Tests cover all three cases.

Activity

  1. tesujimath commented on Jul 28, 2026

    @tesujimath
    Owner

    Thanks for raising this. There are certainly some considerations here.

    Here are my initial thoughts.

    1. I think load-env itself should handle null/ nothing as unset variable. Obviously would need this proposal to be taken to the core Nushell team, but my feeling is that is the right solution.
    2. PATH handling I am unsure about. Is the current behaviour that Nushell itself doesn’t notice the new path, but any child processes do? If so isn’t that also arguably a mis-feature in Nushell core which should be fixed there (also in load-env)?
    3. I think PWD should be filtered out by bash-env-json and simply not appear in the output. I would be happy to make such a change there. The point is, cd is simply out of scope for what an environment file is reasonably expected to do.
    4. Yes 😁

    Trying to give you an early reply as I’m otherwise pretty tied up this week sorry. Hopefully this suggests alternative solutions for you?

    Thanks for your interest!

  2. TyceHerrman commented on Jul 29, 2026

    @TyceHerrman
    ContributorAuthor

    Very helpful!

    For load-env handling null or nothing as an unset variable, I agree now that this is a better fit in Nushell core. There’s nushell/nushell#15872, which covers explicit unset flags but doesn’t cover treating nothing as removal, so I’ll bring this up over there in core.

    For PATH handling, Nushell sees the change. which finds new commands from the updated PATH and they execute. Child processes also receive the updated PATH. Only $env.PATH changes from a list to a string. Nushell’s Direnv guide tells users to convert PATH after load-env to restore its usual list form. This issue would affect any external environment importer, so Nushell core providing an explicit conversion option that applies $env.ENV_CONVERSIONS may make the most sense. I’ll raise that upstream too.

    Filtering PWD would be better in bash-env-json. I agree. That prevents load-env from rejecting the entire delta. As an extra wrinkle, OLDPWD can change with it, so filtering both avoids inconsistent directory state.

    I’ll leave this open for now and reevaluate what this issue should actually be scoped to after those things are resolved.

  3. tesujimath commented on Aug 1, 2026

    @tesujimath
    Owner

    The PWD aspects of this are now fixed in tesujimath/bash-env-json#19, released as bash-env-json 0.11.0.

  4. TyceHerrman commented on Oct 15, 2026

    @TyceHerrman
    ContributorAuthor

    status with Nushell 0.116.1 and bash-env-json 0.11.0:

    • Directory variable filtering is fixed upstream. bash-env-json #19, released in 0.11.0, filters PWD, OLDPWD, and DIRSTACK. I confirmed that changing directories in the Bash input no longer causes load-env to reject the delta.
    • PATH normalization is available in Nushell core. Nushell #18832 makes refreshing ENV_CONVERSIONS also restore PATH to its usual list form.
    • Unset semantics remain unresolved in Nushell core. Nushell #15872 remains open. load-env still keeps keys with nothing values in $env, although child processes don't receive them. Actual removal still requires hide-env.

    When the imported Bash environment changes PATH, load-env leaves it as a string. The second line restores it to a Nushell list and applies configured environment conversions.

    bash-env ./environment.sh | load-env
    $env.ENV_CONVERSIONS = $env.ENV_CONVERSIONS

    pattern needs documenting and a regression test

    Separately, project's bash-env-json pin dependency could use an update to bring in the directory filtering fix from 0.11.0 (will open pr for this)

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

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions