Files
2026-10-09 14:35:12 -04:00

97 lines
3.5 KiB
Nu

def disk-base-dir [] {
$env.HERMES_DISK_BASE_DIR? | default "/var/lib/hermes-vm"
}
def snap-dir [] { $"(disk-base-dir)/snapshots" }
# Single string, not rest args: nu matches bare --flags on this command first.
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 <subcommand>"
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 <snap> 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 }
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'
}
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
}
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
}