aboutsummaryrefslogtreecommitdiffstats
path: root/README.md
blob: b00e196bb6e25d55bfedbe0161d28e9d400f5f46 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# 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 local images
  for speed (one per layer, see below), 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);
- `/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.

## Host setup

In the host's NixOS configuration:

```nix
imports = [
  (import /path/to/firecracker/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 `fcbr0` (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 tap 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:

- `proxyPort` (default 10443): where the proxy listens; the guests' port
  443 gets redirected there.
- `prefix6` (default `fdfc: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) and `maxNewConnections` (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
`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.  `/home` is gone when the guest exits, too,
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):

```sh
./result/bin/run-firecracker-vm --home ~/fc-home.img [--home-size 50G]
```

`/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).

The guest gets 2 CPUs and 1024 MiB unless told otherwise (`--vcpus N`,
1 to 32; `--mem-size MIB`).

The guests run sshd.  Its host key comes from the host at boot, so it's
not in the store: `--ssh-host-key FILE`, by default
`~/.local/share/run-firecracker-vm/ssh_host_ed25519_key`, created on first
use, so all your guests have the same one, and one known_hosts line does:

```sh
echo "172.16.0.*,fdfc:fc::* $(cut -d' ' -f1,2 ~/.local/share/run-firecracker-vm/ssh_host_ed25519_key.pub)" >> ~/.ssh/known_hosts
```

Who may log in goes in a layer, e.g.:

```nix
{ users.users.root.openssh.authorizedKeys.keys = [ "ssh-ed25519 AAAA..." ]; }
```

A guest on tap fctapN is 172.16.0.(N+2) (see `fc-egress vms`).

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):

```sh
nix-build pkgs/run-firecracker-vm --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
(`firecracker-layer-2-my-service.img`).  Changing a layer
changes the images of the layers above 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 VM's whitelist.
Each user has any number of named whitelists, in
`/var/lib/fc-egress/USER/LIST`, and manages them with `fc-egress`
(`-l LIST`; without it, the list `default`):

```sh
fc-egress list
fc-egress allow https://cache.nixos.org     # the domain and its subdomains
fc-egress allow https://github.com/NixOS/   # only paths starting with /NixOS/
fc-egress allow tcp://192.0.2.7:22   # plain TCP to that address and port
fc-egress allow tcp://git.example.org:22   # or name (resolved by the host)
fc-egress allow udp://192.0.2.0/24:5000-5100
fc-egress deny https://github.com/NixOS/
fc-egress -l work allow https://example.com # the list "work"
fc-egress lists
fc-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`:

```sh
fc-egress -l box allow https://cache.nixos.org
./result/bin/run-firecracker-vm --name box               # list box
./result/bin/run-firecracker-vm --name box2 --egress box # list box, too
fc-egress vms                        # running VMs: tap (as in the log), name, list
fc-egress -v box allow https://example.org  # the list box uses
fc-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/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.

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; `fc-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
`fcbr0`; this module rejects what isn't allowed).

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.