-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathflake.nix
More file actions
396 lines (332 loc) · 13.6 KB
/
flake.nix
File metadata and controls
396 lines (332 loc) · 13.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
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
{
description = "Framework System library and CLI tool for Framework Computer hardware";
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
rust-overlay = {
url = "github:oxalica/rust-overlay";
inputs.nixpkgs.follows = "nixpkgs";
};
flake-utils.url = "github:numtide/flake-utils";
};
outputs = { self, nixpkgs, rust-overlay, flake-utils, ... }:
flake-utils.lib.eachDefaultSystem (system:
let
pkgs = import nixpkgs {
inherit system;
overlays = [ rust-overlay.overlays.default ];
};
# Read toolchain from rust-toolchain.toml
rustToolchain = pkgs.rust-bin.fromRustupToolchainFile ./rust-toolchain.toml;
# Toolchain extended with Windows cross-compilation target
rustToolchainWindows = rustToolchain.override {
targets = [ "x86_64-pc-windows-gnu" ];
};
# Create a custom rustPlatform with our toolchain
rustPlatform = pkgs.makeRustPlatform {
cargo = rustToolchain;
rustc = rustToolchain;
};
# rustPlatform with Windows cross-compilation target
rustPlatformWindows = pkgs.makeRustPlatform {
cargo = rustToolchainWindows;
rustc = rustToolchainWindows;
};
# Common build inputs for OS builds
commonBuildInputs = with pkgs; [
openssl
libgit2
zlib
] ++ pkgs.lib.optionals pkgs.stdenv.isLinux [
systemdLibs # libudev
] ++ pkgs.lib.optionals pkgs.stdenv.isDarwin [
pkgs.libiconv
pkgs.darwin.apple_sdk.frameworks.Security
pkgs.darwin.apple_sdk.frameworks.SystemConfiguration
];
commonNativeBuildInputs = with pkgs; [
pkg-config
zlib # Required by framework_lib build script at runtime
];
# Filter source to only include files needed for the build
buildSrc = pkgs.lib.cleanSourceWith {
src = ./.;
filter = path: type:
let
baseName = baseNameOf path;
relativePath = pkgs.lib.removePrefix (toString ./. + "/") path;
# Only include files/folders needed for the Rust build
includedRoots = [
"framework_lib"
"framework_tool"
"framework_uefi"
"res"
".cargo"
];
includedFiles = [
"Cargo.toml"
"Cargo.lock"
"rust-toolchain.toml"
];
isIncludedRoot = builtins.any (root:
relativePath == root || pkgs.lib.hasPrefix (root + "/") relativePath
) includedRoots;
in
isIncludedRoot || builtins.elem baseName includedFiles;
};
# Git dependency output hashes
gitDependencyHashes = {
"redox_hwio-0.1.6" = "sha256-knLIZ7yp42SQYk32NGq3SUGvJFVumFhD64Njr5TRdFs=";
"smbios-lib-0.9.1" = "sha256-3L8JaA75j9Aaqg1z9lVs61m6CvXDeQprEFRq+UDCHQo=";
};
# Build function for the CLI tool (Linux/macOS)
buildFrameworkTool = { release ? false, features ? [] }:
let
profile = if release then "release" else "debug";
featuresStr = if features == [] then "" else "--features ${builtins.concatStringsSep "," features}";
in
rustPlatform.buildRustPackage {
pname = "framework_tool";
version = "0.5.0";
src = buildSrc;
cargoLock = {
lockFile = ./Cargo.lock;
outputHashes = gitDependencyHashes;
};
buildType = profile;
# Build only the tool, not the UEFI package
buildPhase = ''
runHook preBuild
cargo build \
${if release then "--release" else ""} \
-p framework_tool \
${featuresStr}
runHook postBuild
'';
# Run tests for framework_lib
checkPhase = ''
runHook preCheck
cargo test -p framework_lib
runHook postCheck
'';
installPhase = ''
runHook preInstall
mkdir -p $out/bin
cp target/${profile}/framework_tool $out/bin/
runHook postInstall
'';
nativeBuildInputs = commonNativeBuildInputs;
buildInputs = commonBuildInputs;
# Environment variables for C library bindings
OPENSSL_NO_VENDOR = "1";
LIBGIT2_NO_VENDOR = "1";
};
# MinGW cross-compiler for Windows builds
mingw = pkgs.pkgsCross.mingwW64.stdenv.cc;
mingwPthreads = pkgs.pkgsCross.mingwW64.windows.pthreads;
# Build function for Windows cross-compilation (Linux -> Windows)
buildFrameworkToolWindows = { release ? false }:
let
profile = if release then "release" else "debug";
in
rustPlatformWindows.buildRustPackage {
pname = "framework_tool";
version = "0.5.0";
src = buildSrc;
cargoLock = {
lockFile = ./Cargo.lock;
outputHashes = gitDependencyHashes;
};
buildType = profile;
buildNoDefaultFeatures = true;
# Disable cargo-auditable as it's incompatible with cross-compilation
auditable = false;
buildPhase = ''
runHook preBuild
cargo build \
${if release then "--release" else ""} \
--target x86_64-pc-windows-gnu \
-p framework_tool
runHook postBuild
'';
# Skip check phase - can't run .exe on Linux
doCheck = false;
installPhase = ''
runHook preInstall
mkdir -p $out/bin
cp target/x86_64-pc-windows-gnu/${profile}/framework_tool.exe $out/bin/
runHook postInstall
'';
nativeBuildInputs = [ mingw ];
CARGO_TARGET_X86_64_PC_WINDOWS_GNU_LINKER = "${mingw}/bin/x86_64-w64-mingw32-gcc";
CC_x86_64_pc_windows_gnu = "${mingw}/bin/x86_64-w64-mingw32-gcc";
CXX_x86_64_pc_windows_gnu = "${mingw}/bin/x86_64-w64-mingw32-g++";
AR_x86_64_pc_windows_gnu = "${mingw}/bin/x86_64-w64-mingw32-ar";
CARGO_TARGET_X86_64_PC_WINDOWS_GNU_RUSTFLAGS = "-L native=${mingwPthreads}/lib";
};
# Build function for UEFI application
buildFrameworkUefi = { release ? false, features ? [] }:
let
profile = if release then "release" else "debug";
featuresStr = if features == [] then "" else "--features ${builtins.concatStringsSep "," features}";
in
rustPlatform.buildRustPackage {
pname = "framework_uefi";
version = "0.5.0";
src = buildSrc;
cargoLock = {
lockFile = ./Cargo.lock;
outputHashes = gitDependencyHashes;
};
buildType = profile;
buildNoDefaultFeatures = true;
# Disable cargo-auditable as it's incompatible with UEFI linker
auditable = false;
# Target for UEFI - passed via args to avoid affecting build scripts
buildPhase = ''
runHook preBuild
cargo build \
${if release then "--release" else ""} \
--target x86_64-unknown-uefi \
-p framework_uefi \
${featuresStr}
runHook postBuild
'';
# Skip check phase - UEFI binaries can't be tested on host
doCheck = false;
installPhase = ''
runHook preInstall
mkdir -p $out/bin
cp target/x86_64-unknown-uefi/${profile}/uefitool.efi $out/bin/
runHook postInstall
'';
nativeBuildInputs = commonNativeBuildInputs;
buildInputs = commonBuildInputs;
# Environment variables for C library bindings
OPENSSL_NO_VENDOR = "1";
LIBGIT2_NO_VENDOR = "1";
};
# Package definitions
framework-tool-debug = buildFrameworkTool { release = false; };
framework-tool-release = buildFrameworkTool { release = true; };
framework-uefi-debug = buildFrameworkUefi { release = false; };
framework-uefi-release = buildFrameworkUefi { release = true; };
framework-tool-windows = buildFrameworkToolWindows { release = true; };
framework-tool-windows-debug = buildFrameworkToolWindows { release = false; };
# Wrapper script to run the UEFI build in an emulator
run-qemu = pkgs.writeShellScriptBin "run-framework-uefi-qemu" ''
set -e
# Create temporary directory for ESP and OVMF vars
TMPDIR=$(mktemp -d)
trap "rm -rf $TMPDIR" EXIT
# Set up ESP filesystem structure
mkdir -p "$TMPDIR/esp/efi/boot"
cp ${framework-uefi-debug}/bin/uefitool.efi "$TMPDIR/esp/efi/boot/bootx64.efi"
# Copy OVMF_VARS.fd to temp (it needs to be writable)
cp ${pkgs.OVMF.fd}/FV/OVMF_VARS.fd "$TMPDIR/OVMF_VARS.fd"
chmod 644 "$TMPDIR/OVMF_VARS.fd"
echo "Starting QEMU with Framework UEFI tool..."
echo "Debug output will be written to: $TMPDIR/debug.log"
${pkgs.qemu}/bin/qemu-system-x86_64 \
''${QEMU_KVM:+-enable-kvm} \
-M q35 \
-m 1024 \
-net none \
-serial stdio \
-debugcon "file:$TMPDIR/debug.log" -global isa-debugcon.iobase=0x402 \
-usb \
-drive if=pflash,format=raw,readonly=on,file=${pkgs.OVMF.fd}/FV/OVMF_CODE.fd \
-drive if=pflash,format=raw,readonly=off,file="$TMPDIR/OVMF_VARS.fd" \
-drive format=raw,file=fat:rw:"$TMPDIR/esp" \
"$@"
'';
# Wrapper for release QEMU build
run-qemu-release = pkgs.writeShellScriptBin "run-framework-uefi-qemu" ''
set -e
TMPDIR=$(mktemp -d)
trap "rm -rf $TMPDIR" EXIT
mkdir -p "$TMPDIR/esp/efi/boot"
cp ${framework-uefi-release}/bin/uefitool.efi "$TMPDIR/esp/efi/boot/bootx64.efi"
cp ${pkgs.OVMF.fd}/FV/OVMF_VARS.fd "$TMPDIR/OVMF_VARS.fd"
chmod 644 "$TMPDIR/OVMF_VARS.fd"
echo "Starting QEMU with Framework UEFI tool (release)..."
${pkgs.qemu}/bin/qemu-system-x86_64 \
''${QEMU_KVM:+-enable-kvm} \
-M q35 \
-m 1024 \
-net none \
-serial stdio \
-debugcon "file:$TMPDIR/debug.log" -global isa-debugcon.iobase=0x402 \
-usb \
-drive if=pflash,format=raw,readonly=on,file=${pkgs.OVMF.fd}/FV/OVMF_CODE.fd \
-drive if=pflash,format=raw,readonly=off,file="$TMPDIR/OVMF_VARS.fd" \
-drive format=raw,file=fat:rw:"$TMPDIR/esp" \
"$@"
'';
in
{
checks = {
inherit framework-tool-release framework-uefi-release;
};
packages = {
default = framework-tool-release;
tool = framework-tool-release;
tool-debug = framework-tool-debug;
uefi = framework-uefi-release;
uefi-debug = framework-uefi-debug;
windows = framework-tool-windows;
windows-debug = framework-tool-windows-debug;
run-qemu = run-qemu;
run-qemu-release = run-qemu-release;
};
# Convenience apps for `nix run`
apps = {
default = flake-utils.lib.mkApp { drv = framework-tool-release; exePath = "/bin/framework_tool"; };
tool = flake-utils.lib.mkApp { drv = framework-tool-release; exePath = "/bin/framework_tool"; };
qemu = flake-utils.lib.mkApp { drv = run-qemu; };
qemu-release = flake-utils.lib.mkApp { drv = run-qemu-release; };
};
devShells.cross-windows = pkgs.mkShell {
packages = [
rustToolchainWindows
];
# Ensure build scripts (e.g. libgit2-sys) use the native host compiler
HOST_CC = "cc";
CARGO_TARGET_X86_64_PC_WINDOWS_GNU_LINKER = "${mingw}/bin/x86_64-w64-mingw32-gcc";
CC_x86_64_pc_windows_gnu = "${mingw}/bin/x86_64-w64-mingw32-gcc";
CXX_x86_64_pc_windows_gnu = "${mingw}/bin/x86_64-w64-mingw32-g++";
AR_x86_64_pc_windows_gnu = "${mingw}/bin/x86_64-w64-mingw32-ar";
CARGO_TARGET_X86_64_PC_WINDOWS_GNU_RUSTFLAGS = "-L native=${mingwPthreads}/lib";
};
devShells.default = pkgs.mkShell {
packages = with pkgs; [
rustToolchain
# Development tools
gnumake
qemu
parted
OVMF
# Build dependencies
pkg-config
openssl
libgit2
zlib
] ++ pkgs.lib.optionals pkgs.stdenv.isLinux [
systemdLibs # libudev
];
OPENSSL_NO_VENDOR = "1";
LIBGIT2_NO_VENDOR = "1";
# Set up OVMF symlinks for `make run` compatibility
shellHook = ''
if [ ! -d ovmf ] || [ ! -e ovmf/OVMF_CODE.fd ] || [ ! -e ovmf/OVMF_VARS.fd ]; then
mkdir -p ovmf
ln -sf ${pkgs.OVMF.fd}/FV/OVMF_CODE.fd ovmf/OVMF_CODE.fd
# OVMF_VARS needs to be writable, so copy it instead of symlinking
cp -f ${pkgs.OVMF.fd}/FV/OVMF_VARS.fd ovmf/OVMF_VARS.fd
chmod 644 ovmf/OVMF_VARS.fd
echo "OVMF files set up in ovmf/"
fi
'';
};
}
);
}