Files
hermenix/nu/lib/common.nu
T
2026-09-17 16:53:00 -04:00

90 lines
3.2 KiB
Nu

# hermenix — shared helpers for the host-side nushell tools (hermenix,
# hermes-vm, hermes-ctl).
#
# Not a standalone script: nu can't reliably resolve relative `use` imports
# once a script has been copied into the Nix store as a single wrapped
# binary, so the flake concatenates this file with each tool's own script
# instead (see flake.nix / modules/host.nix). Keep this file free of a
# `main` def and of anything specific to one tool.
# ---------------------------------------------------------------- output ---
def say [msg: string] { print $" ($msg)" }
def note [msg: string] { print $" (ansi dark_gray)($msg)(ansi reset)" }
def ok [msg: string] { print $" (ansi green)✓(ansi reset) ($msg)" }
def warn [msg: string] { print $" (ansi yellow)!(ansi reset) ($msg)" }
def die [msg: string] {
print -e $"\n(ansi red)error:(ansi reset) ($msg)"
exit 1
}
def step [n: int, title: string] { print $"\n(ansi attr_bold)== ($n). ($title)(ansi reset)" }
# Read a line of user input. On a real terminal this prompts interactively
# via `input`. When stdin isn't a terminal (e.g. piped in from a test or
# script), `input` would otherwise try to read from the controlling terminal
# and hang forever waiting for a keystroke that never comes — so read the
# piped stdin directly instead.
def read-line [prompt: string] {
if (is-terminal --stdin) {
input $prompt
} else {
print -n $prompt
(
^cat
| lines
| first
| str trim
)
}
}
def ask [prompt: string, default: string = ""] {
let suffix = if $default != "" { $" [($default)]" } else { "" }
let reply = (read-line $" ($prompt)($suffix): ")
if $reply == "" { $default } else { $reply }
}
def confirm [prompt: string] {
let reply = (read-line $" ($prompt) [y/N] ")
(($reply | str starts-with "y") or ($reply | str starts-with "Y"))
}
def confirm-word [word: string, prompt: string] {
(read-line $" ($prompt) ") == $word
}
# Single-quote a string for safe embedding in a remote shell command line.
def shquote [s: string] {
"'" + ($s | str replace --all "'" "'\\''") + "'"
}
# ------------------------------------------------------------------- ssh ---
# argv for reaching the guest over vsock — no inbound network path needed.
# Shared by every tool that talks to the guest (hermenix migrate, hermes-vm,
# hermes-ctl).
def vsock-ssh-args [vsock_cid: int, guest_user: string, guest_host: string] {
[
"ssh"
"-o" $"ProxyCommand=socat - VSOCK-CONNECT:($vsock_cid):22"
"-o" "StrictHostKeyChecking=accept-new"
$"($guest_user)@($guest_host)"
]
}
# Run an argv list (e.g. from vsock-ssh-args, with trailing command words
# appended) as an external command.
def run-argv [argv: list<string>] {
^($argv | first) ...($argv | skip 1)
}
# --------------------------------------------------------------- version ---
# HERMENIX_VERSION is baked in at build time (flake.nix / modules/host.nix)
# as "YYYY.MM.DD.hh.mm-<commit-sha>" from this repo's own git metadata. "dev"
# is only seen running an unwrapped checkout of the script directly.
def hermenix-version [] {
$env.HERMENIX_VERSION? | default "dev"
}
def "main version" [] { print (hermenix-version) }