Repository navigation
Expand file tree
/
Copy pathflake.nix
More file actions
277 lines (243 loc) · 10.6 KB
/
Copy pathflake.nix
File metadata and controls
277 lines (243 loc) · 10.6 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
{
description = "Qubix: declarative Hyper-V appliance factory for NixOS VMs";
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixos-25.11";
nixos-generators = {
url = "github:nix-community/nixos-generators";
inputs.nixpkgs.follows = "nixpkgs";
};
};
outputs = { self, nixpkgs, nixos-generators }:
let
system = "x86_64-linux";
lib = nixpkgs.lib;
pkgs = import nixpkgs {
inherit system;
config.allowUnfreePredicate = pkg:
builtins.elem (lib.getName pkg) [
"spotify"
];
};
# GitHub repository that publishes release assets. `qubixctl -Command up`
# downloads images from here when it is not asked to build them in WSL.
releaseRepo = "PhysShell/qubix";
# Evaluate a machine's NixOS config. The Hyper-V manifest is derived from
# evaluated machine configs so that settings like hostName, static
# networking and the home-disk contract have one source of truth: the
# machine file. No more keeping two files in sync by hand.
#
# The Hyper-V image module is part of the evaluation so that
# `nixosConfigurations.*` describe exactly the system that ends up in the
# VHDX (root filesystem, growPartition, Hyper-V guest services included).
evalMachine = modules: lib.nixosSystem {
inherit system pkgs;
modules = modules ++ imageModules;
specialArgs = { inherit self; };
};
# The upstream Hyper-V image module plus Qubix's override of it (see
# profiles/image/hyperv.nix). Both the `nixosConfigurations.*` evaluation
# and the nixos-generators build path have to see the same pair, or the
# VHDX would stop matching the config the manifest and the tests describe.
# nixos-generators imports the upstream module itself; Nix keys modules by
# path, so naming it twice is free.
imageModules = [
"${nixpkgs}/nixos/modules/virtualisation/hyperv-image.nix"
./profiles/image/hyperv.nix
];
# Extract network-related manifest fields from an evaluated NixOS config.
# Returns an empty attrset when no static IP is configured (DHCP mode).
mkNetworkFields = cfg:
let net = cfg.qubix.network;
in lib.optionalAttrs (net.staticIp != null) {
staticIp = net.staticIp;
gatewayIp = net.gateway;
# Derive subnet from staticIp + prefixLength (correct for /17-/24).
natSwitchSubnet =
let parts = lib.splitString "." net.staticIp;
in "${builtins.elemAt parts 0}.${builtins.elemAt parts 1}"
+ ".${builtins.elemAt parts 2}.0/${toString net.prefixLength}";
};
# One manifest entry per machine. Everything the Windows controller needs
# to create the VM, find the images and open the RDP window lives here.
mkMachineManifest = { name, cfg }: {
# Nix package names, used by the WSL build path.
package = "${name}-vhdx";
homePackage = "${name}-home-vhdx";
# Derived from the machine config — only one place to change.
vmName = "qubix-${cfg.networking.hostName}";
hostName = cfg.networking.hostName;
# Used in DHCP mode; ignored when staticIp is present.
switchName = "Default Switch";
cpuCount = 2;
# Startup, not "how much it needs". Hyper-V has to find the whole
# startup amount before the guest exists, and only then does Dynamic
# Memory - which qubixctl enables between 1 and this maximum - let
# hv_balloon give the surplus back. A 4 GiB floor is what a host with
# other things running refuses first, and the appliance does not want
# it: tests/xrdp-session.nix brings up Xorg under xrdp, Openbox and
# Spotify with its window mapped and maximised in a 2 GiB VM, which is
# the whole kiosk. The maximum stays at 6 GiB because Spotify's cache
# is what grows, and growing is what Dynamic Memory is for.
memoryStartupBytes = 2147483648;
maxMemoryBytes = 6442450944;
vmRoot = "C:\\HyperV\\Qubix";
wslDistro = "NixOS";
homeDisk = {
inherit (cfg.qubix.homeDisk) enable label sizeMiB;
};
# RDP window the controller opens after boot. The lab password is
# already public in this repo; storing it in Windows Credential Manager
# for one-click connects does not make it any less public.
rdp = {
user = "rdp";
password = cfg.qubix.labPassword;
width = 1280;
height = 800;
};
# GitHub release assets, as produced by the `${name}-release` package.
release = {
repo = releaseRepo;
systemAsset = "${name}.vhdx.gz";
homeAsset = "${name}-home.vhdx.gz";
sumsAsset = "SHA256SUMS";
};
# Network fields are present only when qubix.network.staticIp is set
# in the machine file. Nothing to change here when toggling static IP.
} // mkNetworkFields cfg;
spotibox = evalMachine [ ./machines/spotibox.nix ];
spotiboxDebug = evalMachine [ ./machines/spotibox-debug.nix ];
qubixManifest = {
schemaVersion = 2;
machines = {
spotibox = mkMachineManifest { name = "spotibox"; cfg = spotibox.config; };
};
};
mkHypervImage = modules:
nixos-generators.nixosGenerate {
inherit system pkgs lib;
nixosSystem = lib.nixosSystem;
format = "hyperv";
modules = modules ++ imageModules;
specialArgs = {
inherit self;
};
};
# A UUID that is a function of its name: the name's SHA-256 cut to the
# 8-4-4-4-12 shape, with RFC 9562's version (8, custom) and variant bits
# set. The same name gives the same UUID on every machine, every build.
stableUuid = name:
let
h = builtins.hashString "sha256" name;
at = start: len: builtins.substring start len h;
# The variant is the top two bits of octet 8, binary 10.
variant = builtins.elemAt [ "8" "9" "a" "b" ]
(lib.mod (lib.fromHexString (at 16 1)) 4);
in "${at 0 8}-${at 8 4}-8${at 13 3}-${variant}${at 17 3}-${at 20 12}";
# Pre-formatted, labelled, empty ext4 disk for /home as a dynamic VHDX.
# The guest mounts it by label (profiles/storage/persistent-home.nix), the
# host copies it next to the VM once and never touches it again.
#
# The filesystem is the same bytes on every build. mke2fs draws the UUID
# and the directory hash seed at random unless told otherwise, and ext4's
# metadata checksums are seeded from the UUID, so a random one does not
# change 16 bytes but every checksummed structure: two builds of one
# commit differed in some 6,300 bytes across 551 blocks. Both are derived from
# what the disk is instead - the machine and the label - and
# SOURCE_DATE_EPOCH fixes the timestamps mke2fs writes, which stdenv
# happens to set as well; tests/home-seed.nix pins the UUID. The VHDX
# around it is not reproducible: qemu-img writes fresh GUIDs into its
# headers and metadata, and a sequence number and log entries of its own.
mkHomeSeed = cfg:
let
hd = cfg.qubix.homeDisk;
fsUuid = stableUuid "qubix-home-seed:${cfg.networking.hostName}:${hd.label}";
in pkgs.runCommand "qubix-home-${cfg.networking.hostName}.vhdx" {
nativeBuildInputs = [ pkgs.e2fsprogs pkgs.qemu-utils ];
passthru = { inherit fsUuid; };
} ''
raw="$TMPDIR/home.raw"
truncate -s ${toString hd.sizeMiB}M "$raw"
SOURCE_DATE_EPOCH=1 mkfs.ext4 -q -F -L ${lib.escapeShellArg hd.label} -m 0 \
-U ${fsUuid} \
-E lazy_itable_init=1,lazy_journal_init=1,hash_seed=${fsUuid} "$raw"
qemu-img convert -f raw -O vhdx -o subformat=dynamic "$raw" "$out"
'';
# Everything one GitHub release needs, gzip'd so that Windows can unpack
# it with nothing but .NET (no zstd/7-Zip dependency on the host).
mkReleaseBundle = { name, systemImage, homeImage, manifest }:
pkgs.runCommand "qubix-release-${name}" {
nativeBuildInputs = [ pkgs.pigz ];
} ''
mkdir -p "$out"
vhdx=$(find -L ${systemImage} -type f -name '*.vhdx' -print -quit)
test -n "$vhdx" || { echo "no .vhdx in ${systemImage}" >&2; exit 1; }
pigz -9 -n -c "$vhdx" > "$out/${name}.vhdx.gz"
pigz -9 -n -c ${homeImage} > "$out/${name}-home.vhdx.gz"
cp ${manifest} "$out/manifest.json"
cd "$out"
sha256sum ${name}.vhdx.gz ${name}-home.vhdx.gz manifest.json > SHA256SUMS
'';
manifestJson = (pkgs.formats.json { }).generate "qubix-manifest.json" qubixManifest;
in {
# Exposed for introspection (`nix eval`, `nixos-rebuild build-vm`, tests).
nixosConfigurations = {
spotibox = spotibox;
spotibox-debug = spotiboxDebug;
};
packages.${system} = {
spotibox-vhdx = mkHypervImage [
./machines/spotibox.nix
];
spotibox-debug-vhdx = mkHypervImage [
./machines/spotibox-debug.nix
];
spotibox-home-vhdx = mkHomeSeed spotibox.config;
spotibox-release = mkReleaseBundle {
name = "spotibox";
systemImage = self.packages.${system}.spotibox-vhdx;
homeImage = self.packages.${system}.spotibox-home-vhdx;
manifest = manifestJson;
};
qubix-manifest-json = manifestJson;
# The systems the images are made of. A VHDX is its `toplevel` closure
# plus a filesystem around it, and toplevel builds without KVM, so this
# is what `tools/closure.sh` measures and what the CI closure budget
# gates on. It is also the fastest way to check "does this change even
# build" without waiting for an image.
spotibox-toplevel = spotibox.config.system.build.toplevel;
spotibox-debug-toplevel = spotiboxDebug.config.system.build.toplevel;
default = self.packages.${system}.spotibox-vhdx;
};
checks.${system} = {
spotibox-basic =
import ./tests/spotibox-basic.nix {
inherit pkgs;
};
spotibox-appliance-split =
import ./tests/appliance-split.nix {
inherit pkgs lib;
prod = spotibox.config;
debug = spotiboxDebug.config;
};
spotibox-xrdp-session =
import ./tests/xrdp-session.nix {
inherit pkgs;
};
spotibox-xrdp-audio =
import ./tests/xrdp-audio.nix {
inherit pkgs;
};
spotibox-kernel-contract =
import ./tests/kernel-contract.nix {
inherit pkgs lib;
prod = spotibox.config;
debug = spotiboxDebug.config;
};
spotibox-home-seed-identity =
import ./tests/home-seed.nix {
inherit pkgs lib;
seed = self.packages.${system}.spotibox-home-vhdx;
};
};
};
}