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. --- README.md | 40 ++++++++++++++++++++++++++-------------- 1 file changed, 26 insertions(+), 14 deletions(-) (limited to 'README.md') diff --git a/README.md b/README.md index cfa364e..422d100 100644 --- a/README.md +++ b/README.md @@ -6,12 +6,14 @@ console. Boots in about a second and a quarter. Stable Nix, no flakes. The guest gets: -- the host's Nix store, read-only, with its own closure from local images - for speed (one per layer, see below), and a writable layer on top, so - nix and `nix-shell` work; +- its own closure, from local images (one per layer, see below), as its + store, read-only, without nix; +- or, with the layer `layers/nix.nix` (see below), on top of that the + host's Nix store, read-only, and a writable layer on top of both, so + nix and `nix-shell` work, and `/nixpkgs`: the host's nixpkgs, + read-only, on the guest's `NIX_PATH`; - `/xchg`: the directory `xchg` in the current working directory, shared with the host (and with other VMs started in the same directory); -- `/nixpkgs`: the host's nixpkgs, read-only, on the guest's `NIX_PATH`; - network access only to HTTPS, through a proxy on the host that allows only listed domains, optionally only listed paths. @@ -61,15 +63,24 @@ nix-build pkgs/run-firecracker-vm Leave the guest with `reboot`. -What nix in the guest adds to the store goes to a scratch disk that's -removed when the guest exits. To keep it across boots: +The guest's store is read-only, and there's no nix, unless it has the +nix layer, preferably first, so its image is the same for all guests +with nix (see layers below): + +```sh +nix-build pkgs/run-firecracker-vm --arg layers '[ layers/nix.nix ./my-service.nix ]' +``` + +What nix in such a guest adds to the store goes to a disk of its own, +which the run script then requires (and refuses for a guest without nix, +like `--nixpkgs` and `--nixpkgs-cache`, below): ```sh ./result/bin/run-firecracker-vm --store ~/fc-store.img [--store-size 50G] ``` The file gets created (sparse, default 16G) if it doesn't exist. Only one -VM at a time can use it. `/home` is gone when the guest exits, too, +VM at a time can use it. `/home` is gone when the guest exits, unless given a disk of its own, which works the same way (kept apart from the store, so either can be kept or thrown away): @@ -86,12 +97,12 @@ so the state doesn't silently end up on the tmpfs: The run script then refuses to start the guest without `--home`. -`/nixpkgs` is the nixpkgs the guest was built from (``, checkout -or not); `--nixpkgs DIR` serves another one. It's not cached in the -guest, so changes to it show up right away, but evaluating is slow; -`--nixpkgs-cache=loose` caches it (several times faster, but changes may -not show up while the guest runs). Other modes: `mmap`, `readahead`, -`none` (the default). +With nix, `/nixpkgs` is the nixpkgs the guest was built from +(``, checkout or not); `--nixpkgs DIR` serves another one. It's +not cached in the guest, so changes to it show up right away, but +evaluating is slow; `--nixpkgs-cache=loose` caches it (several times +faster, but changes may not show up while the guest runs). Other modes: +`mmap`, `readahead`, `none` (the default). The guest gets 2 CPUs and 1024 MiB unless told otherwise (`--vcpus N`, 1 to 32; `--mem-size MIB`). @@ -198,6 +209,7 @@ For `nix-shell` and the like to download, allow `cache.nixos.org`. - `pkgs/run-firecracker-vm/default.nix`: the guest's NixOS configuration, the Firecracker configuration, and the `run-firecracker-vm` script. - `pkgs/run-firecracker-vm/kernel.nix`: the cut-down kernel. +- `layers/nix.nix`: nix, as a layer. - `modules/host/default.nix`: the host's side: network, DHCP/DNS, egress proxy, `fc-egress`. - `modules/host/egress.lua`: the egress policy, for the proxy (OpenResty). @@ -205,7 +217,7 @@ For `nix-shell` and the like to download, allow `cache.nixos.org`. ## Caveats - x86_64 only. -- The guest can read all of the host's store. +- A guest with nix can read all of the host's store. - Only HTTPS with a server name (SNI) gets out; plain HTTP and other protocols don't. - Paths in the host's store outside the guest's closure are visible in -- cgit v1.3.1