Nushell Shell Integration Failure in Home Manager Module
Summary
The worktrunk Nushell shell integration fails to wrap the wt command correctly when installed via the Home Manager module. This failure prevents automatic directory changes (e.g., during wt switch) because Nushell executes the external binary directly instead of the shell-wrapped function.
Root Cause
The worktrunk Home Manager module (located in nix/home-manager-module.nix in the worktrunk repository) employs the use command to load the generated Nushell configuration:
nushell = lib.mkIf cfg.enableNushellIntegration {
extraConfig = ''
use ${
pkgs.runCommand "worktrunk-nushell-config.nu" { } ''
${lib.getExe cfg.package} config shell init nu > $out
''
}
'';
};
This approach fails for two reasons:
- Missing Exports: The
templates/nushell.nufile defines thewtfunction usingdef, notexport def. In Nushell,useon a file without exports runs the script but does not bring its definitions into the caller's scope. - Namespace Isolation: Even with exports,
useon a file with a dynamic name (generated by Nix) would place the command under a randomized namespace unlessuse ... *is employed.
Observed Error
When attempting to switch worktrees:
▲ Worktree for feature-a @ ~/Documents/Projects/home-manager.feature-a, but cannot change directory — ran /home/lateef/.nix-profile/bin/wt; shell integration wraps wt
↳ To change directory, run wt switch feature-a
↳ To enable automatic cd, run wt config shell install
Recommended Fixes
- Switch to
source: Update the Home Manager module to usesourceinstead ofuse.sourceoperates at parse-time and brings all definitions into the current scope, mimicking the behavior ofevalin POSIX shells. - Add
exportto Template: Updatetemplates/nushell.nuto useexport deffor the wrapper function and completions. This ensures the file acts as a proper Nushell module.
Immediate Workaround
Disable the built-in Nushell integration and manually source the configuration in the Home Manager nushell configuration:
# In worktrunk.nix
programs.worktrunk.enableNushellIntegration = false;
# In nushell.nix
programs.nushell.extraConfig = ''
source ${
pkgs.runCommand "worktrunk-nushell-config.nu" { } ''
${lib.getExe config.programs.worktrunk.package} config shell init nu > $out
''
}
'';
Thanks for reporting this — and for the excellent root cause analysis! I was able to confirm the issue and identified the same two problems you described:
- The Home Manager module uses
useinstead ofsource, so definitions aren't brought into the caller's scope - The nushell template uses
definstead ofexport def, so definitions aren't exported when loaded as a module
I've opened #1476 with a fix that addresses both:
- Switches
use→sourceinnix/home-manager-module.nix(matching how bash/zsh/fish integrations work) - Adds
exportto bothdefstatements in the nushell template (so the file works correctly regardless of load method)
A maintainer will review it shortly.