diff options
| author | tv <tv@krebsco.de> | 2026-11-16 21:17:41 +0000 |
|---|---|---|
| committer | tv <tv@krebsco.de> | 2026-11-16 22:18:46 +0100 |
| commit | c7e2c1e6043fd942c02a6d0b9bbbca13554b7c38 (patch) | |
| tree | 5b2de046caad48e5a478719ec87365f02e86a363 | |
| parent | 8776ce858ef5b0b7578a6cc9734a1d04787fb1b7 (diff) | |
give the guest nix
/nix/store becomes an overlay of the image and a tmpfs, so nix can add
to it. The run script puts the registration of the image's paths into
the image, and the guest loads it before nix-daemon starts; root goes
through the daemon too, so that happens on first use, not at boot.
NIX_PATH has the host's nixpkgs, for nix-shell -p.
| -rw-r--r-- | default.nix | 45 |
1 files changed, 38 insertions, 7 deletions
diff --git a/default.nix b/default.nix index d553c73..045d843 100644 --- a/default.nix +++ b/default.nix @@ -8,7 +8,7 @@ let nixos = import (pkgs.path + "/nixos") { inherit (pkgs.stdenv.hostPlatform) system; - configuration = { modulesPath, ... }: { + configuration = { config, modulesPath, ... }: { imports = [ (modulesPath + "/profiles/minimal.nix") (modulesPath + "/profiles/perlless.nix") @@ -22,7 +22,7 @@ let boot.kernelPackages = kernelPackages; boot.initrd.allowMissingModules = true; # e.g. ahci, efivarfs with a cut-down kernel - boot.initrd.kernelModules = [ "virtio_mmio" "virtio_blk" "erofs" ]; + boot.initrd.kernelModules = [ "virtio_mmio" "virtio_blk" "erofs" "overlay" ]; boot.kernelParams = [ "console=ttyS0" "reboot=k" "panic=1" "8250.nr_uarts=1" # only ttyS0 exists (stops 0x2f8/0x3e8/0x2e8 probing) @@ -35,10 +35,38 @@ let fsType = "tmpfs"; options = [ "mode=755" ]; }; - fileSystems."/nix/store" = { + # The store: the image (read-only) with a tmpfs on top, so nix can add + # to it. What it adds lives in RAM, and is gone after a reboot. + fileSystems."/nix/.ro-store" = { device = "/dev/vda"; fsType = "erofs"; options = [ "ro" "nodev" "nosuid" ]; + neededForBoot = true; + }; + fileSystems."/nix/.rw-store" = { + fsType = "tmpfs"; + options = [ "mode=0755" ]; + neededForBoot = true; + }; + fileSystems."/nix/store".overlay = { + lowerdir = [ "/nix/.ro-store" ]; + upperdir = "/nix/.rw-store/store"; + 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 image get + # registered (see the run script) before anything uses the store, + # and only once something does, not at boot. + nix.nixPath = [ "nixpkgs=${pkgs.path}" ]; + environment.variables.NIX_REMOTE = "daemon"; + systemd.services.nix-load-db = { + requiredBy = [ "nix-daemon.service" ]; + before = [ "nix-daemon.service" ]; + unitConfig.ConditionPathExists = "/nix/.ro-store/.registration"; + serviceConfig.Type = "oneshot"; + serviceConfig.RemainAfterExit = true; + script = "${config.nix.package.out}/bin/nix-store --load-db < /nix/.ro-store/.registration"; }; # /xchg: the host's ./xchg via 9P over vsock, see the run script, # mounted on first access. The kernel's 9P client can't do vsock @@ -87,7 +115,6 @@ let boot.initrd.systemd.tpm2.enable = false; boot.kexec.enable = false; networking.firewall.enable = false; - nix.enable = false; powerManagement.enable = false; programs.fuse.enable = false; programs.nano.enable = false; @@ -187,12 +214,16 @@ pkgs.writeShellScriptBin "run-firecracker-vm" /* sh */ '' mkdir -p "$cache" nix-store -qR ${toplevel} "$@" > "$tmp"/store-paths - img=$cache/$(sha256sum < "$tmp"/store-paths | cut -c-32).img + img=$cache/$({ echo v2; cat "$tmp"/store-paths; } | sha256sum | cut -c-32).img + # The image gets the paths' registration for the guest's nix database + # as .registration (nix ignores names starting with a dot). if ! test -e "$img"; then + nix-store --dump-db $(cat "$tmp"/store-paths) > "$tmp"/.registration + { cat "$tmp"/store-paths; echo "$tmp"/.registration; } | tar -c -P --verbatim-files-from \ - --transform 'flags=rSh;s|/nix/store/||' \ - --files-from "$tmp"/store-paths \ + --transform 'flags=rSh;s|^/nix/store/||;s|^.*/\.registration$|.registration|' \ + --files-from - \ | mkfs.erofs --quiet --tar=f --force-uid=0 --force-gid=0 --hard-dereference -T 0 "$img.$$" mv "$img.$$" "$img" find "$cache" -type f ! -path "$img" -delete |
