From 4e35ef641c9bca1f63c3248877295fe61d4c1d20 Mon Sep 17 00:00:00 2001 From: tv Date: Sat, 21 Nov 2026 02:32:31 +0000 Subject: run-firecracker-vm: nix as a layer, a read-only store without it The base system had nix, the nix-daemon, the host's store and nixpkgs via 9P, and a writable store on a drive, a scratch file without --store. Now all of that is in a layer of its own, layers/nix.nix, and the base system has neither nix nor nix-daemon in its closure: its store is its images, read-only, an overlay of them, or with a single image, the image itself, as overlayfs needs at least two lower layers without an upper one. This can become the base layer for guests that only run services. As a layer, nix gets an image of its own: put first, it's the same for all guests with nix, instead of in the image of whichever layer wants nix. It sets fc.nix, which the run script goes by, and nix.enable; setting nix.enable without it fails an assertion, as the run script wouldn't give the guest the drive for its store. The image decides, so the run script checks that the options fit it: a guest with nix requires --store (no more scratch store), and one without refuses --store, --store-size, --nixpkgs and --nixpkgs-cache. Without the rw-store drive, the images come first, and home after them; the store and nixpkgs aren't served, there's no fc.registration, and the initrd doesn't mount anything via 9P. The run script now waits for each of the 9P servers it starts, not only the store's. --- layers/nix.nix | 120 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 120 insertions(+) create mode 100644 layers/nix.nix (limited to 'layers') diff --git a/layers/nix.nix b/layers/nix.nix new file mode 100644 index 0000000..8e00d13 --- /dev/null +++ b/layers/nix.nix @@ -0,0 +1,120 @@ +# nix, as a layer: put it into the layers, before the ones that need it, +# for an image of its own, the same for all of them: +# --arg layers '[ layers/nix.nix ./my-layer.nix ]' +# Without it, the guest has neither nix nor nix-daemon, and its store is +# its images, read-only. With it, the run script requires --store. +{ config, lib, pkgs, ... }: +let + inherit (config.fileSystems."/nix/store".overlay) lowerdir; +in +{ + nix.enable = true; + fc.nix = true; + + # The store, on top of the guest's own closure from images (which reading + # is much faster than reading the host's store): the host's store, + # read-only via 9P over vsock (see mount.vsock9p and the run script), + # for anything else, like nixpkgs; then the first drive on top, so nix + # can add to the store (one that persists, see the run script). The + # host's store doesn't change under a path, so 9P may cache freely. + # Store paths don't collide, so the order of the lower layers doesn't + # matter. + fileSystems."/nix/.host-store" = { + device = "565"; + fsType = "vsock9p"; + options = [ "ro" "cache=loose" ]; + noCheck = true; + neededForBoot = true; + }; + fileSystems."/nix/.rw-store" = { + device = "/dev/vda"; # the first drive, see the run script + fsType = "ext4"; + options = [ "noatime" ]; + noCheck = true; + neededForBoot = true; + }; + # nix's database goes there, too, so it matches what's in the store. + fileSystems."/nix/var" = { + device = "/nix/.rw-store/var"; + fsType = "none"; + options = [ "bind" ]; + depends = [ "/nix/.rw-store" ]; + }; + systemd.services.nix-var-dir = { + requiredBy = [ "nix-var.mount" ]; + before = [ "nix-var.mount" ]; + unitConfig.DefaultDependencies = false; + unitConfig.RequiresMountsFor = "/nix/.rw-store"; + serviceConfig.Type = "oneshot"; + serviceConfig.ExecStart = "${pkgs.coreutils}/bin/mkdir -p -m 0755 /nix/.rw-store/var"; + }; + # The images are an overlay already: this is one image more than the + # base system, so there are at least two. + fileSystems."/nix/store".overlay = { + lowerdir = [ "/nix/.host-store" ]; + upperdir = "/nix/.rw-store/store"; + workdir = "/nix/.rw-store/work"; + }; + # nix in the guest deletes a path in the store that isn't in its + # database before it adds it, like the host's store's paths. On the + # overlay, deleting a lower layer's path leaves a whiteout in the rw-store, + # or an opaque directory, which is what nix adds again, if it gets that + # far. With a persistent rw-store, that hides the path from any + # later image, too. Any path that a lower layer has is complete + # there, and the same as anything of that name in the rw-store, so + # drop such paths from the rw-store before mounting the overlay. + boot.initrd.systemd.services.fc-rw-store-unshadow = { + requiredBy = [ "sysroot-nix-store.mount" ]; + before = [ "sysroot-nix-store.mount" ]; + unitConfig.DefaultDependencies = false; + unitConfig.RequiresMountsFor = map (d: "/sysroot" + d) ([ "/nix/.rw-store" ] ++ lowerdir); + serviceConfig.Type = "oneshot"; + script = '' + cd /sysroot/nix/.rw-store/store 2>/dev/null || exit 0 + for p in *; do + for d in ${toString (map (d: "/sysroot" + d) lowerdir)}; do + if test -e "$d/$p"; then + rm -rf "$p" + break + fi + done + done + ''; + }; + # For the host's store, in the initrd. + boot.initrd.systemd.extraBin."mount.vsock9p" = config.fc.mountVsock9p; + boot.initrd.systemd.storePaths = [ pkgs.socat pkgs.util-linux ]; + + # 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" ]; + before = [ "nix-daemon.service" ]; + serviceConfig.Type = "oneshot"; + serviceConfig.RemainAfterExit = true; + # With a persistent store, paths from an earlier image or host + # store may be gone; --verify removes them from the database. + script = '' + for arg in $(cat /proc/cmdline); do + case $arg in fc.registration=*) + ${config.nix.package.out}/bin/nix-store --load-db < "''${arg#*=}" + esac + done + ${config.nix.package.out}/bin/nix-store --verify + ''; + }; + # 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" ]; + }; +} -- cgit v1.3.1