-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflake.nix
More file actions
419 lines (378 loc) · 14.8 KB
/
Copy pathflake.nix
File metadata and controls
419 lines (378 loc) · 14.8 KB
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
{
description = "attest — NixOS test driver in elixir";
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
flake-parts.url = "github:hercules-ci/flake-parts";
treefmt-nix = {
url = "github:numtide/treefmt-nix";
inputs.nixpkgs.follows = "nixpkgs";
};
firecracker-src = {
url = "github:firecracker-microvm/firecracker/v1.15.0";
flake = false;
};
};
outputs =
inputs@{ flake-parts, ... }:
flake-parts.lib.mkFlake { inherit inputs; } {
imports = [
inputs.treefmt-nix.flakeModule
];
systems = [
"x86_64-linux"
"aarch64-linux"
"aarch64-darwin"
];
perSystem =
{
config,
self',
inputs',
pkgs,
system,
lib,
...
}:
let
# headless erlang (no wx/gtk/webkitgtk, saves ~200MB closure)
erlang = (pkgs.beam.override { wxSupport = false; }).interpreters.erlang_27;
beamPackages = pkgs.beam.packagesWith erlang;
elixir = beamPackages.elixir_1_17;
# import package build
packageSet = import ./nix/package.nix {
inherit beamPackages;
lib = pkgs.lib;
};
inherit (packageSet) attest;
# patched firecracker from our fork (vsock snapshot/restore fix)
firecracker-patched = pkgs.firecracker.overrideAttrs (old: {
version = "1.16.0-dev";
src = inputs.firecracker-src;
cargoDeps = pkgs.rustPlatform.fetchCargoVendor {
src = inputs.firecracker-src;
hash = "sha256-0RzHIivl1upuFeZjTJvTZXqHjirzmVEK2ySB4W6aiVA=";
};
checkFlags = old.checkFlags ++ [
"--skip=env::tests::test_copy_exec_to_chroot"
];
});
in
{
checks = {
inherit attest;
# mix format check
format =
pkgs.runCommand "mix-format-check"
{
nativeBuildInputs = [ elixir ];
src = ./.;
}
''
cd $src
export ERL_FLAGS="+fnu"
export MIX_HOME=$TMPDIR/.mix
export HEX_HOME=$TMPDIR/.hex
mix format --check-formatted
touch $out
'';
# mix test - use all deps (including test deps)
test = beamPackages.mixRelease {
pname = "attest-tests";
version = "0.1.0";
src = attest.src;
mixNixDeps = attest.passthru.testMixNixDeps;
nativeBuildInputs = [
pkgs.vde2
pkgs.tesseract
pkgs.imagemagick
];
buildPhase = ''
runHook preBuild
export ERL_FLAGS="+fnu"
MIX_ENV=test mix compile --no-deps-check
runHook postBuild
'';
checkPhase = ''
export ERL_FLAGS="+fnu"
MIX_ENV=test mix test
'';
doCheck = true;
installPhase = ''
runHook preInstall
touch $out
runHook postInstall
'';
dontFixup = true;
};
}
// lib.optionalAttrs pkgs.stdenv.isLinux (
let
integrationTests = import ./integration-tests {
inherit pkgs;
attest = attest;
};
in
{
# single-vm integration test (boot, execute, screenshot, shutdown)
integration = integrationTests.basic;
# multi-vm integration test (two VMs via Driver)
integration-multi-vm = integrationTests.multi-vm;
# make-test smoke test (nix wrapper end-to-end)
make-test-smoke = import ./nix/make-test.nix {
inherit pkgs;
attest = attest;
name = "smoke";
nodes = {
machine = { };
};
testScript = ''
start_all.()
Attest.wait_for_unit(machine, "multi-user.target")
output = Attest.succeed(machine, "echo hello-from-make-test")
unless String.contains?(output, "hello-from-make-test") do
raise "unexpected output: #{inspect(output)}"
end
'';
};
# multi-node make-test (two VMs, validates multi-node pipeline)
make-test-multi = import ./nix/make-test.nix {
inherit pkgs;
attest = attest;
name = "multi";
nodes = {
server = { };
client = { };
};
testScript = ''
start_all.()
Attest.wait_for_unit(server, "multi-user.target")
Attest.wait_for_unit(client, "multi-user.target")
server_out = Attest.succeed(server, "hostname")
client_out = Attest.succeed(client, "hostname")
unless String.contains?(server_out, "server") do
raise "server hostname mismatch: #{inspect(server_out)}"
end
unless String.contains?(client_out, "client") do
raise "client hostname mismatch: #{inspect(client_out)}"
end
'';
};
# firecracker make-test smoke test (ext4 rootfs, vmlinux, vsock backdoor)
firecracker-smoke = import ./nix/firecracker/make-test.nix {
inherit pkgs;
attest = attest;
name = "fc-smoke";
nodes = {
machine = { };
};
testScript = ''
start_all.()
Attest.wait_for_unit(machine, "multi-user.target")
output = Attest.succeed(machine, "echo hello-from-firecracker")
unless String.contains?(output, "hello-from-firecracker") do
raise "unexpected output: #{inspect(output)}"
end
'';
};
# snapshot/restore
firecracker-snapshot = import ./nix/firecracker/make-test.nix {
inherit pkgs;
attest = attest;
name = "fc-snapshot";
splitStore = true;
nodes.machine = {
# FC snapshot/restore only works with guest kernels 5.10 and 6.1
# (newer kernels triple-fault ~1s after restore)
# https://github.com/firecracker-microvm/firecracker/blob/main/docs/kernel-policy.md
boot.kernelPackages = pkgs.linuxPackages_6_1;
};
testScript = ''
start_all.()
Attest.wait_for_unit(machine, "multi-user.target")
Attest.snapshot_create(machine, "/tmp/snap")
Attest.snapshot_restore(machine, "/tmp/snap")
result = Attest.succeed(machine, "echo restored-ok")
unless String.contains?(result, "restored-ok") do
raise "unexpected output after restore: #{inspect(result)}"
end
'';
};
# pre-built snapshot: VM boots from cached snapshot (~128ms restore)
firecracker-prebuilt = import ./nix/firecracker/make-test.nix {
inherit pkgs;
attest = attest;
name = "fc-prebuilt";
splitStore = true;
usePrebuiltSnapshots = true;
nodes.machine = {
boot.kernelPackages = pkgs.linuxPackages_6_1;
};
testScript = ''
start_all.()
# VM should already be at multi-user.target (restored from snapshot)
output = Attest.succeed(machine, "systemctl is-active multi-user.target")
unless String.trim(output) == "active" do
raise "multi-user.target was not active: #{inspect(output)}"
end
output = Attest.succeed(machine, "echo hello-from-prebuilt-snapshot")
unless String.contains?(output, "hello-from-prebuilt-snapshot") do
raise "unexpected: #{inspect(output)}"
end
'';
};
# firecracker split-store smoke test (erofs nix store + minimal rootfs)
firecracker-split = import ./nix/firecracker/make-test.nix {
inherit pkgs;
attest = attest;
name = "fc-split";
splitStore = true;
nodes = {
machine = { };
};
testScript = ''
start_all.()
Attest.wait_for_unit(machine, "multi-user.target")
output = Attest.succeed(machine, "echo hello-from-split-store")
unless String.contains?(output, "hello-from-split-store") do
raise "unexpected output: #{inspect(output)}"
end
# verify nix store is an overlay mount
mount_out = Attest.succeed(machine, "mount | grep '/nix/store'")
unless String.contains?(mount_out, "overlay") do
raise "nix store not overlay-mounted: #{inspect(mount_out)}"
end
'';
};
# firecracker multi-VM networking test (bridge + TAP)
firecracker-network = import ./nix/firecracker/make-test.nix {
inherit pkgs;
attest = attest;
name = "fc-net";
splitStore = true;
enableNetwork = true;
nodes = {
alice =
{ pkgs, ... }:
{
environment.systemPackages = [ pkgs.iputils ];
};
bob =
{ pkgs, ... }:
{
environment.systemPackages = [ pkgs.iputils ];
};
};
testScript = ''
start_all.()
# verify both VMs have IPs
alice_ip = Attest.succeed(alice, "ip -4 addr show eth0 | grep inet")
unless String.contains?(alice_ip, "192.168.1.1"), do: raise("alice should be .1")
bob_ip = Attest.succeed(bob, "ip -4 addr show eth0 | grep inet")
unless String.contains?(bob_ip, "192.168.1.2"), do: raise("bob should be .2")
# ping each other
Attest.succeed(alice, "ping -c 1 -W 3 192.168.1.2")
Attest.succeed(bob, "ping -c 1 -W 3 192.168.1.1")
# hostname resolution via /etc/hosts
Attest.succeed(alice, "ping -c 1 -W 3 bob")
Attest.succeed(bob, "ping -c 1 -W 3 alice")
'';
};
# cloud-hypervisor make-test smoke test (ext4 rootfs, vmlinux, vsock)
cloud-hypervisor-smoke = import ./nix/cloud-hypervisor/make-test.nix {
inherit pkgs;
attest = attest;
name = "ch-smoke";
nodes = {
machine = { };
};
testScript = ''
start_all.()
Attest.wait_for_unit(machine, "multi-user.target")
output = Attest.succeed(machine, "echo hello-from-cloud-hypervisor")
unless String.contains?(output, "hello-from-cloud-hypervisor") do
raise "unexpected output: #{inspect(output)}"
end
'';
};
# cloud-hypervisor multi-VM networking test (bridge + TAP)
cloud-hypervisor-network = import ./nix/cloud-hypervisor/make-test.nix {
inherit pkgs;
attest = attest;
name = "ch-net";
splitStore = true;
enableNetwork = true;
nodes = {
alice =
{ pkgs, ... }:
{
environment.systemPackages = [ pkgs.iputils ];
};
bob =
{ pkgs, ... }:
{
environment.systemPackages = [ pkgs.iputils ];
};
};
testScript = ''
start_all.()
alice_ip = Attest.succeed(alice, "ip -4 addr show eth0 | grep inet")
unless String.contains?(alice_ip, "192.168.1.1"), do: raise("alice should be .1")
bob_ip = Attest.succeed(bob, "ip -4 addr show eth0 | grep inet")
unless String.contains?(bob_ip, "192.168.1.2"), do: raise("bob should be .2")
Attest.succeed(alice, "ping -c 1 -W 3 192.168.1.2")
Attest.succeed(bob, "ping -c 1 -W 3 192.168.1.1")
Attest.succeed(alice, "ping -c 1 -W 3 bob")
Attest.succeed(bob, "ping -c 1 -W 3 alice")
'';
};
perf-budget = import ./nix/perf-budget.nix {
inherit pkgs;
checks = self'.checks;
};
}
);
packages = {
default = attest;
inherit attest;
}
// lib.optionalAttrs pkgs.stdenv.isLinux {
bench = import ./nix/bench.nix {
inherit pkgs attest;
};
};
devShells.default = import ./nix/devshell.nix {
inherit pkgs elixir beamPackages;
checks = self'.checks;
};
# treefmt configuration
treefmt = {
projectRootFile = "flake.nix";
programs = {
nixfmt.enable = true;
};
settings.formatter = {
# custom mix format wrapper
mix-format = {
command = pkgs.writeShellApplication {
name = "mix-format";
runtimeInputs = [ elixir ];
text = ''
export MIX_HOME="''${TMPDIR:-/tmp}/.mix"
export HEX_HOME="''${TMPDIR:-/tmp}/.hex"
mix format "$@"
'';
};
includes = [
"*.ex"
"*.exs"
];
};
};
};
};
flake = {
# nixos module for running tests (future)
# nixosModules.default = ./nix/module.nix;
};
};
}