Files
nix-config/utils.nu
T
ryan4yin | 二花 7cedea6161 feat(upload-vm): write the image into the VM's own disk (#285)
The VMs now declare their root disk as a DataVolume with `source: upload`, so
upload straight into `<vm>-disk` (CDI only accepts an upload into a DataVolume)
instead of a shared golden PVC. Takes the disk size as an argument.
2026-09-21 13:42:54 +08:00

104 lines
3.3 KiB
Nu

def repeat-str [s: string, n: int] {
(1..$n | each { $s } | str join)
}
# ================= NixOS related =========================
export def nixos-switch [
name: string
mode: string
verbosity: string
] {
if $mode not-in ["switch" "boot"] {
error make { msg: $"unsupported deployment mode '($mode)'; expected switch or boot" }
}
if $verbosity not-in ["normal" "debug"] {
error make { msg: $"unsupported verbosity '($verbosity)'; expected normal or debug" }
}
print $"nixos-switch '($name)' in '($mode)' mode with '($verbosity)' verbosity..."
print (repeat-str "=" 50)
if $verbosity == "debug" {
# show details via nix-output-monitor
nom build $".#nixosConfigurations.($name).config.system.build.toplevel" --accept-flake-config --show-trace --verbose
nixos-rebuild $mode --sudo --flake $".#($name)" --accept-flake-config --show-trace --verbose
} else {
nixos-rebuild $mode --sudo --flake $".#($name)" --accept-flake-config
}
}
# ====================== Misc =============================
export def make-editable [
path: string
] {
print (repeat-str "=" 50)
let tmpdir = (mktemp -d)
rsync -avz --copy-links $"($path)/" $tmpdir
rsync -avz --copy-links --chmod=D2755,F744 $"($tmpdir)/" $path
}
# ================= macOS related =========================
export def darwin-build [
name: string
verbosity: string
] {
print $"darwin-build '($name)' with '($verbosity)' verbosity..."
print (repeat-str "=" 50)
let target = $".#darwinConfigurations.($name).system"
if "debug" == $verbosity {
nom build $target --extra-experimental-features "nix-command flakes" --show-trace --verbose
} else {
nix build $target --extra-experimental-features "nix-command flakes"
}
}
export def darwin-switch [
name: string
verbosity: string
] {
print $"darwin-switch '($name)' with '($verbosity)' verbosity..."
print (repeat-str "=" 50)
if "debug" == $verbosity {
sudo -E ./result/sw/bin/darwin-rebuild switch --flake $".#($name)" --show-trace --verbose
} else {
sudo -E ./result/sw/bin/darwin-rebuild switch --flake $".#($name)"
}
}
export def darwin-rollback [] {
./result/sw/bin/darwin-rebuild --rollback
}
# ==================== Virtual Machines related =====================
# Build and upload a VM image
export def upload-vm [
name: string
size: string
verbosity: string
] {
print $"upload-vm '($name)' with '($verbosity)' verbosity..."
print (repeat-str "=" 50)
let target = $".#($name)"
if "debug" == $verbosity {
nom build $target --show-trace --verbose
} else {
nix build $target
}
# Write the freshly built image straight into the VM's own root disk (a
# DataVolume with `source: upload` in k8s-gitops). CDI only accepts an
# upload into a DataVolume, and this way the disk is populated once, on the
# node the VM is pinned to.
#
# Requires the CDI upload proxy to be reachable from here; it is exposed
# as a NodePort by the cluster (see infra/configs/base/kubevirt).
let uploadProxyUrl = "https://192.168.5.181:30443"
let image = (glob result/nixos-image-*.qcow2 | first)
virtctl image-upload dv $"($name)-disk" -n vms --size $size --image-path $image --uploadproxy-url $uploadProxyUrl --insecure
}