From 58827c3bee1fedeb5f471328b60adea420339a2f Mon Sep 17 00:00:00 2001 From: tv Date: Wed, 25 Nov 2026 12:27:15 +0000 Subject: run-firecracker-vm: the guest drops its caches when nixpkgs's commit changes With --nixpkgs-cache (loose: 7s for nix-instantiate '' -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. --- pkgs/run-firecracker-vm/default.nix | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) (limited to 'pkgs/run-firecracker-vm/default.nix') 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 -- cgit v1.3.1