Skip to content

nushell: load session environment at runtime - #9666

Open
LisaScheers wants to merge 1 commit into
nix-community:masterfrom
LisaScheers:agent/nushell-session-environment
Open

nushell: load session environment at runtime#9666
LisaScheers wants to merge 1 commit into
nix-community:masterfrom
LisaScheers:agent/nushell-session-environment

Conversation

@LisaScheers

@LisaScheers LisaScheers commented Jul 16, 2026

Copy link
Copy Markdown

What changed

  • Generate a Nushell env.nu loader when a system or Home Manager session
    environment needs to be applied.
  • At shell startup, source the OS-generated setEnvironment script when Home Manager is used as a NixOS or nix-darwin module, followed by hm-session-vars.sh.
  • Capture only variables changed by those scripts and load them into Nushell, preserving PATH as a list<string>.
  • Keep the existing source guards and load the generated environment before the user's envFile/extraEnv, so inherited or user-specific values keep their expected precedence.
  • Document the new env.nu ownership and migration path in the 26.11 release
    notes.

Why

Nushell does not source the POSIX environment scripts used by Bash and Zsh. As a result, a directly launched Nushell login shell can miss the Nix profile PATH, Home Manager session variables, and expansions involving runtime values such as $HOME and $USER.

Evaluating these scripts at build time captures sandbox values instead of the user's login environment. This implementation evaluates them at shell startup and transfers the resulting environment through a NUL-delimited representation, which also handles values containing newlines.

This builds on the investigation and experiments in #6941 while avoiding its sandbox-environment limitation.

User impact

Nushell can be launched directly instead of through Zsh or Bash while receiving the system and Home Manager environment. home.sessionVariables, home.sessionPath, POSIX default/alternate expansions, and NixOS/nix-darwin profile paths are applied consistently.

Fixes #4313
Fixes #4918
Fixes #6507
Addresses #6568
Fixes #6901

Validation

  • Added a runtime test that launches Nushell from an intentionally minimal environment and verifies OS variables, $HOME/$USER expansion, POSIX default/alternate expansion, home.sessionPath, list-form PATH, and user env.nu precedence.
  • Ran the full test suite with nix build .#test-all.
  • Built all 13 Nushell-focused Home Manager module tests on aarch64-darwin.
  • Evaluated the new test derivation for x86_64-linux.
  • Rendered the HTML manual with nix build .#docs-html.
  • Ran the repository formatter, the 80-column check for added lines, and
    git diff --check.

@github-actions github-actions Bot added the shell label Jul 16, 2026
@LisaScheers
LisaScheers force-pushed the agent/nushell-session-environment branch from 79ac1f5 to fa1d942 Compare July 16, 2026 04:17
Source the OS and Home Manager POSIX environment scripts when Nushell
starts so runtime HOME and USER expansions and profile paths match managed
POSIX shells.
@LisaScheers
LisaScheers force-pushed the agent/nushell-session-environment branch from fa1d942 to 460b87c Compare July 16, 2026 04:18
@LisaScheers
LisaScheers marked this pull request as ready for review July 16, 2026 04:21
@iniw

iniw commented Jul 29, 2026

Copy link
Copy Markdown
Contributor

Note that this PR still doesn't you to seamlessly set nushell as the login shell on macOS if you want the config to be stored in XDG directories. If XDG_CONFIG_HOME is not set during startup, nushell will look for the config file in ~/Library/Application Support/nushell, which is not where home manager has placed it if you have the XDG-related options enabled. I'm not sure what the best way to fix it is 🤷

@LisaScheers

Copy link
Copy Markdown
Author

the real solution is to let the xdg module write a plist with something like

<!-- ~/Library/LaunchAgents/setenv.XDG_CONFIG_HOME.plist -->
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
  <plist version="1.0">
  <dict>
  <key>Label</key>
  <string>setenv.XDG_CONFIG_HOME</string>
  <key>ProgramArguments</key>
  <array>
    <string>/bin/launchctl</string>
    <string>setenv</string>
    <string>XDG_CONFIG_HOME</string>
    <string>{USER_HOME}/.config</string>
  </array>
  <key>RunAtLoad</key>
  <true/>
</dict>
</plist>

to ~/Library/LaunchAgents/setenv.XDG_CONFIG_HOME.plist

this would set it for all applications

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

Labels

Projects

None yet

2 participants