1{ stdenvNoCC, lib, buildPackages
2, fetchurl, fetchpatch, perl
3, elf-header
4}:
5
6let
7 common = { version, sha256, patches ? [] }: stdenvNoCC.mkDerivation {
8 name = "linux-headers-${version}";
9
10 src = fetchurl {
11 url = "mirror://kernel/linux/kernel/v4.x/linux-${version}.tar.xz";
12 inherit sha256;
13 };
14
15 ARCH = stdenvNoCC.hostPlatform.platform.kernelArch or (throw "missing kernelArch");
16
17 # It may look odd that we use `stdenvNoCC`, and yet explicit depend on a cc.
18 # We do this so we have a build->build, not build->host, C compiler.
19 depsBuildBuild = [ buildPackages.stdenv.cc ];
20 # `elf-header` is null when libc provides `elf.h`.
21 nativeBuildInputs = [ perl elf-header ];
22
23 extraIncludeDirs = lib.optional stdenvNoCC.hostPlatform.isPowerPC ["ppc"];
24
25 inherit patches;
26
27 hardeningDisable = lib.optional stdenvNoCC.buildPlatform.isDarwin "format";
28
29 makeFlags = [
30 "SHELL=bash"
31 # Avoid use of runtime build->host compilers for checks. These
32 # checks only cared to work around bugs in very old compilers, so
33 # these changes should be safe.
34 "cc-version:=9999"
35 "cc-fullversion:=999999"
36 # `$(..)` expanded by make alone
37 "HOSTCC:=$(BUILD_CC)"
38 "HOSTCXX:=$(BUILD_CXX)"
39 ];
40
41 # Skip clean on darwin, case-sensitivity issues.
42 buildPhase = lib.optionalString (!stdenvNoCC.buildPlatform.isDarwin) ''
43 make mrproper $makeFlags
44 ''
45 # For some reason, doing `make install_headers` twice, first without
46 # INSTALL_HDR_PATH=$out then with, is neccessary to get this to work
47 # for darwin cross. @Ericson2314 has no idea why.
48 + ''
49 make headers_install $makeFlags
50 '';
51
52 checkPhase = ''
53 make headers_check $makeFlags
54 '';
55
56 installPhase = ''
57 make headers_install INSTALL_HDR_PATH=$out $makeFlags
58 ''
59 # Some builds (e.g. KVM) want a kernel.release.
60 + '' mkdir -p $out/include/config
61 echo "${version}-default" > $out/include/config/kernel.release
62 ''
63 # These oddly named file records teh `SHELL` passed, which causes bootstrap
64 # tools run-time dependency.
65 + ''
66 find "$out" -name '..install.cmd' -print0 | xargs -0 rm
67 '';
68
69 meta = with lib; {
70 description = "Header files and scripts for Linux kernel";
71 license = licenses.gpl2;
72 platforms = platforms.linux;
73 };
74 };
75in {
76
77 linuxHeaders = common {
78 version = "4.19.16";
79 sha256 = "1pqvn6dsh0xhdpawz4ag27vkw1abvb6sn3869i4fbrz33ww8i86q";
80 patches = [
81 ./no-relocs.patch # for building x86 kernel headers on non-ELF platforms
82 ./no-dynamic-cc-version-check.patch # so we can use `stdenvNoCC`, see `makeFlags` above
83 ];
84 };
85}