diff options
| author | tv <tv@krebsco.de> | 2026-11-25 12:27:15 +0000 |
|---|---|---|
| committer | tv <tv@krebsco.de> | 2026-11-25 12:27:25 +0000 |
| commit | 58827c3bee1fedeb5f471328b60adea420339a2f (patch) | |
| tree | ddda66da0b4ced4ba98411d3c9038eeb9d34cacc | |
| parent | 39012ddac260255a60bbf99777e9cf1ba9bb219b (diff) | |
run-firecracker-vm: the guest drops its caches when nixpkgs's commit changes
With --nixpkgs-cache (loose: 7s for nix-instantiate '<nixpkgs>' -A
hello, against 20s without), the guest doesn't see changes to the host's
nixpkgs until it drops its caches. When nixpkgs is a Git checkout, the
run script now checks its commit every 5 seconds, and on a change
(checkout, pull, rebase), connects to the guest's vsock port 1235, where
layers/nix.nix has fc-drop-caches run. Changes without a commit still
need fc-drop-caches by hand.
| -rw-r--r-- | README.md | 6 | ||||
| -rw-r--r-- | layers/nix.nix | 14 | ||||
| -rw-r--r-- | pkgs/run-firecracker-vm/default.nix | 24 |
3 files changed, 41 insertions, 3 deletions
@@ -165,7 +165,11 @@ With nix, `/nixpkgs` is the nixpkgs the guest was built from not cached in the guest, so changes to it show up right away, but evaluating is slow; `--nixpkgs-cache=loose` caches it (several times faster, but changes may not show up while the guest runs). Other modes: -`mmap`, `readahead`, `none` (the default). +`mmap`, `readahead`, `none` (the default). With a cache mode, and a Git +checkout, the run script tells the guest to drop its caches whenever the +checkout's commit changes. For other changes, and in the rare case +that the guest looked for a store path before the host had it, anyone +in the guest can drop them: `fc-drop-caches` (it takes seconds). The guest gets 2 CPUs and 1024 MiB unless told otherwise (`--vcpus N`, 1 to 32; `--mem-size MIB`). diff --git a/layers/nix.nix b/layers/nix.nix index 42d4729..02c37ac 100644 --- a/layers/nix.nix +++ b/layers/nix.nix @@ -145,4 +145,18 @@ in } ''); }; + # And the run script has it done via vsock port 1235 when the host's + # nixpkgs checkout changes its commit (with --nixpkgs-cache). + systemd.sockets.fc-drop-caches = { + wantedBy = [ "sockets.target" ]; + socketConfig = { + ListenStream = "vsock::1235"; + Accept = true; + MaxConnections = 1; + }; + }; + systemd.services."fc-drop-caches@" = { + serviceConfig.StandardInput = "socket"; + script = "/run/wrappers/bin/fc-drop-caches"; + }; } diff --git a/pkgs/run-firecracker-vm/default.nix b/pkgs/run-firecracker-vm/default.nix index 11dfa46..23507b2 100644 --- a/pkgs/run-firecracker-vm/default.nix +++ b/pkgs/run-firecracker-vm/default.nix @@ -550,7 +550,7 @@ pkgs.writeShellScriptBin "run-firecracker-vm" /* sh */ '' # restarts the guest on reboot (leave it with poweroff instead), and # takes no commas in --store and --home. set -efu - export PATH=${lib.makeBinPath [ pkgs.coreutils pkgs.e2fsprogs pkgs.findutils pkgs.gnugrep pkgs.gnused pkgs.jq pkgs.openssh pkgs.util-linux pkgs.firecracker pkgs.cloud-hypervisor pkgs.unpfs ]} + export PATH=${lib.makeBinPath [ pkgs.coreutils pkgs.e2fsprogs pkgs.findutils pkgs.gnugrep pkgs.gnused pkgs.jq pkgs.openssh pkgs.util-linux pkgs.firecracker pkgs.cloud-hypervisor pkgs.git pkgs.socat pkgs.unpfs ]} usage() { echo "usage: $0 [--store FILE] [--store-size SIZE] [--home FILE] [--home-size SIZE] [--mem-size MIB] [--nixpkgs DIR] [--nixpkgs-cache MODE] [--egress LIST] [--name NAME] [--vcpus N] [--secret NAME=FILE]... [--vmm firecracker|cloud-hypervisor]" @@ -566,6 +566,7 @@ pkgs.writeShellScriptBin "run-firecracker-vm" /* sh */ '' vcpus=2 vmm=firecracker nixpkgs=${lib.escapeShellArg (toString pkgs.path)} + nixpkgs_cache= nix_opt= # the last option that's only for a guest with nix secrets=() # NAME=FILE, as given while test $# -gt 0; do @@ -577,7 +578,7 @@ pkgs.writeShellScriptBin "run-firecracker-vm" /* sh */ '' --home-size) home_size=$2; shift 2 ;; --mem-size) mem_size=$2; shift 2 ;; --nixpkgs) nixpkgs=$2; nix_opt=$1; shift 2 ;; - --nixpkgs-cache) args="$args fc.9p566=cache=$2"; nix_opt=$1; shift 2 ;; + --nixpkgs-cache) args="$args fc.9p566=cache=$2"; nixpkgs_cache=$2; nix_opt=$1; shift 2 ;; --egress) egress=$2; shift 2 ;; --name) name=$2; shift 2 ;; --vcpus) vcpus=$2; shift 2 ;; @@ -831,6 +832,25 @@ pkgs.writeShellScriptBin "run-firecracker-vm" /* sh */ '' ${lib.optionalString nix '' serve 565 /nix/store serve 566 "$nixpkgs" + # With a cache mode for nixpkgs, the guest doesn't see changes to it + # (see fc-drop-caches in layers/nix.nix). When it's a Git checkout + # whose commit changes (checkout, pull, rebase), the guest gets told + # to drop its caches: a connection to its vsock port 1235 (on the + # socket uds_path, with CONNECT PORT). Changes without a commit + # need fc-drop-caches by hand. + if test -n "$nixpkgs_cache" && git -C "$nixpkgs" rev-parse HEAD > /dev/null 2>&1; then + ( + head=$(git -C "$nixpkgs" rev-parse HEAD) + while sleep 5; do + new=$(git -C "$nixpkgs" rev-parse HEAD 2>/dev/null) || continue + if test "$new" != "$head"; then + printf 'CONNECT 1235\n' | socat -t 10 - UNIX-CONNECT:"$tmp/vsock\:" > /dev/null 2>&1 || : + head=$new + fi + done + ) & + servers="$servers $!" + fi ''} trap 'kill $servers; rm -rf "$tmp"; if test -n "$vm"; then : > "$vm"; if test -w "$vm.name"; then : > "$vm.name"; fi; fi' EXIT for port in $ports; do |
