1{
2 lib,
3 stdenv,
4 fetchurl,
5 buildPackages,
6 linuxHeaders,
7 perl,
8 nixosTests,
9}:
10
11let
12 commonMakeFlags = [
13 "prefix=$(out)"
14 "SHLIBDIR=$(out)/lib"
15 ];
16in
17
18stdenv.mkDerivation rec {
19 pname = "klibc";
20 version = "2.0.14";
21
22 src = fetchurl {
23 url = "mirror://kernel/linux/libs/klibc/2.0/klibc-${version}.tar.xz";
24 hash = "sha256-KBv7aD4ZaBhBKvcLiWi3cmR1qA/xxL1nEZ5r9QWfkHU=";
25 };
26
27 patches = [ ./no-reinstall-kernel-headers.patch ];
28
29 depsBuildBuild = [ buildPackages.stdenv.cc ];
30 nativeBuildInputs = [ perl ];
31 strictDeps = true;
32
33 hardeningDisable = [
34 "format"
35 "stackprotector"
36 ];
37
38 makeFlags =
39 commonMakeFlags
40 ++ [
41 "KLIBCARCH=${if stdenv.hostPlatform.isRiscV64 then "riscv64" else stdenv.hostPlatform.linuxArch}"
42 "KLIBCKERNELSRC=${linuxHeaders}"
43 ]
44 # TODO(@Ericson2314): We now can get the ABI from
45 # `stdenv.hostPlatform.parsed.abi`, is this still a good idea?
46 ++ lib.optional (stdenv.hostPlatform.linuxArch == "arm") "CONFIG_AEABI=y"
47 ++ lib.optional (
48 stdenv.hostPlatform != stdenv.buildPlatform
49 ) "CROSS_COMPILE=${stdenv.cc.targetPrefix}";
50
51 # Install static binaries as well.
52 postInstall = ''
53 dir=$out/lib/klibc/bin.static
54 mkdir $dir
55 cp $(find $(find . -name static) -type f ! -name "*.g" -a ! -name ".*") $dir/
56
57 for file in ${linuxHeaders}/include/*; do
58 ln -sv $file $out/lib/klibc/include
59 done
60 '';
61
62 passthru.tests = {
63 # uses klibc's ipconfig
64 inherit (nixosTests) initrd-network-ssh;
65 };
66
67 meta = {
68 description = "Minimalistic libc subset for initramfs usage";
69 mainProgram = "klcc";
70 homepage = "https://kernel.org/pub/linux/libs/klibc/";
71 maintainers = with lib.maintainers; [ fpletz ];
72 license = lib.licenses.bsd3;
73 platforms = lib.platforms.linux;
74 };
75}