-
Notifications
You must be signed in to change notification settings - Fork 34
/
flake.nix
276 lines (245 loc) · 8.76 KB
/
flake.nix
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
{
description = "Cannoli development shell";
nixConfig = {
extra-substituters = ["https://cannoli.cachix.org"];
extra-trusted-public-keys = ["cannoli.cachix.org-1:nFKY7lRczFkkHacy6/OlfmpOU22MeEiDo90YV0QkVoQ="];
};
inputs = {
# Use the rolling-release cycle of nixpkgs for most tools
# flake.lock will pin this to a fixed version, update with "nix flake update"
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
# Pull in nixpkgs at a point in time where qemu-8.1.2 existed
# Use this to find the applicable git hash:
# https://lazamar.co.uk/nix-versions/?channel=nixpkgs-unstable&package=qemu
qemu-nixpkgs.url = "github:NixOS/nixpkgs/6eed4c2552c41690535d08a2e071bca005226a4a";
# Rust toolchain
rust-overlay = {
url = "github:oxalica/rust-overlay";
inputs.nixpkgs.follows = "nixpkgs";
};
# Builds rust crates, runs tests
crane = {
url = "github:ipetkov/crane";
inputs.nixpkgs.follows = "nixpkgs";
};
};
outputs = {
self,
nixpkgs,
qemu-nixpkgs,
rust-overlay,
crane,
...
}: let
# see qemu's configure script for possible targets:
# ./configure --help
cpuTargets = [
"aarch64"
"aarch64_be"
"alpha"
"arm"
"armeb"
"cris"
"hppa"
"i386"
"loongarch64"
"m68k"
"microblaze"
"microblazeel"
"mips"
"mips64"
"mips64el"
"mipsel"
"mipsn32"
"mipsn32el"
"nios2"
"or1k"
"ppc"
"ppc64"
"ppc64le"
"riscv32"
"riscv64"
"s390x"
"sh4"
"sh4eb"
"sparc"
"sparc32plus"
"sparc64"
"x86_64"
"xtensa"
"xtensaeb"
];
# concat "-linux-user" to the cpu names to create the actual configure strings
hostCpuTargets = map (arch: arch + "-linux-user") cpuTargets;
# stripped down cannoli sources that qemu needs to build
# (prevents unnecessary rebuilds of qemu after unrelated changes in this repo)
fs = lib.fileset;
cannoli-headers = fs.toSource {
root = ./.;
fileset = fs.intersection
(fs.gitTracked ./.)
(fs.fileFilter ({name,...}: name == "cannoli.h") ./.);
};
qemuSrc = pkgs: pkgs.fetchurl {
url = "https://download.qemu.org/qemu-8.1.2.tar.xz";
sha256 = "sha256-VBUmp2RXbrSU0v9exGrrJT5i6ikDXRwjwKivTmzU8Ic=";
};
# nixpkgs overlay defining the qemu-cannoli package
qemu-cannoli-overlay = final: prev: {
# a function that takes a package, and applies optimizations
optimize = pkg:
(pkg.overrideAttrs (old: {
# inject these flags when the compiler gets invoked
# unfortunately won't show up in configure script outputs :(
NIX_CFLAGS_COMPILE = [ (old.NIX_CFLAGS_COMPILE or "") ] ++ [
"-O3" # more optimizations than the nixpkgs default of -O2
"-march=x86-64-v3" # this targets modern x86_64 but not including
# avx512, should be a good balance of modern
# features (e.g. avx2) but still run on CPUs
# within the past 8 years at least.
];
})).override rec {
# use the newest gcc available, assuming it optimizes better
stdenv = final.gcc12Stdenv;
buildPackages.stdenv = stdenv;
};
qemu-cannoli = final.optimize ((final.qemu.overrideAttrs (old: {
pname = "qemu-cannoli";
src = qemuSrc final;
configureFlags = old.configureFlags ++ [
# speed up builds with these flags
"--disable-docs"
"--disable-tools"
"--disable-download"
# add the cannoli configure flag
"--with-cannoli=${cannoli-headers}"
];
patches = [ ./qemu_patches.patch ];
})).override {
inherit hostCpuTargets;
});
};
qemu-pkgs = import qemu-nixpkgs {
system = "x86_64-linux";
overlays = [
qemu-cannoli-overlay
];
};
pkgs = import nixpkgs {
system = "x86_64-linux";
overlays = [
(import rust-overlay)
];
};
lib = pkgs.lib;
rustToolchain = pkgs.rust-bin.fromRustupToolchainFile ./rust-toolchain.toml;
craneLib = (crane.mkLib pkgs).overrideToolchain rustToolchain;
# environment variable for bindgen
LIBCLANG_PATH = "${pkgs.llvmPackages_15.libclang.lib}/lib";
# compile dependencies once
vendoredCrates = craneLib.vendorCargoDeps {
cargoLock = ./Cargo.lock;
};
srcForCrate = cratePath: fs.toSource {
root = ./.;
fileset = fs.unions [
./Cargo.lock ./.cargo
# common crates needed by everything in the workspace
./jitter ./mempipe ./cannoli ./extensions/arch
# keep all Cargo.tomls as scaffolding for workspace
(fs.fileFilter (file: file.name == "Cargo.toml") ./.)
# add the source of the crate we want to compile
(./. + ("/" + cratePath))
];
};
crateNameFromPath = cratePath: (lib.importTOML (./. + ("/" + cratePath + "/Cargo.toml"))).package.name;
workspaceDeps = cratePath: craneLib.buildDepsOnly {
src = ./.;
pname = crateNameFromPath cratePath;
cargoVendorDir = vendoredCrates;
cargoExtraArgs = "-p ${crateNameFromPath cratePath}";
};
buildWorkspaceCrate = cratePath: craneLib.buildPackage {
src = srcForCrate cratePath;
pname = crateNameFromPath cratePath;
# create a src/lib.rs next to every Cargo.toml
postUnpack = "${lib.getExe pkgs.fd} Cargo.toml -tf -x mkdir -p {//}/src ';' -x touch {//}/src/lib.rs";
cargoArtifacts = workspaceDeps cratePath;
cargoVendorDir = vendoredCrates;
cargoExtraArgs = "-p ${crateNameFromPath cratePath}";
doNotLinkInheritedArtifacts = true;
inherit LIBCLANG_PATH;
};
workspaceCratePaths = (lib.importTOML ./Cargo.toml).workspace.members;
# attrSet of crateName -> derivation for this workspace
workspaceMembers = lib.listToAttrs (lib.forEach workspaceCratePaths (path: {
name = crateNameFromPath path;
value = buildWorkspaceCrate path;
}));
# builds every crate in the workspace
workspace = pkgs.symlinkJoin {
name = "cannoli-workspace";
paths = lib.attrValues workspaceMembers;
};
in
with pkgs; {
# run "nix build" to build qemu with cannoli patches. binaries will be
# placed in ./result/bin/
packages.x86_64-linux = rec {
cannoli = qemu-pkgs.qemu-cannoli;
default = runCommand "qemu-cannoli" {} ''
mkdir -p $out/bin
cp -vP ${cannoli}/bin/* $out/bin/
rm $out/bin/{qemu-ga,qemu-kvm}
'';
# build all workspace crates
inherit workspace;
};
# ci checks, run with "nix flake check"
checks.x86_64-linux = {
inherit (qemu-pkgs) qemu-cannoli;
# build and run cargo tests for all workspace crates
inherit workspace;
} //
# run end-to-end examples (integration tests)
lib.mapAttrs (name: env: pkgs.runCommand name (env // {
# injected/derived environment variables
CANNOLI_SERVER = "${workspaceMembers."${env.example}"}/bin/${env.example}";
CANNOLI_JITTER = "${workspaceMembers."${env.example}"}/lib/lib${env.example}.so";
CANNOLI_QEMU = "${qemu-pkgs.qemu-cannoli}/bin/${env.qemu}";
}) ''
$preRun
${pkgs.python3}/bin/python3 ${./examples/ci_runner.py}
touch $out
'') {
symbolizer-mipsel-run = {
example = "symbolizer";
qemu = "qemu-mipsel";
CANNOLI_TARGET = "${./examples/symbolizer/example_app}";
preRun = ''cp ${./examples/symbolizer/symbols.txt} symbols.txt'';
};
symbolizer-riscv64-run = {
example = "symbolizer";
qemu = "qemu-riscv64";
CANNOLI_TARGET = "${./examples/symbolizer/example_app64}";
preRun = ''cp ${./examples/symbolizer/symbols.txt} symbols.txt'';
};
};
# run "nix develop" to drop into a shell that has qemu with cannoli,
# and a known-good nightly rust toolchain
devShells.x86_64-linux.default = mkShell {
# packages available in the shell
buildInputs = [
qemu-pkgs.qemu-cannoli
gnumake
pkgsCross.mipsel-linux-gnu.pkgsStatic.stdenv.cc
rustToolchain
nix
];
shellHook = ''
export LIBCLANG_PATH=${LIBCLANG_PATH}
echo cannoli developer shell activated
'';
};
};
}