-
Notifications
You must be signed in to change notification settings - Fork 0
/
default.nix
617 lines (597 loc) · 18.1 KB
/
default.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
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
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
{
rust-channel ? "stable",
build-flags ? import ./nix/flags.nix,
versions ? import ./nix/versions.nix,
image-tag ? "latest",
contianer-repo ? "ghcr.io/githedgehog/dpdk-sys",
}:
rec {
rust-version = versions.rust.${rust-channel};
llvm-version = rust-version.llvm;
llvm-overlay = self: super: rec {
llvmPackages = super.${llvmPackagesVersion};
llvmPackagesVersion = "llvmPackages_${llvm-version}";
};
rust-overlay = (
import (builtins.fetchTarball "https://github.com/oxalica/rust-overlay/archive/master.tar.gz")
);
toolchainPkgs =
import
(builtins.fetchTarball {
url = "https://github.com/NixOS/nixpkgs/archive/${versions.nixpkgs.commit}.tar.gz";
sha256 = versions.nixpkgs.hash.nix32.unpacked.sha256;
})
{
overlays = [
llvm-overlay
rust-overlay
helpersOverlay
];
};
helpersOverlay = self: super: rec {
tmpdir = self.stdenv.mkDerivation {
name = "${project-name}-tmpdir";
src = null;
dontUnpack = true;
installPhase = ''
mkdir --parent "$out/tmp"
'';
};
# Temporary hack to get around upstream test failures
redis = super.redis.overrideAttrs {
doCheck = false;
};
graphviz-links = self.stdenv.mkDerivation {
name = "${project-name}-tmpdir";
src = null;
dontUnpack = true;
installPhase = ''
mkdir -p $out/opt/local/bin
ln -s "${self.graphviz}/bin/dot" "$out/opt/local/bin/dot"
'';
};
};
project-name = "dpdk-sys";
crossOverlay =
{ build-flags, crossEnv }:
self: super: rec {
inherit build-flags;
buildWithFlags =
flags: pkg:
(pkg.overrideAttrs (orig: {
CFLAGS = "${orig.CFLAGS or ""} ${flags.CFLAGS}";
CXXFLAGS = "${orig.CXXFLAGS or ""} ${flags.CXXFLAGS}";
LDFLAGS = "${orig.LDFLAGS or ""} ${flags.LDFLAGS}";
}));
fancy.stdenvDynamic = (
self.stdenvAdapters.useMoldLinker (
if crossEnv == "musl64" then
# TODO: It doesn't really make any sense to me that I need
# to use pkgsMusl here.
# In my mind that is implied by the fact that self is a
# crossEnv.
self.pkgsMusl.${self.llvmPackagesVersion}.libcxxStdenv
else
self.llvmPackages.libcxxStdenv
)
);
fancy.stdenv = self.stdenvAdapters.makeStaticLibraries fancy.stdenvDynamic;
# TODO: consider ways to LTO optimize musl (this one might be a bit tricky)
buildWithMyFlags = pkg: (buildWithFlags build-flags pkg);
optimizedBuild =
pkg:
(buildWithMyFlags (pkg.override { stdenv = fancy.stdenv; })).overrideAttrs {
withDoc = false;
doCheck = false;
};
fancy.libmd = (optimizedBuild super.libmd).overrideAttrs (orig: {
configureFlags = orig.configureFlags ++ [
"--enable-static"
"--disable-shared"
];
postFixup =
(orig.postFixup or "")
+ ''
rm $out/lib/*.la
'';
});
fancy.libbsd =
((optimizedBuild super.libbsd).override {
libmd = fancy.libmd;
}).overrideAttrs
(orig: {
doCheck = false;
configureFlags = orig.configureFlags ++ [
"--enable-static"
"--enable-shared"
];
postFixup =
(orig.postFixup or "")
+ ''
rm $out/lib/*.la
'';
});
fatLto =
pkg:
pkg.overrideAttrs (orig: {
CFLAGS = "${orig.CFLAGS or ""} -ffat-lto-objects";
});
at-spi2-atk = null; # no users in container
at-spi2-core = null; # no users in container
bluez = null;
dbus = super.dbus.override { enableSystemd = false; };
gtk3 = null;
libusb = null;
libusb1 = null;
systemd = null;
systemdLibs = null;
systemdMinimal = null;
tinysparql = null;
util-linux = super.util-linux.override { systemdSupport = false; };
rdma-core = (fatLto (optimizedBuild super.rdma-core)).overrideAttrs (orig: {
outputs = [
"out"
"dev"
];
perl = null;
cmakeFlags = orig.cmakeFlags ++ [
"-DENABLE_STATIC=1"
"-DNO_PYVERBS=1"
"-DNO_MAN_PAGES=1"
"-DIOCTL_MODE=write"
"-DNO_COMPAT_SYMS=1"
];
patches =
(orig.patches or [ ])
++ (
if crossEnv == "musl64" then
[ ]
else
[
(super.fetchpatch {
# you need to patch rdma-core to build with clang + glibc 2.40.x since glibc 2.40 has improved fortifying
# this function with clang.
name = "fix-for-glibc-2.40.x";
url = "https://git.openembedded.org/meta-openembedded/plain/meta-networking/recipes-support/rdma-core/rdma-core/0001-librdmacm-Use-overloadable-function-attribute-with-c.patch?id=69769ff44ed0572a7b3c769ce3c36f28fff359d1";
sha256 = "sha256-k+T8vSkvljksJabSJ/WRCXTYfbINcW1n0oDQrvFXXGM=";
})
]
);
});
iptables = null;
ethtool = null;
iproute2 = null;
libnl = (optimizedBuild super.libnl).overrideAttrs (orig: {
configureFlags = orig.configureFlags ++ [
"--enable-static"
"--disable-shared"
];
postFixup =
(orig.postFixup or "")
+ ''
rm $out/lib/*.la
'';
});
jansson = (optimizedBuild super.jansson).overrideAttrs (orig: {
cmakeFlags = [ "-DJANSSON_BUILD_SHARED_LIBS=OFF" ];
});
libmnl = optimizedBuild super.libmnl;
libnetfilter_conntrack = optimizedBuild super.libnetfilter_conntrack;
libnftnl = optimizedBuild super.libnftnl;
libpcap = optimizedBuild super.libpcap;
numactl = (optimizedBuild super.numactl).overrideAttrs (orig: {
outputs = super.lib.lists.remove "man" orig.outputs;
configurePhase = ''
set -euxo pipefail;
./configure \
--prefix=$out \
--libdir=$out/lib \
--includedir=$out/include \
--enable-static \
--enable-shared;
'';
buildPhase = ''
set -euxo pipefail;
make;
rm ./.libs/*.la;
'';
});
dpdk = (
optimizedBuild (
self.callPackage ./nix/dpdk {
libbsd = fancy.libbsd;
libmd = fancy.libmd;
}
)
);
dpdk-wrapper = (
optimizedBuild (
self.callPackage ./nix/dpdk-wrapper {
inherit dpdk;
libbsd = fancy.libbsd;
bintools = self.llvmPackages.bintools;
}
)
);
libyang =
((optimizedBuild super.libyang).override { pcre2 = self.fancy.pcre2; }).overrideAttrs
(orig: {
cmakeFlags = (orig.cmakeFlags or [ ]) ++ [
"-DENABLE_STATIC=1"
"-DBUILD_SHARED_LIBS=ON"
];
});
libcap =
((optimizedBuild super.libcap).override {
usePam = false;
}).overrideAttrs
(orig: {
nativeBuildInputs = (orig.nativeBuildInputs or [ ]) ++ [ self.llvmPackages.bintools ];
LD = "lld";
configureFlags = (orig.configureFlags or [ ]) ++ [ "--enable-static" ];
makeFlags = orig.makeFlags ++ [ "GOLANG=no" ];
postInstall =
orig.postInstall
+ ''
# extant postInstall removes .a files for no reason
rm $lib/lib/*.so*;
cp ./libcap/*.a $lib/lib;
'';
});
json_c = (optimizedBuild super.json_c).overrideAttrs (orig: {
cmakeFlags = (orig.cmakeFlags or [ ]) ++ [ "-DENABLE_STATIC=1" ];
postInstall =
(orig.postInstall or "")
+ ''
mkdir -p $dev/lib
cp libjson-c.a $dev/lib;
'';
});
rtrlib = (optimizedBuild super.rtrlib).overrideAttrs (orig: {
cmakeFlags = (orig.cmakeFlags or [ ]) ++ [ "-DENABLE_STATIC=1" ];
});
abseil-cpp = (optimizedBuild super.abseil-cpp);
protobuf_25 = (optimizedBuild super.protobuf_25).overrideAttrs (orig: {
cmakeFlags = (orig.cmakeFlags or [ ]) ++ [ "-Dprotobuf_BUILD_SHARED_LIBS=OFF" ];
});
protobufc = (optimizedBuild super.protobufc).overrideAttrs (orig: {
configureFlags = (orig.configureFlags or [ ]) ++ [
"--enable-static"
"--disable-shared"
];
});
fancy.pcre2 = (optimizedBuild super.pcre2).overrideAttrs (orig: {
configureFlags = (orig.configureFlags or [ ]) ++ [
"--enable-static"
"--disable-shared"
];
});
frr =
(buildWithMyFlags (
self.callPackage ./nix/frr {
stdenv = fancy.stdenvDynamic;
json_c = json_c.dev;
}
)).overrideAttrs
(orig: {
nativeBuildInputs = (orig.nativeBuildInputs or [ ]) ++ [
self.fancy.pcre2
self.protobufc
];
LDFLAGS =
(orig.LDFLAGS or "")
+ " -L${self.protobufc}/lib -Wl,-lprotobuf-c -L${self.fancy.pcre2}/lib -Wl,-lpcre2-8";
});
};
pkgs.dev =
(import toolchainPkgs.path {
overlays = [
(self: prev: {
pkgsCross.gnu64 = import prev.path {
overlays = [
llvm-overlay
helpersOverlay
(crossOverlay {
build-flags = build-flags.dev;
crossEnv = "gnu64";
})
];
};
pkgsCross.musl64 = import prev.path {
overlays = [
llvm-overlay
helpersOverlay
(crossOverlay {
build-flags = build-flags.dev;
crossEnv = "musl64";
})
];
};
})
];
}).pkgsCross;
pkgs.release =
(import toolchainPkgs.path {
overlays = [
(self: prev: {
pkgsCross.gnu64 = import prev.path {
overlays = [
llvm-overlay
helpersOverlay
(crossOverlay {
build-flags = build-flags.release;
crossEnv = "gnu64";
})
];
};
pkgsCross.musl64 = import prev.path {
overlays = [
llvm-overlay
helpersOverlay
(crossOverlay {
build-flags = build-flags.release;
crossEnv = "musl64";
})
];
};
})
];
}).pkgsCross;
sysrootPackageListFn =
libc: pkgs:
with pkgs;
(
[
fancy.libbsd
fancy.libbsd.dev
dpdk
dpdk-wrapper
fancy.libmd
libmnl
libnftnl
libnl.out
libpcap
numactl
rdma-core
]
++ (
if libc == "gnu64" then
[
glibc
glibc.out
libgcc.libgcc
glibc.dev
glibc.static
]
else
[ ]
)
++ (
if libc == "musl64" then
[
musl.out
musl.dev
]
else
[ ]
)
);
sysrootPackageList = {
gnu64 = {
dev = sysrootPackageListFn "gnu64" pkgs.dev.gnu64;
release = sysrootPackageListFn "gnu64" pkgs.release.gnu64;
};
musl64 = {
dev = sysrootPackageListFn "musl64" pkgs.dev.musl64;
release = sysrootPackageListFn "musl64" pkgs.release.musl64;
};
};
rust-toolchain =
with rust-version;
(toolchainPkgs.rust-bin.${channel}.${version}.${profile}.override {
inherit targets extensions;
});
compileEnvPackageList = with toolchainPkgs; [
(callPackage ./nix/shell-fixup { })
bash-completion
bashInteractive
cacert
cargo-nextest
coreutils
just
libcap # for test runner
llvmPackages.clang
llvmPackages.libclang.lib
llvmPackages.lld
rust-toolchain
sudo # for test runner
tmpdir
];
docEnvPackageList = (
with toolchainPkgs;
[
(callPackage ./nix/mdbook-alerts { })
(callPackage ./nix/plantuml-wrapper { })
bash
coreutils
file # needed for mdbook-plantuml to work (runtime exe dep)
fontconfig.lib # needed for mdbook-plantuml to work (runtime exe dep)
fontconfig.out # needed for mdbook-plantuml to work (runtime exe dep)
glibc.out
graphviz # needed for mdbook-plantuml to work (runtime exe dep)
graphviz-links # needed for mdbook-plantuml to work (runtime exe dep)
jre # needed for mdbook-plantuml to work (runtime exe dep)
mdbook
mdbook-katex
mdbook-mermaid
mdbook-plantuml
openssl.out # needed for mdbook-plantuml to work (runtime exe dep)
plantuml # needed for mdbook-plantuml to work (runtime exe dep)
tmpdir
]
);
env = {
sysroot.gnu64.dev = toolchainPkgs.symlinkJoin {
name = "${project-name}-env-dev-sysroot-gnu64";
paths = sysrootPackageListFn "gnu64" pkgs.dev.gnu64;
};
sysroot.gnu64.release = toolchainPkgs.symlinkJoin {
name = "${project-name}-env-release-sysroot-gnu64";
paths = sysrootPackageListFn "gnu64" pkgs.release.gnu64;
};
sysroot.musl64.dev = toolchainPkgs.symlinkJoin {
name = "${project-name}-env-dev-sysroot-musl64";
paths = sysrootPackageListFn "musl64" pkgs.dev.musl64;
};
sysroot.musl64.release = toolchainPkgs.symlinkJoin {
name = "${project-name}-env-release-sysroot-musl64";
paths = sysrootPackageListFn "musl64" pkgs.release.musl64;
};
compile = toolchainPkgs.symlinkJoin {
name = "${project-name}-env-compile";
paths = compileEnvPackageList;
};
doc = toolchainPkgs.symlinkJoin {
name = "${project-name}-doc";
paths = docEnvPackageList;
};
};
sysrootFn =
libc: profile:
let
libcShortName = (if libc == "gnu64" then "gnu" else "musl");
in
toolchainPkgs.stdenv.mkDerivation {
name = "${project-name}-sysroot.${libc}.${profile}";
nativeBuildInputs = [ toolchainPkgs.rsync ];
src = null;
dontUnpack = true;
installPhase = ''
mkdir --parent "$out/sysroot/x86_64-unknown-linux-${libcShortName}/${profile}/"{lib,include}
rsync -rLhP \
"${env.sysroot.${libc}.${profile}}/lib/" \
"$out/sysroot/x86_64-unknown-linux-${libcShortName}/${profile}/lib/"
rsync -rLhP \
"${env.sysroot.${libc}.${profile}}/include/" \
"$out/sysroot/x86_64-unknown-linux-${libcShortName}/${profile}/include/"
'';
# Rust can't decided if the profile is called dev or debug so we need a fixup
postFixup =
(
if profile == "dev" then
''
ln -s dev $out/sysroot/x86_64-unknown-linux-${libcShortName}/debug
''
else
""
)
+ (
if libc == "gnu64" then
''
export lib="$out/sysroot/x86_64-unknown-linux-${libcShortName}/${profile}/lib"
cd $lib
cat > libm.a <<EOF
OUTPUT_FORMAT(elf64-x86-64)
GROUP ( libm-${pkgs.dev.gnu64.glibc.version}.a libmvec.a )
EOF
cat > libm.so <<EOF
OUTPUT_FORMAT(elf64-x86-64)
GROUP ( libm.so.6 AS_NEEDED ( libmvec.so.1 ) )
EOF
cat > libc.so <<EOF
OUTPUT_FORMAT(elf64-x86-64)
GROUP ( libc.so.6 libc_nonshared.a AS_NEEDED ( ld-linux-x86-64.so.2 ) )
EOF
''
else
""
);
};
sysroot.gnu64.dev = sysrootFn "gnu64" "dev";
sysroot.gnu64.release = sysrootFn "gnu64" "release";
sysroot.musl64.dev = sysrootFn "musl64" "dev";
sysroot.musl64.release = sysrootFn "musl64" "release";
sysroots = with sysroot; [
gnu64.dev
gnu64.release
musl64.dev
musl64.release
];
clearDeps =
obj: with builtins; (/. + "${unsafeDiscardStringContext (unsafeDiscardOutputDependency (obj))}");
maxLayers = 120;
initfrr = toolchainPkgs.stdenv.mkDerivation {
name = "${project-name}-initfrr";
src = ./nix/frr/bin;
dontUnpack = true;
installPhase = ''
mkdir -p $out/bin
cp $src/init.sh $out/bin/init.sh
chmod +x $out/bin/init.sh
'';
};
frrContainerContents = (
with pkgs.release.gnu64;
[
bash
coreutils
frr
glibc.bin
glibc.out
gnugrep
gnused
initfrr
libxcrypt
ncurses
readline
tmpdir
]
);
container = {
frr = toolchainPkgs.dockerTools.buildLayeredImage {
name = "${contianer-repo}/frr";
tag = "${image-tag}";
contents = map clearDeps frrContainerContents;
config = {
Env = [
"LD_LIBRARY_PATH=/lib"
"PATH=/bin:/libexec/frr"
];
Entrypoint = [ "/bin/init.sh" ];
};
inherit maxLayers;
};
compile-env = toolchainPkgs.dockerTools.buildLayeredImage {
name = "${contianer-repo}/compile-env";
tag = "${image-tag}";
# glibc is needed as an explicit dependency due to the use of linker script in the libm.a file.
# Specifically, the libm.a file contains a GROUP instruction which contains absolute paths to /nix
# and those paths are not preserved by the rsync and clearDeps commands.
contents = [
env.compile
pkgs.dev.gnu64.glibc.static
pkgs.release.gnu64.glibc.static
pkgs.dev.gnu64.glibc.dev
pkgs.release.gnu64.glibc.dev
pkgs.dev.gnu64.glibc.out
pkgs.release.gnu64.glibc.out
] ++ (map clearDeps sysroots);
inherit maxLayers;
};
doc-env = toolchainPkgs.dockerTools.buildLayeredImage {
name = "${contianer-repo}/doc-env";
tag = "${image-tag}";
contents = docEnvPackageList;
inherit maxLayers;
config = {
Entrypoint = [
"/bin/mdbook"
];
Env = [
"LD_LIBRARY_PATH=/lib"
"PATH=/bin"
];
};
};
};
}