rigs
NixOS guests, each a computer of its own, in
Firecracker or Cloud
Hypervisor (see below),
started from a NixOS host, reached by ssh, or with layers/console.nix,
dropping into a root shell on the serial console. Boots in about a
second and a quarter. Stable Nix, no flakes.
The guest gets:
- 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 andnix-shellwork, and/nixpkgs: the host's nixpkgs, read-only, on the guest'sNIX_PATH; - optionally (
--xchg DIR),/xchg: a directory of the host's, shared with it (and with other VMs given 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:
imports = [
(import /path/to/rigs/modules/host { users = { tv = 8; alice = 2; }; })
];
users says who may run VMs, and how many at the same time: each gets
that many tap devices. This sets up a bridge rigbr0 (172.16.0.1/24,
and fdfc:fc::1/64) with the taps, DHCP and DNS for the guests, the egress proxy, and
nftables rules that keep the guests from reaching anything else, on the
host, beyond, or each other. Each running VM has a fixed address, and
the bridge drops anything a guest sends from another, so the proxy knows
whose a connection is. It needs networking.nftables (and enables it).
Optional arguments:
vms: static addresses for VMs, by name and user (172.16.0.2 to .254), e.g. for the host to reach them with a reverse proxy:vms = { hermes = { user = "tv"; ip = "172.16.0.2"; }; };. Any number of them; the user's VM of that name (--name hermes) gets its address on whichever of the user's taps it runs, so at most as many of them run at the same time as the user has taps. Taps have addresses of their own, for VMs without one: from 172.16.0.200 on, so they stand out (but for VMs' there; at most 55 taps in all). The VMs' addresses are also inconfig.rig.vms.NAME(user,ip,ip6):services.nginx.virtualHosts."hermes.example".locations."/".proxyPass = "http://${config.rig.vms.hermes.ip}:9120";proxyPort(default 10443): where the proxy listens; the guests' port 443 gets redirected there.checkPort(default 10080): where the guests' checks of their egress get answered; their port 80 of 172.16.0.1 gets redirected there.prefix6(defaultfdfc:fc::): the guests' IPv6 /64. Each guest gets the address from its MAC (SLAAC, from router advertisements by the host's dnsmasq), for tcp:// and udp:// rules; HTTPS stays IPv4, through the proxy.denyDestinations: addresses no whitelist can open, neither for tcp:// and udp:// rules nor through the proxy, whatever a name resolves to. Default: loopback, private, link-local, CGNAT, multicast and the like (IPv4 and IPv6). The host's own addresses are always out of reach (but for DHCP, DNS and the proxy).maxConnections(default 256) andmaxNewConnections(default 50 per second): limits per guest, so one user can't starve the others.
If the host has a firewall of its own, it has to let the guests in on
rigbr0: udp 67 (DHCP), udp and tcp 53 (DNS), tcp 10443 (the proxy), and
tcp 10080 (the checks).
Running a VM
nix-build pkgs/run-rig
./result/bin/run-rig
The guest's console (the run script's terminal) shows the kernel's and
systemd's messages, but has no shell, unless the guest has
layers/console.nix: root logged in on the console (or another user,
with services.getty.autologinUser; null for a login prompt, which
needs a password). It's small, so rather than a layer, with an image,
of its own, import it into another layer:
{ imports = [ /path/to/rigs/layers/console.nix ]; }
(As a layer of its own, it works, too: --arg layers '[ layers/console.nix ]'.)
A serial console doesn't get told when the terminal's size changes, so
the shell there asks the terminal before each prompt (resize, which
works in tmux, too). A tmux attached on the console keeps the size it
started with: detach and attach again after resizing.
Leave the guest with reboot or poweroff (there, or via ssh, see
below): either ends the VM.
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):
nix-build pkgs/run-rig --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):
./result/bin/run-rig --store ~/rig-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,
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):
./result/bin/run-rig --home ~/rig-home.img [--home-size 50G]
For a faster development loop, a guest with nix can switch to a new
build of its system without a reboot, with layers/switch.nix imported
into a layer. Build the system with the same arguments as the guest,
and switch to it:
sys=$(nix-build --no-out-link pkgs/run-rig -A toplevel --arg layers '[ layers/nix.nix ./my-service.nix ]')
ssh root@VM $sys/bin/switch-to-configuration test
The guest reads the new system from the host's store until it reboots. It's no longer what it booted, so for production, reboot it with the new run script instead. The kernel, its command line, the VMM's configuration, new secrets, and adding or removing layers need a reboot anyway.
A store path that the guest looked for before the host had it (say, a switch to a system that was still being built) stays missing in the guest's store, as the overlay remembers that it wasn't there, until the guest drops its caches (which takes seconds), as root:
echo 2 > /proc/sys/vm/drop_caches
A layer that keeps its state in /home can make that disk mandatory,
so the state doesn't silently end up on the tmpfs:
{ rig.home.required = true; }
The run script then refuses to start the guest without --home.
With nix, /nixpkgs is the nixpkgs the guest was built from
(<nixpkgs>, 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 a cache mode, and a Git
checkout, the run script tells the guest to drop its caches whenever the
checkout's commit changes. For other changes, and in the rare case
that the guest looked for a store path before the host had it, anyone
in the guest can drop them: rig-drop-caches (it takes seconds).
The guest gets 2 CPUs and 1024 MiB unless told otherwise (--vcpus N,
1 to 32; --mem-size MIB).
The same build also runs in Cloud
Hypervisor instead of Firecracker,
with --vmm cloud-hypervisor.
With --nested, the guest gets hardware virtualization (SVM, VMX), and
with it /dev/kvm (with the cut-down kernel, base/kernel.nix, or any
with KVM), to run VMs itself, like NixOS VM tests. It's off by default,
as it opens more of the host's KVM to the guest.
The guests run sshd. Its host key is a secret (see below), so it's not
in the store, and the run script requires it: --secret ssh_host_ed25519_key=FILE (an ed25519 private key without passphrase).
With the same one for all your guests, one known_hosts line does:
ssh-keygen -t ed25519 -N "" -f ~/.local/share/run-rig/ssh_host_ed25519_key
echo "172.16.0.*,fdfc:fc::* $(cut -d' ' -f1,2 ~/.local/share/run-rig/ssh_host_ed25519_key.pub)" >> ~/.ssh/known_hosts
./result/bin/run-rig --secret ssh_host_ed25519_key=$HOME/.local/share/run-rig/ssh_host_ed25519_key
Who may log in goes in a layer, e.g.:
{ users.users.root.openssh.authorizedKeys.keys = [ "ssh-ed25519 AAAA..." ]; }
A guest's address is its VM's, by name (vms, see host setup), or else
its tap's own (see rig-egress vms).
Secrets, like API keys (and that host key), come from the host at boot,
never through the store or /xchg (which other VMs given the same
directory see): a layer declares what it needs, where it goes (default:
/run/secrets/NAME), and whose it is (default: root's, 0400):
{ rig.secrets."hermes.env" = { owner = "hermes"; }; }
and the run script, which doesn't start the guest without them, gets
them with --secret NAME=FILE (FILE read once, so a pipe will do):
./result/bin/run-rig --secret hermes.env=<(pass show hermes/env)
They're only that guest's, and only in its memory (/run), until it
stops; to change one, restart the guest. A secret the guest doesn't
declare ends up in /run/secrets/NAME, root's, with a warning.
The guest's system can be built in layers, each a NixOS module on top of the ones before (the first on top of the base system):
nix-build pkgs/run-rig --arg layers '[ ./base-tools.nix ./my-service.nix ]'
The guest runs the top layer's system. Each layer gets its own image,
with only the store paths the layers below don't have, so the base
image, and those of layers below, stay the same whatever goes on top:
a service layer costs a small image of its own, named after its file
(rig-layer-2-my-service.img). Changing a layer
changes the images of the layers above it.
What all layers share goes into the base system, with base, NixOS
modules like layers', but in the base image. The kernel, for one: set
in a layer, it would leave the base's kernel modules unused in the base
image. So to use a cut-down kernel, based on Firecracker's recommended
guest configuration, instead of linuxPackages_latest:
nix-build pkgs/run-rig \
--arg base '[ base/kernel.nix ]'
And for a slimmer systemd (no importd, homed, machined, nspawn, cryptsetup, TPM2, FIDO2, journal-remote, ...), built from source, with dbus and dbus-broker against it:
nix-build pkgs/run-rig \
--arg base '[ base/kernel.nix base/systemd.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 VM's whitelist.
Each user has any number of named whitelists, in
/var/lib/rig-egress/USER/LIST, and manages them with rig-egress
(-l LIST; without it, the list default):
rig-egress list
rig-egress allow https://cache.nixos.org # the domain and its subdomains
rig-egress allow https://github.com/NixOS/ # only paths starting with /NixOS/
rig-egress allow https://api.github.com/search/issues # that path and below, any query
rig-egress allow tcp://192.0.2.7:22 # plain TCP to that address and port
rig-egress allow tcp://git.example.org:22 # or name (resolved by the host)
rig-egress allow udp://192.0.2.0/24:5000-5100
rig-egress deny https://github.com/NixOS/
rig-egress -l work allow https://example.com # the list "work"
rig-egress lists
rig-egress log -f
Each VM can have a name (also its host name; unique among your running
VMs), and uses one list: the one given with --egress, else the one
named like the VM, else (no name) default:
rig-egress -l box allow https://cache.nixos.org
./result/bin/run-rig --name box # list box
./result/bin/run-rig --name box2 --egress box # list box, too
rig-egress vms # running VMs: tap (as in the log), address, name, list
rig-egress -v box allow https://example.org # the list box uses
rig-egress use box default # switch box to another list
A list that doesn't exist allows nothing. Changes to lists and switches
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/rig-proxy/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.
tcp:// and udp:// rules take IPv4 addresses, IPv6 addresses in brackets
(tcp://[42::1]:22, udp://[42::/16]:5000-5100; not port 443, HTTPS
goes through the proxy, over IPv4), or names. The guests resolve every
name to the host, except the names in such rules: the host resolves
those (IPv4 and IPv6) and tells the guests the same addresses, so
ssh git.example.org works. They get resolved again every 5 minutes.
(Every guest gets those addresses for those names, but only the VMs
whose list has the rule can connect.) Those connections get forwarded, NATed to the host's address, and logged
(rate-limited, kernel log; rig-egress log shows them), as are refused
ones. If the host has a forward
chain of its own, it has to let them through, too (anything from
rigbr0; this module rejects what isn't allowed).
For nix-shell and the like to download, allow cache.nixos.org.
Guests can check what their egress allows, before relying on it, with
rig-egress check (the guest's own, read-only one), which asks the host:
rig-egress check https://github.com/krebs/kartei tcp://172.16.0.1:22
prints true or false and the URI, a line each (exit status 0 if all
are allowed, 1 if not), by the same rules as the egress itself: whole
domains and path rules for https://, and for tcp:// and udp://
the rules, their names as resolved for the VM, and denyDestinations.
It asks POST http://172.16.0.1/check, URIs one per line (at most 100),
true or false for each, a line each; any guest can only ask about
the list its own VM uses.
Layout
pkgs/run-rig/default.nix: the guest's NixOS configuration, the VMM's configuration, and therun-rigscript.base/kernel.nix: the cut-down kernel, forbase.base/systemd.nix: a slimmer systemd, forbase.layers/nix.nix: nix, as a layer.layers/console.nix: a shell on the serial console, to import into a layer.layers/switch.nix: switching a running guest, to import into a layer.modules/host/default.nix: the host's side: network, DHCP/DNS, egress proxy,rig-egress.modules/host/egress.lua: the egress policy, for the proxy (OpenResty).
Caveats
- x86_64 only.
- 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 the guest, but nix there doesn't know them as valid, so it downloads its own copy if it needs one.
