Repository navigation
Expand file tree
/
Copy pathflake.nix
More file actions
388 lines (383 loc) · 18.8 KB
/
Copy pathflake.nix
File metadata and controls
388 lines (383 loc) · 18.8 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
{
description = "blaze: cargo build, test and run from a daemon that keeps your workspace compiled, with live edits";
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.eachSystem [ "x86_64-linux" "aarch64-linux" "aarch64-darwin" ] (system:
let
pkgs = import nixpkgs {
inherit system;
overlays = [ rust-overlay.overlays.default ];
};
# The one toolchain blaze is pinned to: the dated nightly in pins/toolchain
# (`nightly-YYYY-MM-DD`, moved by harness/toolchain_bump.py, which also moves the
# rust-overlay input to one that has it). rust-src is for rust-analyzer in an editor of
# this repository; blaze reads none of it (its standard library is library/,
# docs/toolchain.md).
pin = pkgs.lib.removePrefix "nightly-" (pkgs.lib.fileContents ./pins/toolchain);
rust = pkgs.rust-bin.nightly.${pin}.default.override {
extensions = [ "rust-src" ];
};
# The same nightly with rustc_codegen_cranelift, for the setups bench.py compares with
# (`--setups`, docs/benchmarks.md).
nightly = pkgs.rust-bin.nightly.${pin}.default.override {
extensions = [ "rustc-codegen-cranelift-preview" ];
};
# The one LLVM the `llvm` feature builds against, for every build of it: the dev shells
# here, CI (`.#llvm`, .github/actions/toolchain), the flake's `dist` and the release.
# pins/llvm.json pins it: LLVM 23.1.4, built for each target by
# .github/scripts/llvm.py (the llvm workflow) and fetched here by its sha256. Its static libraries, headers, llvm-config,
# llvm-dwarfdump and llvm-pdbutil; LLVM_SYS_231_PREFIX names it, and
# vendor/llvm-sys links it statically, and its build script (pin.rs) refuses any other. Its tools were
# built on Ubuntu 22.04 with the C++ runtime linked in: on Linux they are pointed at the
# store's libc.
llvmPin = builtins.fromJSON (builtins.readFile ./pins/llvm.json);
llvm =
let pin = llvmPin.targets.${triple} or (throw "pins/llvm.json has no LLVM for ${triple}: run the llvm workflow");
in pkgs.stdenv.mkDerivation {
pname = "blaze-llvm";
inherit (llvmPin) version;
src = pkgs.fetchurl { url = "${llvmPin.release}/${pin.name}"; inherit (pin) sha256; };
nativeBuildInputs = pkgs.lib.optionals pkgs.stdenv.hostPlatform.isLinux [ pkgs.autoPatchelfHook ];
buildInputs = pkgs.lib.optionals pkgs.stdenv.hostPlatform.isLinux [ pkgs.stdenv.cc.cc.lib ];
dontConfigure = true;
dontBuild = true;
# The archives are what the release links: not stripped or rewritten.
dontStrip = true;
installPhase = "cp -r . $out";
};
# blaze lays out the same archive itself and sets this for llvm-sys's and blaze-stencil's
# build scripts over the shell's value (Cargo.toml's [workspace.metadata.blaze.native.llvm]);
# the shell's serves a stage 0 older than that table, and tools other than blaze.
llvmEnv = { LLVM_SYS_231_PREFIX = "${llvm}"; };
# The Python workspace uses Nix's pinned interpreter on NixOS, where uv's downloaded
# interpreters cannot run. uv itself comes from the same flake.lock as the toolchain.
scripts = {
packages = [ pkgs.uv pkgs.python313 pkgs.nodejs ];
UV_PYTHON = "${pkgs.python313}/bin/python3";
UV_PYTHON_DOWNLOADS = "never";
};
# What harness/bench.py runs in on the benchmark machine (docs/benchmarks.md): the
# pinned toolchain, the LLVM 23 blaze links, and the C side the projects' build
# scripts use. Nothing here wraps rustc (no sccache) or replaces the default linker:
# mold and wild are on PATH for the setups that ask for them with `-fuse-ld`. Every
# library directory here costs every link a `realpath` in Nix's linker wrapper, which
# cargo pays and blaze does not: what only one project needs goes in a shell of its own.
bench = llvmEnv // {
packages = [
rust
pkgs.python313
pkgs.uv
pkgs.nodejs
pkgs.git
pkgs.pkg-config
pkgs.openssl
llvm
pkgs.libffi
pkgs.libxml2
pkgs.zlib
pkgs.ncurses
] ++ pkgs.lib.optionals pkgs.stdenv.hostPlatform.isLinux [
# bevy's audio, input and windowing crates link these.
pkgs.alsa-lib
pkgs.udev
pkgs.wayland
pkgs.libxkbcommon
pkgs.mold
pkgs.wild
] ++ [
# dx (Dioxus hot-patching): harness/hotpatch.py, which used it, is deleted; it goes with the flake.
pkgs.dioxus-cli
];
BLAZE_NIGHTLY = "${nightly}";
inherit (scripts) UV_PYTHON UV_PYTHON_DOWNLOADS;
# A C compiler as a distribution ships it: the wrapper's _FORTIFY_SOURCE warns at the
# -O0 of a debug build, which fails configure probes (jemalloc's, in fd).
hardeningDisable = [ "all" ];
};
rustPlatform = pkgs.makeRustPlatform {
cargo = rust;
rustc = rust;
};
src = self;
# The one version, from the workspace's Cargo.toml.
version = (builtins.fromTOML (builtins.readFile ./Cargo.toml)).workspace.package.version;
# The one build of blaze, the release workflow's recipe (.github/scripts/build_release.py):
# the `dist` profile optimized with the profile tools/pgo/profiles.json pins for this system,
# fetched by its sha256. The binaries run each other from next to themselves, unwrapped.
# `--nix`: they load nixpkgs' glibc, so the release's floor of RHEL 9's glibc is not theirs
# (build_release.py checks that instead; release.yaml keeps the floor).
triple = {
x86_64-linux = "x86_64-unknown-linux-gnu";
aarch64-linux = "aarch64-unknown-linux-gnu";
aarch64-darwin = "aarch64-apple-darwin";
}.${system};
pgo = builtins.fromJSON (builtins.readFile ./tools/pgo/profiles.json);
pgoProfile =
let pin = pgo.targets.${triple} or (throw "tools/pgo/profiles.json has no profile for ${triple}: run the pgo workflow");
in pkgs.fetchurl { url = "${pgo.release}/${pin.name}"; inherit (pin) sha256; };
dist = rustPlatform.buildRustPackage (llvmEnv // {
pname = "blaze-dist";
inherit version src;
# `blaze-engine --version` names the commit (a dirty tree has none of its own).
BLAZE_GIT_SHA = self.shortRev or self.dirtyShortRev or "";
cargoLock.lockFile = ./Cargo.lock;
# otool: build_release.py checks that no binary loads libLLVM.
nativeBuildInputs = [ pkgs.python3 ] ++ pkgs.lib.optionals pkgs.stdenv.hostPlatform.isDarwin [ pkgs.cctools ];
doCheck = false;
# cargo-auditable wraps rustc for the workspace's crates, which changes their symbols:
# the profile would find none of them.
auditable = false;
buildPhase = ''
runHook preBuild
python3 -I .github/scripts/build_release.py --target ${triple} --profdata ${pgoProfile} --out shipped --nix
runHook postBuild
'';
installPhase = ''
runHook preInstall
mkdir -p $out/bin $out/share/blaze
cp shipped/* $out/bin/
# The standard library's source, which blaze finds above its programs
# (crates/blaze-cargo/src/toolchain/library.rs).
cp -r library $out/share/blaze/library
runHook postInstall
'';
});
# `nix run .#duckdb`: the DuckDB `blaze debug sql`, `blaze explain` and the explorer run,
# nixpkgs' at the locked rev, parquet built in, so nothing downloads. Pinned: a nixpkgs
# bump that changes it stops here, a visible change.
duckdb =
assert pkgs.duckdb.version == "1.5.5";
pkgs.duckdb;
# What `nix profile install` puts on PATH: the same binaries, with the toolchain, and
# BLAZE_DUCKDB naming that DuckDB (blaze never looks for one on PATH).
daemon = pkgs.runCommand "blaze-${version}" {
nativeBuildInputs = [ pkgs.makeWrapper ];
meta.mainProgram = "blaze";
} ''
mkdir -p $out/bin
for bin in ${dist}/bin/*; do
makeWrapper $bin $out/bin/$(basename $bin) --prefix PATH : ${rust}/bin \
--set BLAZE_DUCKDB ${duckdb}/bin/duckdb
done
'';
# `blaze` with the toolchain and the other programs first on PATH.
run = pkgs.writeShellScriptBin "blaze" ''
export PATH=${daemon}/bin:${rust}/bin:$PATH
exec ${daemon}/bin/blaze "$@"
'';
# eframe/winit dlopen these at run time.
guiLibs = pkgs.lib.optionals pkgs.stdenv.hostPlatform.isLinux (with pkgs; [
libGL
libxkbcommon
wayland
libx11
libxcursor
libxi
libxrandr
libxcb
]);
libPath = pkgs.lib.makeLibraryPath guiLibs;
# One derivation per cargo unit (tools/nix/units.nix, tools/nix/units/*.json from tools/nix/unit_graph.py):
# editing blaze's own source rebuilds only blaze, never the crates it depends on. The
# demo's window is built this way.
units = json: import ./tools/nix/units.nix {
inherit pkgs rust guiLibs json;
repo = ./.;
gitSha = self.shortRev or self.dirtyShortRev or "";
};
lifeUnits = units ./tools/nix/units/life.json;
life-bin = pkgs.runCommand "blaze-life-bin" { nativeBuildInputs = [ pkgs.makeWrapper ]; } ''
mkdir -p $out/bin
makeWrapper ${builtins.head lifeUnits.roots}/bin/life $out/bin/life \
--prefix LD_LIBRARY_PATH : ${libPath}
'';
# The demo's own source and the crates it links (blaze-live, the run side of blaze-inproc
# it takes patches through, and its runtime's source in library/, and blaze-api, which its editor sends edits to the daemon
# with), with the root manifest their `workspace = true` fields come from, copied to a
# writable directory at run time; its crates.io dependencies are vendored from Cargo.lock
# so that needs no network.
lifeSource = pkgs.lib.fileset.toSource {
root = ./.;
fileset = pkgs.lib.fileset.unions [
./Cargo.toml
./demo/life
./crates/blaze-api
./crates/blaze-bytes
./crates/blaze-check
./crates/blaze-core
./crates/blaze-format
./crates/blaze-graph
./crates/blaze-ids
./crates/blaze-inproc
./crates/blaze-inproc-rt
./library/blaze_inproc_rt
./crates/blaze-live
./crates/blaze-mem
./crates/blaze-phf
./crates/blaze-protocol
./crates/blaze-query
./crates/blaze-query-macros
./crates/blaze-radix
./crates/blaze-reclaim
./crates/blaze-rng
./crates/blaze-sha256
./crates/intern
./crates/stdx
./vendor/blake3-servil
];
};
lifeVendor = pkgs.rustPlatform.importCargoLock { lockFile = ./demo/life/Cargo.lock; };
# `nix run github:indexable-inc/blaze#life`: the Game of Life window. On macOS on AArch64 the
# daemon builds the copy and edits to it show up live; elsewhere the prebuilt window runs.
life = pkgs.writeShellScriptBin "blaze-life" ''
export BLAZE_LIFE_SOURCE=${lifeSource} BLAZE_LIFE_VENDOR=${lifeVendor}
export BLAZE_LIFE_DAEMON_BINS=${dist}/bin BLAZE_LIFE_PREBUILT=${life-bin}/bin/life
export BLAZE_LIFE_PATH=${rust}/bin${pkgs.lib.optionalString pkgs.stdenv.hostPlatform.isLinux ":${pkgs.stdenv.cc}/bin"}
export BLAZE_LIFE_LIBS=${libPath}
exec ${pkgs.python3}/bin/python3 -I ${./tools/nix/life_launch.py} "$@"
'';
in {
packages = {
# `nix profile install github:indexable-inc/blaze`: blaze and the programs it runs, so
# `blaze build` works. `#blaze` names it too.
default = daemon;
blaze = daemon;
inherit dist daemon run life life-bin duckdb;
# The pinned LLVM: its tools (llvm-dwarfdump), which CI puts on PATH where the harnesses
# read debug info, and the prefix a build of the `llvm` feature links.
inherit llvm;
};
# `nix run github:indexable-inc/blaze -- build` is `blaze build`.
apps.default = {
type = "app";
program = "${run}/bin/blaze";
};
apps.life = {
type = "app";
program = "${life}/bin/blaze-life";
};
apps.blaze = {
type = "app";
program = "${run}/bin/blaze";
};
apps.duckdb = {
type = "app";
program = "${duckdb}/bin/duckdb";
};
# `nix develop`: the pinned LLVM (LLVM_SYS_231_PREFIX, its tools on PATH) and a C
# toolchain with libstdc++.a, so `cargo build -p blaze-engine` links it in. Rust is
# rustup at the pinned nightly: plain `cargo` is pins/toolchain's and `cargo +<toolchain>` works.
devShells.default = pkgs.mkShell (llvmEnv // {
packages = scripts.packages ++ [ pkgs.rustup llvm ];
inherit (scripts) UV_PYTHON UV_PYTHON_DOWNLOADS;
RUSTUP_TOOLCHAIN = pkgs.lib.fileContents ./pins/toolchain;
# The pinned DuckDB that `blaze slow`, `blaze debug sql` and `blaze explain` run (blaze
# never looks for one on PATH), as the installed `blaze` wrapper sets it.
BLAZE_DUCKDB = "${duckdb}/bin/duckdb";
# As in the bench shell: the wrapper's _FORTIFY_SOURCE fails configure probes at -O0 in
# the C code of the projects the harnesses build.
hardeningDisable = [ "all" ];
} // pkgs.lib.optionalAttrs (system == "x86_64-linux") {
# rustup's rustc links x86-64 Linux with its own rust-lld, past the wrapper of the
# shell's linker that writes the store's libraries into the binary's RUNPATH: without
# this the binary finds neither the store's glibc nor its libgcc_s when it runs.
CARGO_TARGET_X86_64_UNKNOWN_LINUX_GNU_RUSTFLAGS = "-Clinker-features=-lld";
});
# What builds, runs and inspects wasm32 programs: wasmtime for wasm32-wasip1 and
# `--invoke` of a library's exports,
# wasm-tools where the harnesses read objects, wasm-bindgen and node for web guests. The
# targets are rustup's: `rustup target add wasm32-wasip1 wasm32-unknown-unknown`.
devShells.wasm = pkgs.mkShell (llvmEnv // {
packages = scripts.packages ++ [ pkgs.rustup llvm pkgs.wasmtime pkgs.wasm-tools pkgs.wasm-bindgen-cli ];
inherit (scripts) UV_PYTHON UV_PYTHON_DOWNLOADS;
RUSTUP_TOOLCHAIN = pkgs.lib.fileContents ./pins/toolchain;
hardeningDisable = [ "all" ];
});
devShells.bench = pkgs.mkShell bench;
# Python-only work needs neither LLVM nor a Rust toolchain.
devShells.scripts = pkgs.mkShell scripts;
# What harness/bench.py measures bun in (bunprep.py, fed1eb428d:docs/research/bun.md):
# bun's own pinned nightly in place of blaze's, for the cargo side (blaze ignores the pin
# and compiles its own library/), with rust-src, which bun's own build of its Rust
# (`-Zbuild-std`, run by bunprep.py's plan step) reads std from, and
# what bun's own build of its C and C++ needs, as its flake.nix has it: LLVM 23 as the
# C and C++ compiler, cmake, ninja, nasm (x86), go, a bun to run its build scripts, and on Linux lld.
# blaze itself is built in the bench shell.
devShells.bench-bun = (pkgs.mkShell.override { stdenv = pkgs.llvmPackages_23.stdenv; }) {
packages = [
(pkgs.rust-bin.nightly."2026-09-15".default.override { extensions = [ "rust-src" ]; })
pkgs.python313
pkgs.uv
pkgs.nodejs
pkgs.git
pkgs.curl
pkgs.unzip
pkgs.xz
pkgs.pkg-config
pkgs.cmake
pkgs.ninja
pkgs.nasm
pkgs.go
pkgs.bun
pkgs.perl
pkgs.ruby
pkgs.libtool
pkgs.llvmPackages_23.clang
pkgs.llvmPackages_23.llvm
pkgs.llvmPackages_23.lld
];
hardeningDisable = [ "all" ];
inherit (scripts) UV_PYTHON UV_PYTHON_DOWNLOADS;
shellHook = ''
export CC=${pkgs.llvmPackages_23.clang}/bin/clang CXX=${pkgs.llvmPackages_23.clang}/bin/clang++
export AR=${pkgs.llvmPackages_23.llvm}/bin/llvm-ar RANLIB=${pkgs.llvmPackages_23.llvm}/bin/llvm-ranlib
'' + pkgs.lib.optionalString pkgs.stdenv.hostPlatform.isLinux ''
export NIX_CFLAGS_LINK="''${NIX_CFLAGS_LINK:+$NIX_CFLAGS_LINK }-fuse-ld=lld"
'';
};
# The bench shell and what zed's build needs besides (its own nix/build.nix lists the
# same): `nix develop .#bench-zed`.
devShells.bench-zed = pkgs.mkShell (bench // {
packages = bench.packages ++ [ pkgs.cmake pkgs.protobuf ] ++ pkgs.lib.optionals pkgs.stdenv.hostPlatform.isLinux [
pkgs.curl
pkgs.fontconfig
pkgs.freetype
pkgs.libgit2
pkgs.sqlite
pkgs.zstd
pkgs.glib
pkgs.libva
pkgs.vulkan-loader
pkgs.libglvnd
pkgs.libx11
pkgs.libxcb
pkgs.libdrm
pkgs.libgbm
pkgs.libxcomposite
pkgs.libxdamage
pkgs.libxext
pkgs.libxfixes
pkgs.libxrandr
];
PROTOC = "${pkgs.protobuf}/bin/protoc";
} // pkgs.lib.optionalAttrs pkgs.stdenv.hostPlatform.isLinux {
# zed's build script looks for glib's headers under /usr/include.
CXXFLAGS = "-I${pkgs.glib.dev}/include/glib-2.0 -I${pkgs.glib.out}/lib/glib-2.0/include";
ZSTD_SYS_USE_PKG_CONFIG = "1";
} // pkgs.lib.optionalAttrs (system == "x86_64-linux") {
# The WebRTC zed links statically, the build its build script would download into
# every cold target directory.
LK_CUSTOM_WEBRTC = pkgs.fetchzip {
url = "https://github.com/zed-industries/livekit-rust-sdks/releases/download/webrtc-0001d84-4/webrtc-linux-x64-release.zip";
sha256 = "0hlv1p6fi1lgfdyq8q49gghbqbqnq50icrw7i25g2qwqmpf2jyi7";
};
});
});
}