diff options
| author | tv <tv@krebsco.de> | 2026-11-23 00:54:02 +0000 |
|---|---|---|
| committer | tv <tv@krebsco.de> | 2026-11-23 01:08:31 +0000 |
| commit | 86561dbdd2a91b1d0bc5e3f9fa1fab5331ccd3ec (patch) | |
| tree | e660a534047aaa5287fa0b065d09199a240d7d6a | |
| parent | 47c370f495a4b092fdca05f25683d2f7024e89fb (diff) | |
layers/switch.nix: switching a running guest, for development
Every change to a guest meant a new image and a reboot. For a faster
development loop, a guest with nix can now switch to a new build of its
system instead: layers/switch.nix, imported into a layer, turns the
switch on (the base system keeps it off, as a default now), and the run
script's derivation has the system, so the host builds it with
nix-build -A toplevel and runs its switch-to-configuration in the guest,
which sees the host's store, where it got built (tested: a path added on
the host shows up in the running guest). A switched guest runs what it
didn't boot, so for production, it's still a reboot with the new run
script. The kernel, its command line, Firecracker's configuration, new
secrets, and adding or removing layers need one anyway.
A path the guest looked for before the host had it stays missing, as the
store's overlay remembers that (tested), until the guest drops its
caches, which takes seconds, so that's documented, not done every time:
nothing in the guest looks for paths it doesn't have, as a rule.
| -rw-r--r-- | README.md | 26 | ||||
| -rw-r--r-- | layers/switch.nix | 25 | ||||
| -rw-r--r-- | pkgs/run-firecracker-vm/default.nix | 7 |
3 files changed, 56 insertions, 2 deletions
@@ -124,6 +124,31 @@ from the store, so either can be kept or thrown away): ./result/bin/run-firecracker-vm --home ~/fc-home.img [--home-size 50G] ``` +For a faster development loop, a guest with nix can switch to a new +build of its system without a reboot, with `layers/switch.nix` imported +into a layer. Build the system with the same arguments as the guest, +and switch to it: + +```sh +sys=$(nix-build --no-out-link pkgs/run-firecracker-vm -A toplevel --arg layers '[ layers/nix.nix ./my-service.nix ]') +ssh root@VM $sys/bin/switch-to-configuration test +``` + +The guest reads the new system from the host's store until it reboots. +It's no longer what it booted, so for production, reboot it with the new +run script instead. The kernel, its command line, Firecracker's +configuration, new secrets, and adding or removing layers need a reboot +anyway. + +A store path that the guest looked for before the host had it (say, a +switch to a system that was still being built) stays missing in the +guest's store, as the overlay remembers that it wasn't there, until the +guest drops its caches (which takes seconds), as root: + +```sh +echo 2 > /proc/sys/vm/drop_caches +``` + A layer that keeps its state in `/home` can make that disk mandatory, so the state doesn't silently end up on the tmpfs: @@ -299,6 +324,7 @@ the list its own VM uses. - `base/systemd.nix`: a slimmer systemd, for `base`. - `layers/nix.nix`: nix, as a layer. - `layers/console.nix`: a shell on the serial console, to import into a layer. +- `layers/switch.nix`: switching a running guest, to import into a layer. - `modules/host/default.nix`: the host's side: network, DHCP/DNS, egress proxy, `fc-egress`. - `modules/host/egress.lua`: the egress policy, for the proxy (OpenResty). diff --git a/layers/switch.nix b/layers/switch.nix new file mode 100644 index 0000000..d46a187 --- /dev/null +++ b/layers/switch.nix @@ -0,0 +1,25 @@ +# Switching a running guest to a new build of its system, instead of +# rebooting it, for a faster development loop. Not for production: a +# switched guest runs what it didn't boot (/run/current-system isn't +# /run/booted-system), from the host's store; reboot it with the new run +# script, whose images have the new system. Needs layers/nix.nix (the +# guest sees the host's store, where the new system gets built). Small, +# so better imported by another layer: +# imports = [ /path/to/firecracker/layers/switch.nix ]; +# Then, on the host, with the same arguments as the running guest's build: +# sys=$(nix-build --no-out-link pkgs/run-firecracker-vm -A toplevel ...) +# ssh root@VM $sys/bin/switch-to-configuration test +# A store path that the guest looked for before the host had it stays +# missing in the guest's store (the overlay remembers), until the guest +# drops its caches: echo 2 > /proc/sys/vm/drop_caches (takes seconds). +# What needs a reboot anyway: the kernel, its command line, the initrd, +# Firecracker's configuration (memory, vCPUs), new secrets, and layers +# added or removed (the store's overlay would change). +{ config, ... }: +{ + system.switch.enable = true; + assertions = [{ + assertion = config.fc.nix; + message = "layers/switch.nix needs layers/nix.nix, for the host's store."; + }]; +} diff --git a/pkgs/run-firecracker-vm/default.nix b/pkgs/run-firecracker-vm/default.nix index 19a6c9e..63cb06a 100644 --- a/pkgs/run-firecracker-vm/default.nix +++ b/pkgs/run-firecracker-vm/default.nix @@ -307,7 +307,7 @@ let services.resolved.enable = false; services.timesyncd.enable = false; system.nssModules = lib.mkForce [ ]; - system.switch.enable = false; + system.switch.enable = lib.mkDefault false; # see layers/switch.nix system.disableInstallerTools = true; # nixos-rebuild & co., i.e. python systemd.coredump.enable = false; systemd.oomd.enable = false; @@ -821,4 +821,7 @@ pkgs.writeShellScriptBin "run-firecracker-vm" /* sh */ '' firecracker --no-api --config-file "$tmp"/config.json || status=$? exit $status -'' +'' // { + # The guest's system, for switching a running guest (see layers/switch.nix). + inherit toplevel; +} |
