# hermenix vm — control the Hermes Agent VM itself. # # Start/stop, console, config inspection, snapshots and backups. # For driving Hermes the agent running inside the VM, see ctl/ctl.nu. # # Depends on: lib/common.nu, lib/guest.nu (ssh-args). def disk-base-dir [] { $env.HERMES_DISK_BASE_DIR? | default "/var/lib/hermes-vm" } def snap-dir [] { $"(disk-base-dir)/snapshots" } # Run a single already-fully-formed remote command string, or open an # interactive shell if none is given. A single string, not rest args: nu's # parser tries to match any bare --flag-shaped token against this command's # own flags before it ever reaches a rest parameter, so `hermenix vm ssh sudo # foo --bar` would fail to parse. Quote the whole remote command instead. def vm-gssh [cmd?: string] { let argv = if $cmd == null { ssh-args } else { (ssh-args) ++ [$cmd] } run-argv $argv } def "main vm" [] { print "usage: hermenix vm " print "" print " ssh [cmd] open a shell or run a single command in the guest" print " console follow the VM's journal output (journalctl -f)" print " start systemctl start hermes-vm" print " stop systemctl stop hermes-vm" print " restart systemctl restart hermes-vm" print " config show current generation, history and git status" print " config-pull [dir] copy the guest's /etc/nixos here for reading/diffing" print " snapshot trigger a state-disk snapshot now" print " snapshots list existing snapshots" print " rollback restore the state disk from a snapshot" print " backup trigger a restic backup now" print " backups list restic snapshots" } def "main vm ssh" [cmd?: string] { vm-gssh $cmd } def "main vm console" [] { ^journalctl -u hermes-vm -f -o cat } def "main vm start" [] { ^systemctl start hermes-vm } def "main vm stop" [] { ^systemctl stop hermes-vm } def "main vm restart" [] { ^systemctl restart hermes-vm } # What is the guest actually running right now? def "main vm config" [] { vm-gssh 'echo "== current generation =="; \ readlink -f /run/current-system; \ echo; echo "== generations =="; \ sudo nix-env --list-generations -p /nix/var/nix/profiles/system | tail -n 10; \ echo; echo "== /etc/nixos history =="; \ git -C /etc/nixos log --oneline -n 15; \ echo; echo "== uncommitted =="; \ git -C /etc/nixos status --short' } # Copy the guest's live NixOS config here for reading or diffing. def "main vm config-pull" [dest: string = "./hermes-etc-nixos"] { mkdir $dest (vm-gssh "tar -C /etc/nixos -cf - .") | ^tar -C $dest -xf - say $"pulled to ($dest)" } def "main vm snapshot" [] { ^systemctl start hermes-vm-snapshot } def "main vm snapshots" [] { let entries = (glob $"(snap-dir)/state-*" | where {|p| ($p | path type) == "dir" }) if ($entries | is-empty) { say "no snapshots" return } $entries | each {|p| { snapshot: ($p | path basename) size: (du $p | get physical.0) created: (ls -D $p | get modified.0) } } | sort-by created | print } # Restore the state disk from a snapshot. The system disk is left alone — # it holds nothing worth restoring. def "main vm rollback" [snapshot: string] { let snap = $"(snap-dir)/($snapshot)" if not ($snap | path exists) { die $"no such snapshot: ($snapshot)" } warn "Restores Hermes's identity, memories, workspace and NixOS" warn $"config to ($snapshot). Everything since is lost." if not (confirm-word "rollback" "type 'rollback' to confirm:") { die "aborted" } ^systemctl stop hermes-vm let base = (disk-base-dir) let stamp = (date now | format date "%s") ^mv $"($base)/state" $"($base)/state.old-($stamp)" ^btrfs subvolume snapshot $snap $"($base)/state" ^systemctl start hermes-vm ok "restored; previous state kept as state.old-*" } def "main vm backup" [] { ^systemctl start hermes-vm-backup } def "main vm backups" [] { let repo = (^systemctl show hermes-vm-backup -p Environment --value | str trim | split row " " | where {|e| $e | str starts-with "RESTIC_REPOSITORY=" } | get 0? | default "" | str replace "RESTIC_REPOSITORY=" "") ^restic -r $repo snapshots --tag hermes }