Commit 8f67c0b96972

Vincent Demeester <vincent@sbr.pm>
2026-02-02 09:05:25
feat(microvm): add on-demand VM startup via SSH
- Add microvm-ssh script that starts VM if not running - Update SSH config to use ProxyCommand for on-demand startup - VMs are automatically started when SSH'ing to them Usage: ssh claude-home (starts VM if needed, then connects) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent 5987a73
Changed files (2)
home
common
pkgs
my
scripts
home/common/shell/openssh.nix
@@ -180,11 +180,12 @@ in
             };
           };
         }
-        # Generated microvm SSH aliases
-        // (lib.mapAttrs (_name: vm: {
+        # Generated microvm SSH aliases (with on-demand VM startup)
+        // (lib.mapAttrs (name: vm: {
           hostname = vm.ip;
           user = "vincent";
-          proxyJump = globals.microvms.host;
+          # Use ProxyCommand for on-demand VM startup instead of ProxyJump
+          proxyCommand = "microvm-ssh ${name} ${vm.ip} %p";
           identitiesOnly = true;
           identityFile = lib.mkIf hasFido2Keys "~/.ssh/id_homelab_sk";
           extraOptions = {
pkgs/my/scripts/bin/microvm-ssh
@@ -0,0 +1,39 @@
+#!/usr/bin/env bash
+# Start microvm on demand and connect via SSH
+# Usage: microvm-ssh <vm-name> <host> <port>
+#
+# This script is used as SSH ProxyCommand to:
+# 1. Start the microvm if not running (via aomi)
+# 2. Wait for SSH to become available
+# 3. Connect using nc/socat
+
+set -euo pipefail
+
+VM_NAME="${1:-}"
+HOST="${2:-}"
+PORT="${3:-22}"
+JUMP_HOST="${MICROVM_JUMP_HOST:-aomi.sbr.pm}"
+
+if [[ -z "$VM_NAME" ]] || [[ -z "$HOST" ]]; then
+    echo "Usage: microvm-ssh <vm-name> <host> [port]" >&2
+    exit 1
+fi
+
+# Check if VM is running, start if not
+# This runs on the jump host (aomi)
+ssh -q "$JUMP_HOST" "
+    if ! systemctl is-active --quiet microvm@${VM_NAME}; then
+        echo 'Starting microvm@${VM_NAME}...' >&2
+        sudo systemctl start microvm@${VM_NAME}
+        # Wait for VM to boot and SSH to be available
+        for i in {1..30}; do
+            if nc -z ${HOST} ${PORT} 2>/dev/null; then
+                break
+            fi
+            sleep 1
+        done
+    fi
+"
+
+# Connect through the jump host
+exec ssh -q -W "${HOST}:${PORT}" "$JUMP_HOST"