A simple helper that makes it easy to package Nu scripts with Nix, including inline dependency declarations for external commands and other packaged scripts.
flake nu nushell nix
1

Configure Feed

Select the types of activity you want to include in your feed.

initial commit

cswimr (Jul 13, 2026, 11:41 AM EDT) c7c40cd1

+79
+6
flake.nix
··· 1 + { 2 + inputs = { }; 3 + outputs = { ... }: { 4 + lib.mkNuShell = import ./mkNuScript.nix; 5 + }; 6 + }
+73
mkNuScript.nix
··· 1 + { 2 + pkgs, 3 + lib ? pkgs.lib, 4 + self ? null, 5 + inputs ? { }, 6 + src, 7 + file, 8 + name ? baseNameOf (lib.removeSuffix ".nu" file), 9 + meta ? { }, 10 + system ? pkgs.stdenv.hostPlatform.system, 11 + nushell ? pkgs.nushell, 12 + }: 13 + let 14 + resolveDependency = 15 + dep: 16 + let 17 + segments = lib.splitString "." dep; 18 + scope = builtins.head segments; 19 + rest = builtins.tail segments; 20 + joinSegments = lib.concatStringsSep "."; 21 + in 22 + if (scope == "self" && rest != [ ]) then 23 + if self != null then 24 + self.packages.${system}.${joinSegments rest} 25 + else 26 + throw "`self` was not passed to `mkNuScript` or is `null`, but a script references a dependency with the `self.` scope." 27 + else if (scope == "inputs" && rest != [ ]) then 28 + inputs.${builtins.head rest}.packages.${system}.${joinSegments (builtins.tail rest)} 29 + else 30 + pkgs.${dep}; 31 + 32 + extraDependencies = 33 + let 34 + script = builtins.readFile file; 35 + lines = lib.splitString "\n" script; 36 + start = lib.lists.findFirstIndex (line: line == "# START: External Dependencies") null lines; 37 + end = lib.lists.findFirstIndex (line: line == "# END: External Dependencies") null lines; 38 + deps = 39 + if (start != null && end != null) then 40 + map (line: lib.removePrefix "# " (lib.removePrefix "# " line)) ( 41 + builtins.filter (line: lib.hasPrefix "#" line && line != "#") ( 42 + lib.sublist (start + 1) (end - start - 1) lines 43 + ) 44 + ) 45 + else 46 + [ ]; 47 + in 48 + map resolveDependency deps; 49 + 50 + deps = extraDependencies ++ [ nushell ]; 51 + in 52 + pkgs.stdenv.mkDerivation { 53 + inherit name src; 54 + nativeBuildInputs = with pkgs; [ makeWrapper ]; 55 + installPhase = '' 56 + runHook preInstall 57 + 58 + mkdir -p $out/bin 59 + cp $src/${file} $out/bin/${name} 60 + chmod +x $out/bin/${name} 61 + 62 + substituteInPlace $out/bin/${name} \ 63 + --replace-fail "#!/usr/bin/env nu" "#!${lib.getExe nushell}" 64 + 65 + wrapProgram $out/bin/${name} \ 66 + --set PATH ${lib.makeBinPath deps} 67 + 68 + runHook postInstall 69 + ''; 70 + meta = meta // { 71 + mainProgram = name; # override so that mainProgram always matches what the name of the script in $out/bin is 72 + }; 73 + }