From e3fea8300d452b64dfc0bdd7e662193a1c92010c Mon Sep 17 00:00:00 2001 From: tv Date: Mon, 16 Nov 2026 23:27:49 +0000 Subject: add README --- README.md | 106 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 106 insertions(+) create mode 100644 README.md diff --git a/README.md b/README.md new file mode 100644 index 0000000..6435038 --- /dev/null +++ b/README.md @@ -0,0 +1,106 @@ +# firecracker + +NixOS guests in [Firecracker](https://firecracker-microvm.github.io/), +started from a NixOS host, dropping into a root shell on the serial +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 a local image + for speed, and a writable layer on top, so nix and `nix-shell` work; +- `/xchg`: the directory `xchg` in the current working directory, shared + with the host (and with other VMs started in the same directory); +- network access only to HTTPS, through a proxy on the host that allows + only listed domains, optionally only listed paths. + +## Host setup + +In the host's NixOS configuration: + +```nix +imports = [ + (import /path/to/firecracker/modules/host { user = "tv"; }) +]; +``` + +This sets up a bridge `fcbr0` (172.16.0.1/24) with tap devices for the +given user, DHCP and DNS for the guests, the egress proxy, and nftables +rules that keep the guests from reaching anything else, on the host or +beyond. It needs `networking.nftables` (and enables it). + +Optional arguments: `taps` (default 8, the number of VMs that can run at +the same time) and `proxyPort` (default 10443, where the proxy listens; +the guests' port 443 gets redirected there). + +If the host has a firewall of its own, it has to let the guests in on +`fcbr0`: udp 67 (DHCP), udp and tcp 53 (DNS), and tcp 10443 (the proxy). + +## Running a VM + +```sh +nix-build pkgs/run-firecracker-vm +./result/bin/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: + +```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. + +To use a cut-down kernel, based on Firecracker's recommended guest +configuration, instead of `linuxPackages_latest`: + +```sh +nix-build pkgs/run-firecracker-vm \ + --arg kernelPackages 'import pkgs/run-firecracker-vm/kernel.nix {}' +``` + +## Egress + +The guests resolve every name to the host, and all their HTTPS goes to +the proxy, which decides by the server name and the rules in +`/var/lib/fc-egress/allowed`. The user manages these with `fc-egress`: + +```sh +fc-egress list +fc-egress allow cache.nixos.org # the domain and its subdomains +fc-egress allow github.com/NixOS/ # only paths starting with /NixOS/ +fc-egress deny github.com/NixOS/ +fc-egress log -f +``` + +Changes apply to the next connection. Whole domains get passed through +encrypted. Domains with path rules get decrypted to check each request, +with certificates from a CA the host creates on first start +(`/var/lib/fcbr0-egress/ca.pem`). The guest trusts that CA if it exists +when the guest gets built, so build the guest on the host after the proxy +has started once. Everything else, and anything that isn't HTTPS, gets +refused. + +For `nix-shell` and the like to download, allow `cache.nixos.org`. + +## Layout + +- `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. +- `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). + +## Caveats + +- x86_64 only. +- The guest 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 + the guest, but nix there doesn't know them as valid, so it downloads its + own copy if it needs one. -- cgit v1.3.1