aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authortv <tv@krebsco.de>2026-11-16 22:38:07 +0000
committertv <tv@krebsco.de>2026-11-16 23:39:13 +0100
commit3f1c4b1c43012cd7b38ddc18eec9613e37edd63e (patch)
tree5f545636505928965ee65140af97ebdd0f5ef31a
parent2af6a48f296aa24e1a6e8670e4f5784fe28c9ea4 (diff)
put the guest's closure back into an image, on top of the host's store
With the whole store via 9P, boot took twice as long: everything gets read over a single 9P channel. Now the guest's own closure comes from an erofs image, built by Nix from closureInfo (no more runtime image building or cache), as the top layer, with the host's store via 9P below it for everything else, like nixpkgs, which stays out of the closure (and thus the image): NIX_PATH refers to it without context. Also drop nixos-rebuild & co. (and with them python), and mask a few units that have nothing to do here but get started at boot anyway.
-rw-r--r--default.nix70
1 files changed, 60 insertions, 10 deletions
diff --git a/default.nix b/default.nix
index eeb4559..cdaa458 100644
--- a/default.nix
+++ b/default.nix
@@ -42,7 +42,7 @@ let
boot.kernelPackages = kernelPackages;
boot.initrd.allowMissingModules = true; # e.g. ahci, efivarfs with a cut-down kernel
- boot.initrd.kernelModules = [ "virtio_mmio" "vmw_vsock_virtio_transport" "9p" "9pnet_fd" "overlay" ];
+ boot.initrd.kernelModules = [ "virtio_mmio" "virtio_blk" "erofs" "vmw_vsock_virtio_transport" "9p" "9pnet_fd" "overlay" ];
boot.kernelParams = [
"console=ttyS0" "reboot=k" "panic=1"
"8250.nr_uarts=1" # only ttyS0 exists (stops 0x2f8/0x3e8/0x2e8 probing)
@@ -55,11 +55,20 @@ let
fsType = "tmpfs";
options = [ "mode=755" ];
};
- # The store: the host's, read-only via 9P over vsock (see mount.vsock9p
- # below and the run script), with a tmpfs on top, so nix can add to
- # it. What it adds lives in RAM, and is gone after a reboot. The
- # host's store doesn't change under a path, so 9P may cache freely.
- fileSystems."/nix/.ro-store" = {
+ # The store, in layers: the guest's own closure from an image (see
+ # storeImage below), as reading it is much faster than reading the
+ # host's store, which comes next, read-only via 9P over vsock (see
+ # mount.vsock9p and the run script), for anything else, like
+ # nixpkgs; then a tmpfs on top, so nix can add to the store. What it
+ # adds lives in RAM, and is gone after a reboot. The host's store
+ # doesn't change under a path, so 9P may cache freely.
+ fileSystems."/nix/.image" = {
+ device = "/dev/vda";
+ fsType = "erofs";
+ options = [ "ro" "nodev" "nosuid" ];
+ neededForBoot = true;
+ };
+ fileSystems."/nix/.host-store" = {
device = "565";
fsType = "vsock9p";
options = [ "ro" "cache=loose" ];
@@ -72,7 +81,7 @@ let
neededForBoot = true;
};
fileSystems."/nix/store".overlay = {
- lowerdir = [ "/nix/.ro-store" ];
+ lowerdir = [ "/nix/.image" "/nix/.host-store" ];
upperdir = "/nix/.rw-store/store";
workdir = "/nix/.rw-store/work";
};
@@ -82,7 +91,10 @@ let
# 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=${pkgs.path}" ];
+ # 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}" ];
environment.variables.NIX_REMOTE = "daemon";
systemd.services.nix-load-db = {
requiredBy = [ "nix-daemon.service" ];
@@ -148,6 +160,7 @@ let
services.timesyncd.enable = false;
system.nssModules = lib.mkForce [ ];
system.switch.enable = false;
+ system.disableInstallerTools = true; # nixos-rebuild & co., i.e. python
systemd.coredump.enable = false;
systemd.oomd.enable = false;
systemd.tpm2.enable = false;
@@ -161,6 +174,22 @@ let
];
services.journald.storage = "volatile"; # don't flush to /var/log
+ # Nothing to do here, but they'd still get started at boot, each
+ # adding to the reads from the store.
+ systemd.units = lib.genAttrs [
+ "dev-hugepages.mount"
+ "dev-mqueue.mount"
+ "modprobe@configfs.service"
+ "modprobe@drm.service"
+ "modprobe@efi_pstore.service"
+ "sys-fs-fuse-connections.mount"
+ "sys-kernel-config.mount"
+ "sys-kernel-debug.mount"
+ "sys-kernel-tracing.mount"
+ "systemd-pstore.service"
+ "systemd-vconsole-setup.service"
+ ] (_: { enable = lib.mkForce false; });
+
# Of the generators systemd runs after switching root, only fstab and
# getty have anything to do here.
systemd.generators = lib.genAttrs [
@@ -191,13 +220,29 @@ let
inherit (nixos.config.system.build) toplevel kernel initialRamdisk;
+ # The host's nixpkgs, for the guest's NIX_PATH (copied to the store, if
+ # it's not a store path already).
+ nixpkgs = "${pkgs.path}";
+
+ # The guest's closure, as erofs image. (Without nixpkgs, which made it
+ # 7 GiB, and comes from the host's store instead.)
+ storeImage = pkgs.runCommand "firecracker-store.img" {
+ nativeBuildInputs = [ pkgs.erofs-utils pkgs.gnutar ];
+ closure = pkgs.closureInfo { rootPaths = [ toplevel ]; };
+ } ''
+ tar -c -P --verbatim-files-from \
+ --transform 'flags=rSh;s|^/nix/store/||' \
+ --files-from $closure/store-paths |
+ mkfs.erofs --quiet --tar=f --force-uid=0 --force-gid=0 --hard-dereference -T 0 $out
+ '';
+
config = pkgs.writeText "firecracker.json" (builtins.toJSON {
boot-source = {
kernel_image_path = "${kernel.dev}/vmlinux";
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 ]; }}/registration"
+ "fc.registration=${pkgs.closureInfo { rootPaths = [ toplevel nixpkgs ]; }}/registration"
"@TTY_ARGS@"
]);
};
@@ -206,7 +251,12 @@ let
host_dev_name = "@TAP@"; # see host.nix
guest_mac = "@MAC@";
}];
- drives = [ ];
+ drives = [{
+ drive_id = "store";
+ path_on_host = "${storeImage}";
+ is_root_device = false;
+ is_read_only = true;
+ }];
vsock = {
guest_cid = 3;
uds_path = "@VSOCK@";