-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathflake.nix
More file actions
254 lines (218 loc) · 6.32 KB
/
flake.nix
File metadata and controls
254 lines (218 loc) · 6.32 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
{
description = "A very basic flake";
inputs = {
nixpkgs.url = "github:nixos/nixpkgs/nixpkgs-unstable";
fenix = {
url = "github:nix-community/fenix";
inputs.nixpkgs.follows = "nixpkgs";
};
naersk = {
url = "github:nix-community/naersk";
inputs.nixpkgs.follows = "nixpkgs";
};
pyproject-nix = {
url = "github:pyproject-nix/pyproject.nix";
inputs.nixpkgs.follows = "nixpkgs";
};
uv2nix = {
url = "github:adisbladis/uv2nix";
inputs.nixpkgs.follows = "nixpkgs";
inputs.pyproject-nix.follows = "pyproject-nix";
};
pyproject-build-systems = {
url = "github:pyproject-nix/build-system-pkgs";
inputs.nixpkgs.follows = "nixpkgs";
inputs.pyproject-nix.follows = "pyproject-nix";
inputs.uv2nix.follows = "uv2nix";
};
};
outputs =
{
self,
nixpkgs,
fenix,
naersk,
pyproject-nix,
uv2nix,
pyproject-build-systems,
}:
let
system = "x86_64-linux";
pkgs = nixpkgs.legacyPackages.${system}.extend fenix.overlays.default;
# Flake options
enableBPF = true;
enableRust = true;
enableKdf = true;
buildLib = pkgs.callPackage ./build { };
linuxConfigs = pkgs.callPackage ./configs/kernel.nix {
inherit
enableBPF
enableRust
enableKdf
;
};
inherit (linuxConfigs) kernelArgs kernelConfig;
configModule = buildLib.buildKernelConfigModule {
inherit (kernelConfig)
structuredExtraConfig
;
inherit nixpkgs;
};
# Config file derivation
configfile = buildLib.buildKernelConfig {
inherit (kernelConfig)
generateConfigFlags
;
inherit configModule kernel nixpkgs;
};
# Kernel derivation.
kernelDrv = buildLib.buildKernel {
inherit (kernelArgs)
src
modDirVersion
version
kernelPatches
;
inherit configModule configfile nixpkgs;
};
linuxDev = pkgs.linuxPackagesFor kernelDrv;
kernel = linuxDev.kernel;
buildRustModule = buildLib.buildRustModule { inherit kernel; };
buildCModule = buildLib.buildCModule {
inherit kernel;
};
modules = [ cModule ] ++ pkgs.lib.optional enableRust rustModule;
initramfs = buildLib.buildInitramfs {
inherit kernel modules;
extraBin = {
strace = "${pkgs.strace}/bin/strace";
}
// pkgs.lib.optionalAttrs enableBPF {
stackcount = "${pkgs.bcc}/bin/stackcount";
};
storePaths = [
pkgs.foot.terminfo
]
++ pkgs.lib.optionals enableBPF [
pkgs.bcc
pkgs.python3
];
};
runQemu = buildLib.buildQemuCmd { inherit kernel initramfs; };
runGdb = buildLib.buildGdbCmd { inherit kernel modules; };
cModule = buildCModule {
name = "helloworld";
src = ./modules/helloworld;
};
rustModule = buildRustModule {
name = "rust-out-of-tree";
src = ./modules/rust;
};
ebpf-stacktrace = pkgs.stdenv.mkDerivation {
name = "ebpf-stacktrace";
src = ./ebpf/ebpf_stacktrace;
installPhase = ''
runHook preInstall
mkdir $out
cp ./helloworld $out/
cp ./helloworld_dbg $out/
cp runit.sh $out/
runHook postInstall
'';
meta.platforms = [ "x86_64-linux" ];
};
genRustAnalyzer = pkgs.writers.writePython3Bin "generate-rust-analyzer" { } (
builtins.readFile ./scripts/generate_rust_analyzer.py
);
kdf-init = pkgs.callPackage ./kdf-init {
inherit fenix naersk;
};
kdf-cli = pkgs.callPackage ./kdf-cli {
inherit
uv2nix
pyproject-nix
pyproject-build-systems
kdf-init
;
};
devShell =
let
# Rust toolchain with musl target for static compilation
rustToolchain = pkgs.fenix.combine [
pkgs.fenix.stable.rustc
pkgs.fenix.stable.cargo
pkgs.fenix.stable.rustfmt
pkgs.fenix.stable.clippy
pkgs.fenix.targets.x86_64-unknown-linux-musl.stable.rust-std
];
# Create Python set with editable overlay for kdf-cli development
editablePythonSet = kdf-cli.pythonSet.overrideScope kdf-cli.editableOverlay;
# Create virtualenv with all dependencies (including dev dependencies)
kdf-cli-virtualenv = editablePythonSet.mkVirtualEnv "kdf-cli-dev-env" kdf-cli.workspace.deps.all;
nativeBuildInputs =
with pkgs;
[
bear # for compile_commands.json, use bear -- make
git
gdb
qemu
pahole
just
uv
# static analysis
flawfinder
cppcheck
sparse
# Python tools
ruff
ty
# kdf-cli
virtiofsd
kdf-cli-virtualenv
]
++ lib.optionals enableRust [
rustToolchain
genRustAnalyzer
];
buildInputs = [ ];
in
pkgs.mkShell {
inherit buildInputs nativeBuildInputs;
KERNEL = kernel.dev;
KERNEL_VERSION = kernel.modDirVersion;
KERNEL_IMG_DIR = kernel;
RUST_LIB_SRC = pkgs.rustPlatform.rustLibSrc;
# UV environment variables for kdf-cli development
UV_NO_SYNC = "1";
UV_PYTHON = editablePythonSet.python.interpreter;
UV_PYTHON_DOWNLOADS = "never";
shellHook = ''
unset PYTHONPATH
export KDF_CLI_ROOT=$(git rev-parse --show-toplevel)/kdf-cli
'';
};
in
{
lib = {
builders = import ./build/default.nix;
};
packages.${system} = {
inherit
initramfs
kernelDrv
kernel
cModule
ebpf-stacktrace
rustModule
genRustAnalyzer
kdf-init
kdf-cli
runGdb
runQemu
;
kernelConfig = configfile;
};
devShells.${system}.default = devShell;
formatter.${system} = pkgs.nixfmt-tree;
};
}