Repository navigation
Expand file tree
/
Copy pathflake.nix
More file actions
493 lines (474 loc) · 20.9 KB
/
Copy pathflake.nix
File metadata and controls
493 lines (474 loc) · 20.9 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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
{
# Inputs are git URLs, not `github:`, because cloud agent sessions reach GitHub through a proxy
# that serves git clones of public repositories but refuses the archive tarballs `github:`
# fetches (.agents/memory/decisions/tooling-bench-ci.md, "Flake inputs are git URLs").
inputs = {
systems.url = "git+https://github.com/nix-systems/default?shallow=1";
nixpkgs.url = "git+https://github.com/NixOS/nixpkgs?ref=nixos-unstable&shallow=1";
rust-overlay = {
url = "git+https://github.com/oxalica/rust-overlay?shallow=1";
inputs.nixpkgs.follows = "nixpkgs";
};
crane.url = "git+https://github.com/ipetkov/crane?shallow=1";
# Only for GCC 13.3.0, the compiler of the oracle's recordings (`lib.gnuR`).
nixpkgs-gcc13-3.url = "git+https://github.com/NixOS/nixpkgs?ref=nixos-24.11&shallow=1";
};
# .github/workflows/cachix.yml pushes the x86_64-linux package and dev shell here.
nixConfig = {
extra-substituters = [ "https://ry.cachix.org" ];
extra-trusted-public-keys = [ "ry.cachix.org-1:6OKbqSFUFIsagxwxTo06JWHSEWuEtQp4kgQtR/TuVyY=" ];
};
outputs =
{
self,
systems,
nixpkgs,
rust-overlay,
crane,
nixpkgs-gcc13-3,
}:
{
lib = {
eachSystem = nixpkgs.lib.genAttrs (import systems);
makePkgs =
system: nixpkgs':
import nixpkgs' {
inherit system;
config.allowUnsupportedSystem = true;
overlays = [ (import rust-overlay) ];
};
# GNU R's source release that ry's base R is built from, pinned by tools/base-r.
baseRVersion = nixpkgs.lib.trim (builtins.readFile ./tools/base-r/VERSION);
baseRSource =
pkgs:
pkgs.fetchurl {
url = "https://cran.r-project.org/src/base/R-${pkgs.lib.versions.major self.lib.baseRVersion}/R-${self.lib.baseRVersion}.tar.gz";
sha256 = builtins.head (
pkgs.lib.splitString " " (builtins.readFile ./tools/base-r/SHA256SUMS)
);
};
# GNU R as the oracle recorded it (crates/oracle/cases/gnu-r.txt): the release ry bundles,
# built as tools/reference-r.sh builds it on Ubuntu 24.04, so that every digit matches.
# nixpkgs ships another release with another BLAS, compiler and glibc, and each of those
# moves digits (.agents/memory/decisions/tooling-bench-ci.md, "CI runs in the flake's dev
# shell").
gnuR =
pkgs:
let
inherit (pkgs) lib;
# Ubuntu 24.04's GCC is 13.3, and 13.4 no longer pairs up the two halves of a complex
# sum into one vector add, which swaps the operands and so whether NA or NaN wins
# (kernels__complex_na_nan_sums). The compilers come from an older nixpkgs, wrapped to
# link this one's glibc and libraries (they keep their own glibc 2.40 headers);
# gfortran compiles R's own BLAS.
gcc13-3 = import nixpkgs-gcc13-3 { inherit (pkgs.stdenv.hostPlatform) system; };
gcc = pkgs.wrapCC gcc13-3.gcc13.cc;
gfortran = pkgs.wrapCC gcc13-3.gfortran13.cc;
stdenv = pkgs.overrideCC pkgs.stdenv gcc;
# Ubuntu 24.04's reference LAPACK, the very binary the recordings loaded: La_library()
# names liblapack.so.3.12.0. It calls BLAS through libblas.so.3, which R's own libRblas
# shadows once R has loaded it, as on Ubuntu.
lapack = pkgs.stdenv.mkDerivation {
pname = "ubuntu-lapack";
version = "3.12.0-3build1.1";
srcs = [
(pkgs.fetchurl {
url = "http://archive.ubuntu.com/ubuntu/pool/main/l/lapack/liblapack3_3.12.0-3build1.1_amd64.deb";
sha256 = "16ymmshvbx4wcyfn96c0bv4d0zn0ww8i5ivdmvh97lsgkl2dbkh5";
})
(pkgs.fetchurl {
url = "http://archive.ubuntu.com/ubuntu/pool/main/l/lapack/libblas3_3.12.0-3build1.1_amd64.deb";
sha256 = "0n57hdxppb6vazjzs7pa6xhy320pfwimgdssgnzpgzwk12nz03hv";
})
];
nativeBuildInputs = [
pkgs.dpkg
pkgs.autoPatchelfHook
];
buildInputs = [ gfortran.cc.lib ];
unpackPhase = ''
for source in $srcs; do dpkg-deb -x "$source" .; done
'';
installPhase = ''
mkdir -p "$out/lib"
cp -P usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3* usr/lib/x86_64-linux-gnu/blas/libblas.so.3* "$out/lib/"
ln -s liblapack.so.3 "$out/lib/liblapack.so"
ln -s libblas.so.3 "$out/lib/libblas.so"
'';
};
# rlang at exactly the release ry's native trace_back follows (FAST_PATHS in
# crates/interp/src/package_fast_paths.rs), so the rlang oracle cases, the
# native-package-functions scenario and the trace_back fuzz harness run instead of
# skipping. It goes into R's own library, as tools/reference-r.sh installs it. Bump it
# together with that table.
rlang = pkgs.fetchurl {
urls = [
"https://cloud.r-project.org/src/contrib/rlang_1.3.0.tar.gz"
"https://cloud.r-project.org/src/contrib/Archive/rlang/rlang_1.3.0.tar.gz"
];
sha256 = "1717ckqcn0xm31jhhwp5lp587xca28v8jpm18gs6a8p795ym7al0";
};
in
stdenv.mkDerivation {
pname = "gnu-r";
version = self.lib.baseRVersion;
src = self.lib.baseRSource pkgs;
nativeBuildInputs = [
gfortran
pkgs.pkg-config
pkgs.perl
# configure checks date-time conversions against the time zone database.
pkgs.tzdata
# The tools configure records in Renviron: PAGER for help pages, zip and unzip.
pkgs.less
pkgs.zip
pkgs.unzip
];
# tools/dev-setup.sh's libraries for R; png() draws through cairo only with pango.
buildInputs = with pkgs; [
bzip2
cairo
curl
icu74
libdeflate
libjpeg
libpng
libtiff
libx11
libxt
ncurses
pango
pcre2
readline
xz
zlib
zstd
lapack
];
# tools/reference-r.sh's configure line, which the recordings were made with.
configureFlags = [
"--enable-R-shlib"
"--enable-memory-profiling"
"--with-x"
"--without-recommended-packages"
"--disable-java"
# Absolute, so that R CMD INSTALL compiles with them whatever is on PATH.
"CC=${gcc}/bin/gcc"
"CXX=${gcc}/bin/g++"
"FC=${gfortran}/bin/gfortran"
# Sys.which() runs the `which` configure found. Debian's prints a match the way PATH
# spells it (`./tool`), where GNU which prints an absolute path.
"WHICH=${pkgs.debianutils}/bin/which.debianutils"
# glibc 2.41 added these, and R calls glibc's where it finds them; the recordings'
# glibc 2.39 had none, so R used its own, which round differently.
"ac_cv_func_atanpi=no"
"ac_cv_func_atan2pi=no"
"ac_cv_func_cospi=no"
"ac_cv_func_sinpi=no"
"ac_cv_func_tanpi=no"
"ac_cv_func_pown=no"
];
# Ubuntu's GCC hardens by default with exactly these flags; Nix's set adds others
# (strict flex arrays, zeroed call-used registers) that move stack layout.
hardeningDisable = [ "all" ];
env.NIX_CFLAGS_COMPILE = "-D_FORTIFY_SOURCE=3 -fstack-protector-strong -fstack-clash-protection -fcf-protection";
# Ubuntu's GCC links position-independent executables by default, this one does not.
# A case that reads a pointer as a vector length (base__replacement_promises_and_errors)
# sees a long vector only at a PIE's high heap addresses. R itself is libR plus
# Rmain.o, which is already compiled with -fpic; configure's own probes must not link
# with -pie.
postConfigure = ''
substituteInPlace Makeconf --replace-fail "MAIN_LDFLAGS = " "MAIN_LDFLAGS = -pie "
'';
enableParallelBuilding = true;
postInstall = ''
HOME="$TMPDIR" "$out/bin/R" CMD INSTALL ${rlang}
'';
# What nixpkgs' R ships, so that building an R package against this R finds the
# libraries of its dependencies.
setupHook = pkgs.writeText "r-setup-hook.sh" ''
addRLibPath () {
if [[ -d "$1/library" ]]; then
addToSearchPath R_LIBS_SITE "$1/library"
fi
}
addEnvHooks "$targetOffset" addRLibPath
'';
passthru = { inherit stdenv gfortran lapack; };
meta = {
mainProgram = "R";
platforms = lib.platforms.linux;
};
};
# The compiler rustup builds with, from rust-toolchain.toml, plus what nix alone needs: the
# release cross targets and rust-analyzer for the dev shell.
rustToolchain =
pkgs:
let
pinned = (builtins.fromTOML (builtins.readFile ./rust-toolchain.toml)).toolchain;
in
pkgs.rust-bin.fromRustupToolchain (
pinned
// {
targets = (pinned.targets or [ ]) ++ [
"x86_64-unknown-linux-gnu"
"x86_64-pc-windows-gnu"
"aarch64-apple-darwin"
];
components = (pinned.components or [ ]) ++ [ "rust-analyzer" ];
}
);
};
packages = self.lib.eachSystem (
system:
let
pkgs = self.lib.makePkgs system nixpkgs;
rustToolchain = self.lib.rustToolchain pkgs;
craneLib = (crane.mkLib pkgs).overrideToolchain rustToolchain;
unfilteredRoot = ./.;
src = pkgs.lib.fileset.toSource {
root = unfilteredRoot;
fileset = pkgs.lib.fileset.unions [
(craneLib.fileset.commonCargoSources unfilteredRoot)
# Non-Rust files that `commonCargoSources` drops: libR's C sources and headers, the
# stubs and fixtures read by `include_str!` and tests, and the release metadata.
./crates
./types
./editors/code/package.json
./tools/base-r
];
};
baseRSource = self.lib.baseRSource pkgs;
commonArgs = {
inherit src;
pname = "ry";
strictDeps = true;
};
# libR links zlib, bzip2 and xz (crates/libr/build.rs).
commonArgsNative = commonArgs // {
cargoExtraArgs = "-p ry-lang -p libr -p rcmd";
buildInputs = [
pkgs.zlib
pkgs.bzip2
pkgs.xz
];
};
# The dep-only build stubs out every local .rs file, but chrono compiles against the
# patched crate's real sources; interpolating only ./patches keeps the cache stable.
keepPatchesInDummySrc = ''
rm -rf "$out"/patches
cp -r --no-preserve=mode ${./patches} "$out"/patches
'';
# For NixOS and flake users; the only build the sandbox can run, so it carries the tests.
cargoArtifactsNative = craneLib.buildDepsOnly (
commonArgsNative
// {
extraDummyScript = keepPatchesInDummySrc;
}
);
packageUnwrapped = craneLib.buildPackage (
commonArgsNative
// {
cargoArtifacts = cargoArtifactsNative;
# libr's own suites drive GNU R binaries and the corpus, which the sandbox lacks. The
# docs examples test reads docs/, which the source set leaves out so that a docs edit
# does not rebuild the package; `cargo test` runs it everywhere else.
checkPhaseCargoCommand = "cargoWithProfile test -p ry-lang -- --skip docs_examples_hold";
# GNU R lets the CLI's runtime and `ry R` tests run instead of skipping.
nativeCheckInputs = [ pkgs.R ];
# Stub materialization writes to the user cache; the sandbox's HOME is read-only.
preCheck = ''
export XDG_CACHE_HOME="$TMPDIR/.cache"
'';
# `ry run` finds ry's runtime at ../lib/ry/libR.so relative to the executable, and the
# name must stay libR.so, the soname compiled packages record. `ry load` looks for
# ry-rcmd beside the runtime to compile a package's src/. Base R is built with them
# into ../lib/ry/base-r, where `ry` looks for it.
postInstall = ''
mkdir -p "$out/lib/ry"
mv "$out"/lib/libR${pkgs.stdenv.hostPlatform.extensions.sharedLibrary} "$out/lib/ry/"
mv "$out"/bin/ry-rcmd "$out/lib/ry/"
rm -f "$out"/lib/libR.a "$out"/lib/*.rlib
R_SOURCE_TARBALL=${baseRSource} R_SOURCE_CACHE="$TMPDIR/base-r-source" \
bash tools/base-r/build.sh "$out/lib/ry/base-r" "$out/lib/ry/ry-rcmd" \
"$out/lib/ry/libR${pkgs.stdenv.hostPlatform.extensions.sharedLibrary}"
'';
meta.mainProgram = "ry";
}
);
# `ry R` runs GNU R, and a Nix-built binary cannot load a libR built against another
# distribution's libraries, so nixpkgs' R comes first on PATH. ry's runtime uses its own
# base R unless the user sets R_HOME.
packageNative =
pkgs.runCommand "ry-${packageUnwrapped.version}"
{
nativeBuildInputs = [ pkgs.makeWrapper ];
meta.mainProgram = "ry";
}
''
mkdir -p "$out/bin" "$out/lib"
ln -s ${packageUnwrapped}/lib/ry "$out/lib/ry"
makeWrapper ${packageUnwrapped}/bin/ry "$out/bin/ry" \
--prefix PATH : ${pkgs.R}/bin
'';
# Release binaries are cross-linked by zig and cannot run in the sandbox, so they skip
# tests. Linux targets glibc 2.17 with the standard /lib64 loader, so it can load R. zig
# has only libSystem stubs for macOS, so the graph stays framework-free (patches/iana-time-zone).
makeCrossArgs =
target:
commonArgs
// {
CARGO_BUILD_TARGET = builtins.head (pkgs.lib.splitString "." target);
nativeBuildInputs = [
pkgs.cargo-zigbuild
pkgs.zig
];
preBuild =
''
export XDG_CACHE_HOME="$TMPDIR/.cache"
''
# winapi (via reedline and crossterm) links the "synchronization" import library;
# cargo-zigbuild disables winapi's bundled ones and zig's mingw-w64 lacks it,
# so synthesize one.
+ pkgs.lib.optionalString (target == "x86_64-pc-windows-gnu") ''
synchronization_lib_dir="$TMPDIR/synchronization-lib"
mkdir -p "$synchronization_lib_dir"
cat > "$synchronization_lib_dir/synchronization.def" <<'EOF'
LIBRARY "api-ms-win-core-synch-l1-2-0.dll"
EXPORTS
WaitOnAddress
WakeByAddressAll
WakeByAddressSingle
EOF
zig dlltool -m i386:x86-64 \
-d "$synchronization_lib_dir/synchronization.def" \
-l "$synchronization_lib_dir/libsynchronization.a"
export RUSTFLAGS="-L native=$synchronization_lib_dir''${RUSTFLAGS:+ $RUSTFLAGS}"
'';
doCheck = false;
};
makeCrossArtifacts =
target:
craneLib.buildDepsOnly (
(makeCrossArgs target)
// {
extraDummyScript = keepPatchesInDummySrc;
buildPhaseCargoCommand = "cargo zigbuild --release --target ${target} -p ry-lang";
checkPhaseCargoCommand = "true";
}
);
buildCrossPackage =
target:
craneLib.buildPackage (
(makeCrossArgs target)
// {
cargoArtifacts = makeCrossArtifacts target;
buildPhaseCargoCommand = ''
cargoBuildLog=$(mktemp cargoBuildLogXXXX.json)
cargo zigbuild --release --target ${target} --message-format json-render-diagnostics -p ry-lang >"$cargoBuildLog"
'';
# A store path in a release binary (ELF interpreter, RUNPATH) fails the build.
allowedReferences = [ ];
}
);
in
{
default = packageNative;
ry = packageNative;
ry-unwrapped = packageUnwrapped;
ry-linux-x86_64 = buildCrossPackage "x86_64-unknown-linux-gnu.2.17";
ry-macos-aarch64 = buildCrossPackage "aarch64-apple-darwin";
ry-windows-x86_64 = buildCrossPackage "x86_64-pc-windows-gnu";
}
);
# `ci` holds everything CI's test job runs on (`nix develop .#ci -c just ci`), and `default`
# adds the tools people and agents develop with, so both test against the same GNU R,
# compiler, glibc and locales.
devShells = self.lib.eachSystem (
system:
let
pkgs = self.lib.makePkgs system nixpkgs;
gnuR = self.lib.gnuR pkgs;
# GNU R's own compiler, so that C a test compiles orders NA and NaN as GNU R does.
makeShell =
rustToolchain: packages:
(pkgs.mkShell.override { inherit (gnuR) stdenv; }) {
# libR links zlib, bzip2 and xz (crates/libr/build.rs), and `just base-r` links the
# recommended packages against LAPACK, BLAS and libgfortran.
buildInputs = [
pkgs.zlib
pkgs.bzip2
pkgs.xz
gnuR.lapack
gnuR.gfortran.cc.lib
];
packages = [
rustToolchain
gnuR
# tools/base-r/lapack.sh compiles LAPACK's Fortran.
gnuR.gfortran
pkgs.just
pkgs.python3
pkgs.glibc.bin
]
++ packages;
# The Rust toolchain propagates nixpkgs' default GCC, which would come first on PATH.
shellHook = ''
export PATH="${gnuR.stdenv.cc}/bin:$PATH"
'';
# ry loads LAPACK and BLAS by soname (crates/interp/src/linalg.rs), which on Ubuntu
# finds these very libraries in /usr/lib; Nix's loader never looks there.
LD_LIBRARY_PATH = "${gnuR.lapack}/lib";
# Nix's glibc looks for time zones in its own store path, which has none.
TZDIR = "${pkgs.tzdata}/share/zoneinfo";
# The locales oracle cases switch to (`# oracle: requires locale`), which
# tools/oracle-locales.sh checks for.
LOCALE_ARCHIVE = "${
pkgs.glibcLocales.override {
allLocales = false;
locales = [
"C.UTF-8/UTF-8"
"en_US.UTF-8/UTF-8"
"en_US/ISO-8859-1"
];
}
}/lib/locale/locale-archive";
};
in
{
ci = makeShell (pkgs.rust-bin.fromRustupToolchainFile ./rust-toolchain.toml) [ ];
default = makeShell (self.lib.rustToolchain pkgs) (
with pkgs;
[
# tools/env.sh links debug builds with it.
mold
evcxr
# radian runs the shell's GNU R, with renv and devtools built for it on its own
# library path, so `R` and `Rscript` stay the ones CI runs.
(radianWrapper.override {
R = gnuR;
recommendedPackages = [ ];
packages = with (rPackages.override { R = gnuR; }); [
renv
devtools
];
})
cargo-edit
cargo-insta
cargo-nextest
cargo-zigbuild
zig
tree-sitter
bun
zip
]
);
}
);
# `nix flake check` builds the package, which runs ry-lang's tests, and the dev shells.
checks = self.lib.eachSystem (system: {
ry = self.packages.${system}.ry;
dev-shell = self.devShells.${system}.default;
});
};
}