Skip to content

Commit 77be405

Browse files
committed
feat: support to run with nix
1 parent b76289a commit 77be405

File tree

1 file changed

+69
-19
lines changed

1 file changed

+69
-19
lines changed

flake.nix

Lines changed: 69 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
description = "Memtest rewritten in Rust";
2+
description = "Powered Hardware test tool written in Rust";
33

44
inputs = {
55
crane.url = "github:ipetkov/crane";
@@ -18,15 +18,33 @@
1818
lib = nixpkgs.lib;
1919
pkgs = nixpkgs.legacyPackages.${system};
2020
fenix = fenix-src.packages.${system};
21-
systemToTarget = {
22-
"aarch64-darwin" = "aarch64-apple-darwin";
23-
"aarch64-linux" = "aarch64-unknown-linux-gnu";
24-
"i686-linux" = "i686-unknown-linux-gnu";
25-
"x86_64-darwin" = "x86_64-apple-darwin";
26-
"x86_64-linux" = "x86_64-unknown-linux-gnu";
21+
ovmf_hashes = {
22+
x86_64 = {
23+
vars = "sha256-btmHrzo8FVvnFmX1EOrj4Aftqbi5Sv1Z1F6RxKEVZcw=";
24+
code = "sha256-PI4QAiPjx6+P7LawNzP4kkWaXTEqOaapVdpj6GUm/6Q=";
25+
};
26+
aarch64 = {
27+
vars = "sha256-i2NMHmvRFgeFC2kRH2xNvRWD270UYNrHLbrL3BpKEwo=";
28+
code = "sha256-j7i2aFmrXrStxrIv9zzpslmLLbqBe3igeDTbw8y7scQ=";
29+
};
30+
riscv64 = {
31+
vars = "sha256-i2NMHmvRFgeFC2kRH2xNvRWD270UYNrHLbrL3BpKEwo=";
32+
code = "sha256-b24MAyRmI98zE40Auw+ZX2HbHbQ9ln8HLlRTxwL1emY=";
33+
};
2734
};
28-
29-
hostTarget = systemToTarget.${system} or (throw "Unsupported system: ${system}");
35+
systemToTarget = system:
36+
let
37+
arch = builtins.elemAt (lib.splitString "-" system) 0;
38+
os = builtins.elemAt (lib.splitString "-" system) 1;
39+
in
40+
if os == "darwin" then
41+
"${arch}-apple-darwin"
42+
else if os == "linux" then
43+
"${arch}-unknown-linux-gnu"
44+
else
45+
throw "Unsupported system: ${system}";
46+
47+
hostTarget = systemToTarget system;
3048
toolchain = target: fenix.combine [
3149
(fenix.targets.${hostTarget}.default.rust-std)
3250
(fenix.targets.${hostTarget}.default.toolchain)
@@ -49,9 +67,26 @@
4967
'';
5068
};
5169

70+
ovmf_pkg = arch: name: let
71+
version = "2025-02-18";
72+
in pkgs.stdenv.mkDerivation {
73+
inherit version;
74+
pname = "ovmf_${arch}";
75+
src = pkgs.fetchurl {
76+
url = "https://github.com/osdev0/edk2-ovmf-nightly/releases/latest/download/ovmf-${name}-${arch}.fd";
77+
hash = ovmf_hashes.${arch}.${name};
78+
};
79+
80+
unpackPhase = ''
81+
mkdir -p $out
82+
cp $src $out/ovmf-${name}-${arch}.fd
83+
'';
84+
};
5285

5386
mkPackage = { arch, name, target, ... }: let
5487
target_name = lib.toUpper (builtins.replaceStrings [ "-" ] [ "_" ] target);
88+
ovmf_vars = ovmf_pkg arch "vars";
89+
ovmf_code= ovmf_pkg arch "code";
5590
in (craneLib target).buildPackage {
5691
pname = "memsos-${name}";
5792
version = "0.1.0";
@@ -73,6 +108,10 @@
73108
mkdir -p $out/iso_root/boot
74109
cp $out/bin/memsos-boot $out/iso_root/boot/kernel
75110
111+
mkdir -p $out/ovmf
112+
cp ${ovmf_vars}/ovmf-vars-${arch}.fd $out/ovmf/ovmf-vars-${arch}.fd
113+
cp ${ovmf_code}/ovmf-code-${arch}.fd $out/ovmf/ovmf-code-${arch}.fd
114+
76115
mkdir -p $out/iso_root/boot/limine
77116
cp "$LIMINE_DIR/limine-bios.sys" $out/iso_root/boot/limine/
78117
cp "$LIMINE_DIR/limine-bios-cd.bin" $out/iso_root/boot/limine/
@@ -119,35 +158,46 @@
119158
};
120159
};
121160

122-
apps = lib.listToAttrs (map ({ arch, name, target, ... }@args: {
123-
inherit name;
124-
value = {
125-
type = "app";
126-
program = pkgs.writeShellScriptBin "run-${name}" ''
161+
apps = lib.listToAttrs (map ({ arch, name, target, ... }@args: let
162+
pkg = mkPackage args;
163+
run = pkgs.writeShellScriptBin "run-${name}" ''
127164
qemu-system-${arch} \
128-
-cdrom ${mkPackage args}/memsos-${name}.iso \
165+
-cdrom ${pkg}/memsos-${name}.iso \
129166
-M q35 \
130167
-no-reboot \
131168
-no-shutdown \
169+
-drive if=pflash,unit=0,format=raw,file=${pkg}/ovmf/ovmf-code-${arch}.fd,readonly=on \
170+
-drive if=pflash,unit=1,format=raw,file=${pkg}/ovmf/ovmf-vars-${arch}.fd,readonly=on \
132171
-d int
133172
'';
173+
in {
174+
inherit name;
175+
value = {
176+
type = "app";
177+
program = "${run}/bin/run-${name}";
134178
};
135179
}) architectures) // {
136180
list = {
137181
type = "app";
138182
program = "${listApps}/bin/list-apps";
139183
};
140184
# Default App
141-
default = {
142-
type = "app";
143-
program = pkgs.writeShellScriptBin "run-default" ''
185+
default = let
186+
arch = "x86_64";
187+
pkg = mkPackage { arch = arch; name = "x86_64"; target = "x86_64-unknown-none"; };
188+
run = pkgs.writeShellScriptBin "run-default" ''
144189
qemu-system-x86_64 \
145-
-cdrom ${mkPackage { arch = "x86_64"; name = "x86_64"; target = "x86_64-unknown-none"; }}/memsos-x86_64.iso \
190+
-cdrom ${pkg}/memsos-x86_64.iso \
146191
-M q35 \
147192
-no-reboot \
148193
-no-shutdown \
194+
-drive if=pflash,unit=0,format=raw,file=${pkg}/ovmf/ovmf-code-${arch}.fd,readonly=on \
195+
-drive if=pflash,unit=1,format=raw,file=${pkg}/ovmf/ovmf-vars-${arch}.fd,readonly=on \
149196
-d int
150197
'';
198+
in {
199+
type = "app";
200+
program = "${run}/bin/run-default";
151201
};
152202
};
153203
}

0 commit comments

Comments
 (0)