def home-dir [] { $env.HERMES_HOME_DIR? | default "/var/lib/hermes/.hermes" } def gateway-unit [] { $env.HERMES_GATEWAY_UNIT? | default "hermes-agent" } def ctl-gssh [cmd: string] { run-argv ((ssh-args) ++ [$cmd]) } def edit-remote [remote: string, mode: string = "0644"] { let tmp = (mktemp) let fetched = do -i { ctl-gssh $"cat ($remote)" } | complete $fetched.stdout | save -f $tmp let before = open --raw $tmp | hash sha256 ^($env.EDITOR? | default "vim") $tmp if (open --raw $tmp | hash sha256) == $before { rm $tmp say "unchanged" return } (open --raw $tmp) | ctl-gssh $"install -m ($mode) /dev/stdin ($remote)" rm $tmp say $"written to ($remote)" if (confirm "restart the gateway now?") { ctl-gssh $"systemctl --user restart (gateway-unit)" } } # Single string, not rest args: nu matches bare --flags on this command first. # Quote multi-word passthroughs: hermenix ctl "chat --model x" def "main ctl" [remainder: string = ""] { if $remainder == "" { print "usage: hermenix ctl " print "" print " soul [edit] show or edit SOUL.md" print " memory [edit] show or edit memories/USER.md" print " conf [edit] show or edit config.yaml" print " secrets [edit|show] manage .env (keys masked by default)" print " status gateway service status" print " logs follow gateway journal (-f)" print " restart restart the gateway" print " units list all hermes* user units" print " reseed overwrite identity from /etc/nixos/hermes" print " pull [dir] copy live identity back to the host" print "" print "Anything else is passed straight to the hermes CLI in the guest:" print " hermenix ctl \"chat\"" print " hermenix ctl \"update\"" print " hermenix ctl \"sessions list\"" exit 1 } let words = $remainder | split row " " | where {|w| $w != "" } run-argv ((ssh-args) ++ ["-t" "hermes"] ++ $words) } def "main ctl soul" [action?: string] { if $action == "edit" { edit-remote $"(home-dir)/SOUL.md" } else { ctl-gssh $"cat (home-dir)/SOUL.md" } } def "main ctl memory" [action?: string] { if $action == "edit" { edit-remote $"(home-dir)/memories/USER.md" } else { ctl-gssh $"cat (home-dir)/memories/USER.md" } } def "main ctl conf" [action?: string] { if $action == "edit" { edit-remote $"(home-dir)/config.yaml" } else { ctl-gssh $"cat (home-dir)/config.yaml" } } def "main ctl secrets" [action?: string] { if $action == "edit" { edit-remote $"(home-dir)/.env" "0600" } else if $action == "show" { ctl-gssh $"cat (home-dir)/.env" } else { ctl-gssh $"sed 's/=.*/=/' (home-dir)/.env" } } def "main ctl status" [] { ctl-gssh $"systemctl --user status (gateway-unit) --no-pager" } def "main ctl logs" [] { run-argv ((ssh-args) ++ ["-t" $"journalctl --user -u (gateway-unit) -f"]) } def "main ctl restart" [] { ctl-gssh $"systemctl --user restart (gateway-unit)" } def "main ctl units" [] { ctl-gssh "systemctl --user list-units 'hermes*' --all --no-pager" } def "main ctl reseed" [] { warn "This overwrites SOUL.md, memories and config.yaml in the guest" warn "with the seed from /etc/nixos/hermes. Anything Hermes has" warn "learned and written there is lost." if not (confirm-word "reseed" "type 'reseed' to confirm:") { die "aborted" } ctl-gssh $"cp -rT /etc/nixos/hermes/home (home-dir) && cp -rT /etc/nixos/hermes/workspace /var/lib/hermes/workspace && systemctl --user restart (gateway-unit)" ok "reseeded" } def "main ctl pull" [dest: string = "./hermes-home"] { mkdir $dest ( ctl-gssh $"tar -C (home-dir) --exclude=.env --exclude=sessions --exclude=state.db -cf - ." ) | ^tar -C $dest -xf - say $"pulled to ($dest) (secrets and sessions excluded)" }