inshellah/nix/module.nix
2026-03-23 12:17:45 +11:00

92 lines
2.6 KiB
Nix

# NixOS module: automatic nushell completion indexing
#
# Indexes completions using three strategies in priority order:
# 1. Native completion generators (e.g. CMD completions nushell)
# 2. Manpage parsing
# 3. --help output parsing
#
# Produces a directory of .json/.nu files at build time.
# The `complete` command reads from this directory as a system overlay.
#
# Usage:
# { pkgs, ... }: {
# imports = [ ./path/to/inshellah/nix/module.nix ];
# programs.inshellah.enable = true;
# }
{
config,
lib,
pkgs,
...
}:
let
cfg = config.programs.inshellah;
in
{
options.programs.inshellah = {
enable = lib.mkEnableOption "nushell completion indexing via inshellah";
package = lib.mkOption {
type = lib.types.package;
description = "The inshellah package to use for indexing completions.";
};
completionsPath = lib.mkOption {
type = lib.types.str;
default = "/share/inshellah";
description = ''
Subdirectory within the merged environment where completion files
are placed. Used as the system-dir for the completer.
'';
};
ignoreCommands = lib.mkOption {
type = lib.types.listOf lib.types.str;
default = [];
example = [ "meat" "problematic-tool" ];
description = ''
List of command names to skip during completion indexing.
'';
};
};
config = lib.mkIf cfg.enable {
environment.pathsToLink = [ cfg.completionsPath ];
environment.extraSetup =
let
inshellah = "${cfg.package}/bin/inshellah";
destDir = "$out${cfg.completionsPath}";
segments = lib.filter (s: s != "") (lib.splitString "/" cfg.completionsPath);
derefPath = lib.concatMapStringsSep "\n " (seg: ''
_cur="$_cur/${seg}"
if [ -L "$_cur" ]; then
_target=$(readlink "$_cur")
rm "$_cur"
mkdir -p "$_cur"
if [ -d "$_target" ]; then
cp -rT "$_target" "$_cur"
chmod -R u+w "$_cur"
fi
fi'') segments;
ignoreFile = pkgs.writeText "inshellah-ignore" (lib.concatStringsSep "\n" cfg.ignoreCommands);
ignoreFlag = lib.optionalString (cfg.ignoreCommands != []) " --ignore ${ignoreFile}";
in
''
_cur="$out"
${derefPath}
mkdir -p ${destDir}
# Index completions in one pass:
# native generators > manpages > --help fallback
if [ -d "$out/bin" ] && [ -d "$out/share/man" ]; then
${inshellah} index "$out" --dir ${destDir}${ignoreFlag} \
2>/dev/null || true
fi
find ${destDir} -maxdepth 1 -empty -delete
'';
};
}