comprehensive completion generation: native, manpage, --help
Three-strategy pipeline with priority: native completion generators (e.g. CMD completions nushell) > manpage parsing > --help fallback. Single `generate` command produces one module-wrapped .nu file per command. Parallel execution scaled to cores, 200ms timeouts, ELF string scanning to skip binaries without -h support, native gzip decompression via camlzip, SYNOPSIS-based subcommand detection, nix3 manpage strategy, deduplication, nushell builtin exclusion.
This commit is contained in:
parent
01ccf64efc
commit
7f0ec8ab4d
9 changed files with 937 additions and 265 deletions
122
nix/module.nix
122
nix/module.nix
|
|
@ -1,70 +1,37 @@
|
|||
# NixOS module: automatic nushell completion generation from manpages
|
||||
# NixOS module: automatic nushell completion generation
|
||||
#
|
||||
# Modeled on nixpkgs' programs/fish.nix completion generator.
|
||||
# For each package in environment.systemPackages, a small derivation runs
|
||||
# `inshellah manpage-dir` against its share/man directory. Results are merged
|
||||
# into a single directory and placed in nushell's vendor autoload path.
|
||||
# Generates completions using three strategies in priority order:
|
||||
# 1. Native completion generators (e.g. CMD completions nushell)
|
||||
# 2. Manpage parsing
|
||||
# 3. --help output parsing
|
||||
#
|
||||
# Usage in your NixOS configuration:
|
||||
# Runs as a single pass during the system profile build.
|
||||
#
|
||||
# Usage:
|
||||
# { pkgs, ... }: {
|
||||
# imports = [ ./path/to/inshellah/nix/module.nix ];
|
||||
# programs.inshellah.enable = true;
|
||||
# # Optionally add packages not in systemPackages:
|
||||
# # programs.inshellah.extraPackages = [ pkgs.kubectl ];
|
||||
# }
|
||||
|
||||
{ config, lib, pkgs, ... }:
|
||||
{
|
||||
config,
|
||||
lib,
|
||||
pkgs,
|
||||
...
|
||||
}:
|
||||
|
||||
let
|
||||
cfg = config.programs.inshellah;
|
||||
|
||||
# The inshellah binary — override this if you build from the local flake
|
||||
inshellahPkg = cfg.package;
|
||||
|
||||
# Per-package derivation: run inshellah manpage-dir against a package's manpages
|
||||
generateCompletions = package:
|
||||
pkgs.runCommandLocal
|
||||
(let
|
||||
inherit (lib.strings) stringLength substring storeDir;
|
||||
storeLength = stringLength storeDir + 34;
|
||||
pathName = substring storeLength (stringLength package - storeLength) package;
|
||||
in
|
||||
(package.name or pathName) + "_nu-completions"
|
||||
)
|
||||
({
|
||||
inherit package;
|
||||
nativeBuildInputs = [ inshellahPkg ];
|
||||
} // lib.optionalAttrs (package ? meta.priority) {
|
||||
meta.priority = package.meta.priority;
|
||||
})
|
||||
''
|
||||
mkdir -p $out
|
||||
if [ -d "$package/share/man" ]; then
|
||||
inshellah manpage-dir "$package/share/man" > "$out/completions.nu" 2>/dev/null || true
|
||||
# Remove empty files
|
||||
find $out -empty -delete
|
||||
fi
|
||||
'';
|
||||
|
||||
in {
|
||||
in
|
||||
{
|
||||
options.programs.inshellah = {
|
||||
enable = lib.mkEnableOption "nushell completion generation from manpages via inshellah";
|
||||
enable = lib.mkEnableOption "nushell completion generation via inshellah";
|
||||
|
||||
package = lib.mkOption {
|
||||
type = lib.types.package;
|
||||
description = "The inshellah package to use for generating completions.";
|
||||
};
|
||||
|
||||
extraPackages = lib.mkOption {
|
||||
type = lib.types.listOf lib.types.package;
|
||||
default = [];
|
||||
description = ''
|
||||
Additional packages to generate nushell completions from, beyond
|
||||
those in {option}`environment.systemPackages`.
|
||||
'';
|
||||
};
|
||||
|
||||
generatedCompletionsPath = lib.mkOption {
|
||||
type = lib.types.str;
|
||||
default = "/share/nushell/vendor/autoload";
|
||||
|
|
@ -77,33 +44,38 @@ in {
|
|||
};
|
||||
|
||||
config = lib.mkIf cfg.enable {
|
||||
# Merge all per-package completions into a single directory.
|
||||
# This path ends up in the system profile, and nushell discovers it
|
||||
# via XDG_DATA_DIRS -> $prefix/share/nushell/vendor/autoload/
|
||||
environment.pathsToLink = [ cfg.generatedCompletionsPath ];
|
||||
|
||||
environment.systemPackages = [
|
||||
(pkgs.buildEnv {
|
||||
name = "nushell-generated-completions";
|
||||
ignoreCollisions = true;
|
||||
paths = map generateCompletions (
|
||||
config.environment.systemPackages ++ cfg.extraPackages
|
||||
);
|
||||
pathsToLink = [ "/" ];
|
||||
# Nest everything under the vendor autoload path
|
||||
postBuild = ''
|
||||
if [ -d "$out" ]; then
|
||||
tmp=$(mktemp -d)
|
||||
cp -r "$out/"* "$tmp/" 2>/dev/null || true
|
||||
rm -rf "$out/"*
|
||||
mkdir -p "$out${cfg.generatedCompletionsPath}"
|
||||
for f in "$tmp"/*.nu; do
|
||||
[ -f "$f" ] && cp "$f" "$out${cfg.generatedCompletionsPath}/"
|
||||
done
|
||||
rm -rf "$tmp"
|
||||
fi
|
||||
'';
|
||||
})
|
||||
];
|
||||
environment.extraSetup =
|
||||
let
|
||||
inshellah = "${cfg.package}/bin/inshellah";
|
||||
destDir = "$out${cfg.generatedCompletionsPath}";
|
||||
segments = lib.filter (s: s != "") (lib.splitString "/" cfg.generatedCompletionsPath);
|
||||
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;
|
||||
in
|
||||
''
|
||||
_cur="$out"
|
||||
${derefPath}
|
||||
mkdir -p ${destDir}
|
||||
|
||||
# Generate all completions in one pass:
|
||||
# native generators > manpages > --help fallback
|
||||
if [ -d "$out/bin" ] && [ -d "$out/share/man" ]; then
|
||||
${inshellah} generate "$out/bin" "$out/share/man" -o ${destDir} \
|
||||
2>/dev/null || true
|
||||
fi
|
||||
|
||||
find ${destDir} -maxdepth 1 -empty -delete
|
||||
'';
|
||||
};
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue