diff --git a/flake.nix b/flake.nix index 529ab8b..2b9bf73 100644 --- a/flake.nix +++ b/flake.nix @@ -73,6 +73,12 @@ } nuScript; + hermenix-completions = pkgs.writeTextFile { + name = "hermenix-completions"; + destination = "/share/nushell/vendor/autoload/hermenix.nu"; + text = builtins.readFile ./nu/completions.nu; + }; + default = self.packages.${system}.hermenix; }; }; diff --git a/modules/host.nix b/modules/host.nix index 73661df..7fa17d6 100644 --- a/modules/host.nix +++ b/modules/host.nix @@ -809,6 +809,14 @@ in }; environment.systemPackages = [ + # Nushell tab-completion definitions for hermenix. Installed to + # vendor/autoload so nushell picks them up automatically. + (pkgs.writeTextFile { + name = "hermenix-completions"; + destination = "/share/nushell/vendor/autoload/hermenix.nu"; + text = builtins.readFile ../nu/completions.nu; + }) + # Single consolidated CLI: `hermenix vm *` controls the VM, # `hermenix ctl *` drives Hermes inside it. Per-host values are # passed as env vars via makeWrapperArgs --set. diff --git a/nu/completions.nu b/nu/completions.nu new file mode 100644 index 0000000..8bdd9e2 --- /dev/null +++ b/nu/completions.nu @@ -0,0 +1,72 @@ +# Tab-completion definitions for hermenix. +# +# !! GENERATED — do not edit by hand. +# !! Regenerate with: nu nu/gen-completions.nu +# +# Installed to $out/share/nushell/vendor/autoload/ so nushell on NixOS +# picks them up automatically from environment.systemPackages. No manual +# sourcing needed. + +def "nu-complete hermenix top" [] { + [ + [value description]; + [setup "guided host-side setup"] + [migrate "carry an existing flat HERMES_HOME into the guest"] + [remote "run setup/migrate on a remote host over SSH"] + [vm "control the VM (start/stop/console/snapshots/backups)"] + [ctl "drive Hermes inside the guest (identity/config/secrets/gateway)"] + ] +} + +def "nu-complete hermenix vm" [] { + [ + [value description]; + [ssh "open a shell or run a single command in the guest"] + [console "follow the VM's journal output (journalctl -f)"] + [start "systemctl start hermes-vm"] + [stop "systemctl stop hermes-vm"] + [restart "systemctl restart hermes-vm"] + [config "show current generation, history and git status"] + [config-pull "copy the guest's /etc/nixos here for reading/diffing"] + [snapshot "trigger a state-disk snapshot now"] + [snapshots "list existing snapshots"] + [rollback "restore the state disk from a snapshot"] + [backup "trigger a restic backup now"] + [backups "list restic snapshots"] + ] +} + +def "nu-complete hermenix ctl" [] { + [ + [value description]; + [soul "show or edit SOUL.md"] + [memory "show or edit memories/USER.md"] + [conf "show or edit config.yaml"] + [secrets "manage .env (keys masked by default)"] + [status "gateway service status"] + [logs "follow gateway journal (-f)"] + [restart "restart the gateway"] + [units "list all hermes* user units"] + [reseed "overwrite identity from /etc/nixos/hermes"] + [pull "copy live identity back to the host"] + ] +} + +# Context-aware completer: inspects already-typed words to decide +# whether to offer top-level subcommands or a second-level list. +def "nu-complete hermenix ctx" [context: string, offset: int] { + let words = ($context | str trim | split row " " | where { $in != "" }) + match ($words | length) { + 1 => (nu-complete hermenix top) + 2 => { + match ($words | get 1) { + "vm" => (nu-complete hermenix vm) + "ctl" => (nu-complete hermenix ctl) + _ => [] + } + } + _ => [] + } +} + +extern "hermenix" [...args: string@"nu-complete hermenix ctx"] diff --git a/nu/gen-completions.nu b/nu/gen-completions.nu new file mode 100644 index 0000000..56f7b1b --- /dev/null +++ b/nu/gen-completions.nu @@ -0,0 +1,147 @@ +#!/usr/bin/env nu +# +# gen-completions.nu — regenerate nu/completions.nu from the usage print +# blocks in the source files. +# +# Run from the repo root: +# nu nu/gen-completions.nu +# +# The single source of truth for each subcommand's name and description is +# its line in the relevant usage block (main [], main vm [], main ctl []). +# This script extracts those lines and rewrites completions.nu in place so +# they never drift apart. + +# Parse the usage print lines from a source file. +# +# Looks for lines of the form: +# print " [optional args] " +# where name and description are separated by 2+ spaces. +# +# strip_prefix: if non-empty, strip " " from the front of each +# line's content before splitting (used for hermenix.nu, where lines read +# " hermenix setup desc" and we want just "setup"). +# +# Returns a list of {value, description} records, skipping any line whose +# extracted name contains a quote or starts with "hermenix" (those are +# passthrough-example lines, not real subcommands). +def parse-usage [file: string, strip_prefix: string = ""] { + open --raw $file + | lines + | where { $in =~ 'print " \S' } + | each {|line| + # Strip everything up to and including the opening `print "` and the + # closing `"` (last one), leaving just the inner usage string. + let inner = ($line + | str replace -r '^[^"]*"' "" + | str replace -r '"[^"]*$' "") + + # Optionally remove a leading " " so that + # " hermenix setup desc" becomes " setup desc". + let body = if ($strip_prefix != "") { + $inner | str replace $" ($strip_prefix) " " " + } else { + $inner + } + + # Split on 2+ spaces — left side is "name [optional args]", right is desc. + let parts = ($body | str trim | split row -r " +" | where { $in != "" }) + + if ($parts | length) < 2 { + null + } else { + let name = ($parts | first | split row " " | first) + # Re-join all remaining parts — descriptions like "systemctl stop hermes-vm" + # would otherwise get cut at the double-space inside the description. + let desc = ($parts | skip 1 | str join " " | str trim) + {value: $name, description: $desc} + } + } + | where { $in != null } + | where { $in.value != "" } + | where { not ($in.value | str contains '"') } + | where { not ($in.value | str starts-with "hermenix") } + | uniq-by value +} + +# Render one `nu-complete` function that returns a [value description] table. +def render-complete [fn_name: string, entries: list] { + let rows = ($entries + | each {|e| $" [($e.value) \"($e.description)\"]" } + | str join "\n") + [ + $"def \"nu-complete ($fn_name)\" [] {" + " [" + " [value description];" + $rows + " ]" + "}" + ] | str join "\n" +} + +# Render the context-aware dispatcher that picks between the three tables. +# Written as a single-quoted literal — no interpolation needed. +def render-ctx [] { +'# Context-aware completer: inspects already-typed words to decide +# whether to offer top-level subcommands or a second-level list. +def "nu-complete hermenix ctx" [context: string, offset: int] { + let words = ($context | str trim | split row " " | where { $in != "" }) + match ($words | length) { + 1 => (nu-complete hermenix top) + 2 => { + match ($words | get 1) { + "vm" => (nu-complete hermenix vm) + "ctl" => (nu-complete hermenix ctl) + _ => [] + } + } + _ => [] + } +}' +} + +def render-extern [] { + 'extern "hermenix" [...args: string@"nu-complete hermenix ctx"]' +} + +def render-header [] { + [ + "# Tab-completion definitions for hermenix." + "#" + "# !! GENERATED — do not edit by hand." + "# !! Regenerate with: nu nu/gen-completions.nu" + "#" + "# Installed to $out/share/nushell/vendor/autoload/ so nushell on NixOS" + "# picks them up automatically from environment.systemPackages. No manual" + "# sourcing needed." + ] | str join "\n" +} + +# ── main ──────────────────────────────────────────────────────────────── + +let repo_root = ($env.FILE_PWD | path dirname) +let out_file = ($repo_root | path join "nu" "completions.nu") + +let top = (parse-usage ($repo_root | path join "nu" "hermenix.nu") "hermenix") +let vm = (parse-usage ($repo_root | path join "nu" "vm" "vm.nu")) +let ctl = (parse-usage ($repo_root | path join "nu" "ctl" "ctl.nu")) + +if ($top | is-empty) { print -e "warn: no top-level completions found in hermenix.nu" } +if ($vm | is-empty) { print -e "warn: no vm completions found in vm/vm.nu" } +if ($ctl | is-empty) { print -e "warn: no ctl completions found in ctl/ctl.nu" } + +[ + (render-header) + "" + (render-complete "hermenix top" $top) + "" + (render-complete "hermenix vm" $vm) + "" + (render-complete "hermenix ctl" $ctl) + "" + (render-ctx) + "" + (render-extern) + "" +] | str join "\n" | save -f $out_file + +print $"wrote ($out_file) [top=($top | length) vm=($vm | length) ctl=($ctl | length)]"