aboutsummaryrefslogtreecommitdiffstats
path: root/pkgs/run-firecracker-vm/default.nix
diff options
context:
space:
mode:
authortv <tv@krebsco.de>2026-11-25 12:27:15 +0000
committertv <tv@krebsco.de>2026-11-25 12:27:25 +0000
commit58827c3bee1fedeb5f471328b60adea420339a2f (patch)
treeddda66da0b4ced4ba98411d3c9038eeb9d34cacc /pkgs/run-firecracker-vm/default.nix
parent39012ddac260255a60bbf99777e9cf1ba9bb219b (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.
Diffstat (limited to 'pkgs/run-firecracker-vm/default.nix')
-rw-r--r--pkgs/run-firecracker-vm/default.nix24
1 files changed, 22 insertions, 2 deletions
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