worktrunk icon indicating copy to clipboard operation
worktrunk copied to clipboard

Nushell Shell Integration Failure in Home Manager Module

Open mystilleef opened this issue 8 months ago • 1 comments

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:

  1. Missing Exports: The templates/nushell.nu file defines the wt function using def, not export def. In Nushell, use on a file without exports runs the script but does not bring its definitions into the caller's scope.
  2. Namespace Isolation: Even with exports, use on a file with a dynamic name (generated by Nix) would place the command under a randomized namespace unless use ... * 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

  1. Switch to source: Update the Home Manager module to use source instead of use. source operates at parse-time and brings all definitions into the current scope, mimicking the behavior of eval in POSIX shells.
  2. Add export to Template: Update templates/nushell.nu to use export def for 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
    ''
  }
'';

mystilleef avatar Mar 12 '26 23:03 mystilleef

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:

  1. The Home Manager module uses use instead of source, so definitions aren't brought into the caller's scope
  2. The nushell template uses def instead of export def, so definitions aren't exported when loaded as a module

I've opened #1476 with a fix that addresses both:

  • Switches use → source in nix/home-manager-module.nix (matching how bash/zsh/fish integrations work)
  • Adds export to both def statements in the nushell template (so the file works correctly regardless of load method)

A maintainer will review it shortly.

worktrunk-bot avatar Mar 13 '26 00:03 worktrunk-bot