This commit is contained in:
atagen 2026-05-19 23:32:51 +10:00
parent da4bc139eb
commit fc1b3886bc
49 changed files with 9089 additions and 5482 deletions

View file

@ -10,7 +10,7 @@
#
# Usage:
# { pkgs, ... }: {
# imports = [ ./path/to/inshellah/nix/module.nix ];
# imports = [ ./path/to/inshellah-rs/nix/module.nix ];
# programs.inshellah.enable = true;
# }
@ -23,6 +23,34 @@
let
cfg = config.programs.inshellah;
completerSnippet = ./inshellah-completer.nu;
dynamicStubCommands = [
"systemctl"
"journalctl"
"coredumpctl"
"loginctl"
"machinectl"
"networkctl"
"hostnamectl"
"timedatectl"
"localectl"
"ssh"
"scp"
"sftp"
"docker"
"podman"
"kubectl"
"git"
"jj"
"npm"
"pnpm"
"yarn"
"make"
"just"
"cargo"
"pkill"
];
dynamicStubCommandArgs = lib.escapeShellArgs dynamicStubCommands;
in
{
options.programs.inshellah = {
@ -72,9 +100,33 @@ in
'';
};
timeoutMs = lib.mkOption {
type = lib.types.nullOr lib.types.int;
default = null;
example = 200;
description = ''
per-subprocess timeout in milliseconds. when null the binary's
compiled-in default is used (currently 200ms).
'';
};
workers = lib.mkOption {
type = lib.types.nullOr lib.types.int;
default = null;
example = 8;
description = ''
worker thread count for the parallel scrape pool. when null,
`std::thread::available_parallelism` is used.
'';
};
snippet = lib.mkOption {
type = lib.types.str;
readOnly = true;
default = builtins.readFile completerSnippet;
description = ''
nushell external completer snippet installed by the module.
'';
};
};
@ -98,7 +150,10 @@ in
(lib.hiPrio wrapped)
cfg.package
];
environment.pathsToLink = [ "/share/nushell/autoload" ];
environment.pathsToLink = [
"/share/nushell/autoload"
"/share/nushell/vendor/autoload"
];
environment.extraSetup =
let
inshellah = "${cfg.package}/bin/inshellah";
@ -109,30 +164,38 @@ in
lib.concatStringsSep "\n" cfg.helpOnlyCommands
);
helpOnlyFlag = lib.optionalString (cfg.helpOnlyCommands != [ ]) " --help-only ${helpOnlyFile}";
timeoutFlag = lib.optionalString (cfg.timeoutMs != null) " --timeout-ms ${toString cfg.timeoutMs}";
workersFlag = lib.optionalString (cfg.workers != null) " --workers ${toString cfg.workers}";
snippetFile = pkgs.writeText "inshellah-completer.nu" cfg.snippet;
in
''
mkdir -p ${destDir}
if [ -d "$out/bin" ] && [ -d "$out/share/man" ]; then
${inshellah} index "$out" --dir ${destDir}${ignoreFlag}${helpOnlyFlag} \
${inshellah} index "$out" --dir ${destDir}${ignoreFlag}${helpOnlyFlag}${timeoutFlag}${workersFlag} \
2>/dev/null || true
fi
find ${destDir} -maxdepth 1 -empty -delete
# nushell hardcodes sudo and doas to bypass the external completer,
# returning command-name completion instead of calling inshellah.
# these @complete external stubs override that so inshellah handles
# their flags and elevation stripping. placed in the nushell autoload
# dir so they are sourced automatically at shell startup.
# Install the full nushell completer plus sudo/doas wrapped commands.
# Nushell otherwise hardcodes sudo/doas to bypass external completers.
mkdir -p $out/share/nushell/vendor/autoload
cat > $out/share/nushell/vendor/autoload/inshellah-elevation.nu << 'NUSHELL'
@complete external
extern "sudo" []
cp ${snippetFile} $out/share/nushell/vendor/autoload/inshellah.nu
@complete external
extern "doas" []
NUSHELL
# Register command names for dynamic backends that are actually present
# in the linked profile. The externs keep Nu's command list aware of
# these commands while the external completer still supplies arguments.
stubFile=$out/share/nushell/vendor/autoload/inshellah-command-stubs.nu
: > "$stubFile"
for cmd in ${dynamicStubCommandArgs}; do
if [ -x "$out/bin/$cmd" ]; then
printf '@complete external\nextern "%s" [...args]\n\n' "$cmd" >> "$stubFile"
fi
done
if [ ! -s "$stubFile" ]; then
rm -f "$stubFile"
fi
'';
};
}