From 9687fe329ccc88b13d3614a8d8d332a3ea3dcab9 Mon Sep 17 00:00:00 2001 From: tv Date: Mon, 16 Nov 2026 23:58:26 +0000 Subject: serve nixpkgs to the guest via 9P instead of from the store A outside the store got copied (i.e. hashed) to the store on every evaluation, for the guest's NIX_PATH, so no-op builds still took ~25s with a checkout. Now a third 9P server serves nixpkgs from wherever it is, read-only, at /nixpkgs in the guest, which NIX_PATH points to. It's in neither the closure nor the registration any more, and run-firecracker-vm --nixpkgs DIR can serve another one. --- pkgs/run-firecracker-vm/default.nix | 57 +++++++++++++++++++------------------ 1 file changed, 30 insertions(+), 27 deletions(-) (limited to 'pkgs/run-firecracker-vm') diff --git a/pkgs/run-firecracker-vm/default.nix b/pkgs/run-firecracker-vm/default.nix index 3ff3d0f..98b676b 100644 --- a/pkgs/run-firecracker-vm/default.nix +++ b/pkgs/run-firecracker-vm/default.nix @@ -103,15 +103,13 @@ let workdir = "/nix/.rw-store/work"; }; - # nix, with the host's nixpkgs for nix-shell -p and the like. Root - # goes through the daemon too, so the paths of the guest's closure get - # registered (fc.registration, see the run script) before anything - # uses the store, and only once something does, not at boot. The - # rest of the host's store is there, too, but not registered. - # Without the store reference, so nixpkgs stays out of the closure, - # and thereby out of the image. It gets registered, though, see - # fc.registration. - nix.nixPath = [ "nixpkgs=${builtins.unsafeDiscardStringContext nixpkgs}" ]; + # nix, with the host's nixpkgs for nix-shell -p and the like, at + # /nixpkgs (see below). Root goes through the daemon too, so the + # paths of the guest's closure get registered (fc.registration, see + # the run script) before anything uses the store, and only once + # something does, not at boot. The rest of the host's store is + # there, too, but not registered. + nix.nixPath = [ "nixpkgs=/nixpkgs" ]; environment.variables.NIX_REMOTE = "daemon"; systemd.services.nix-load-db = { requiredBy = [ "nix-daemon.service" ]; @@ -135,6 +133,14 @@ let noCheck = true; options = [ "x-systemd.automount" "nofail" ]; }; + # The host's nixpkgs, wherever it is (see the run script): no copy + # to the store needed, which is what made evaluating this take ~20s. + fileSystems."/nixpkgs" = { + device = "566"; + fsType = "vsock9p"; + noCheck = true; + options = [ "ro" "x-systemd.automount" "nofail" ]; + }; system.fsPackages = [ (pkgs.runCommand "mount.vsock9p" { } "install -D ${mountVsock9p} $out/bin/mount.vsock9p") ]; @@ -240,16 +246,7 @@ let inherit (nixos.config.system.build) toplevel kernel initialRamdisk; - # The host's nixpkgs, for the guest's NIX_PATH. builtins.storePath if - # it's in the store already, as "${pkgs.path}" would copy it to the - # store anyway, hashing all of nixpkgs on every evaluation (~20s). - nixpkgs = - if lib.isStorePath (toString pkgs.path) - then builtins.storePath (toString pkgs.path) - else "${pkgs.path}"; - - # The guest's closure, as erofs image. (Without nixpkgs, which made it - # 7 GiB, and comes from the host's store instead.) + # The guest's closure, as erofs image. storeImage = pkgs.runCommand "firecracker-store.img" { nativeBuildInputs = [ pkgs.erofs-utils pkgs.gnutar ]; closure = pkgs.closureInfo { rootPaths = [ toplevel ]; }; @@ -266,7 +263,7 @@ let initrd_path = "${initialRamdisk}/${nixos.config.system.boot.loader.initrdFile}"; boot_args = toString (nixos.config.boot.kernelParams ++ [ "init=${toplevel}/init" - "fc.registration=${pkgs.closureInfo { rootPaths = [ toplevel nixpkgs ]; }}/registration" + "fc.registration=${pkgs.closureInfo { rootPaths = [ toplevel ]; }}/registration" "@TTY_ARGS@" ]); }; @@ -297,10 +294,12 @@ let }); in pkgs.writeShellScriptBin "run-firecracker-vm" /* sh */ '' - # usage: run-firecracker-vm [--store FILE] [--store-size SIZE] + # usage: run-firecracker-vm [--store FILE] [--store-size SIZE] [--nixpkgs DIR] # The guest gets the host's store, read-only, via 9P over vsock, and the # directory xchg (in the current working directory) at /xchg, the same - # way. VMs started in the same directory share it. + # way. VMs started in the same directory share it. Also the same way, + # nixpkgs at /nixpkgs, read-only: DIR, by default the nixpkgs the guest + # was built from. # What nix in the guest adds to the store goes to a scratch file, removed # when the guest exits, or with --store, to FILE, which keeps it across # boots (created if it doesn't exist, of --store-size, default 16G; the @@ -309,14 +308,16 @@ pkgs.writeShellScriptBin "run-firecracker-vm" /* sh */ '' export PATH=${lib.makeBinPath [ pkgs.coreutils pkgs.e2fsprogs pkgs.findutils pkgs.gnused pkgs.util-linux pkgs.firecracker pkgs.unpfs ]} usage() { - echo "usage: $0 [--store FILE] [--store-size SIZE]" + echo "usage: $0 [--store FILE] [--store-size SIZE] [--nixpkgs DIR]" } rw_store= rw_store_size=16G + nixpkgs=${lib.escapeShellArg (toString pkgs.path)} while test $# -gt 0; do case $1 in --store) rw_store=$2; shift 2 ;; --store-size) rw_store_size=$2; shift 2 ;; + --nixpkgs) nixpkgs=$2; shift 2 ;; -h|--help) usage; exit ;; *) usage >&2; exit 1 esac @@ -370,9 +371,9 @@ pkgs.writeShellScriptBin "run-firecracker-vm" /* sh */ '' n=''${tap#fctap} mac=$(printf '06:00:ac:10:%02x:%02x' $((n >> 8 & 255)) $((n & 255))) - # The 9P servers, for the store and for /xchg, one each per VM. They - # run as the user running this script: xchg's files end up owned by the - # user, and the store is read-only to them. When the guest connects to + # The 9P servers, for the store, /xchg, and nixpkgs, one each per VM. + # They run as the user running this script: xchg's files end up owned by + # the user, and the store is read-only to them. When the guest connects to # vsock port PORT on the host (CID 2), Firecracker connects to the socket # uds_path_PORT. unpfs listens on ADDR:PORT (from unix!ADDR!PORT), hence # the colon at the end of uds_path. RUST_LOG=off silences its logging @@ -385,7 +386,9 @@ pkgs.writeShellScriptBin "run-firecracker-vm" /* sh */ '' store=$! RUST_LOG=off unpfs "unix!$tmp/vsock!_564" xchg > /dev/null & xchg=$! - trap 'kill $store $xchg; rm -rf "$tmp"' EXIT + RUST_LOG=off unpfs "unix!$tmp/vsock!_566" "$nixpkgs" > /dev/null & + nixpkgs_server=$! + trap 'kill $store $xchg $nixpkgs_server; rm -rf "$tmp"' EXIT while ! test -S "$vsock"_565; do sleep 0.01; done sed \ -- cgit v1.3.1