1{ buildPackages, runCommand, nettools, bc, bison, flex, perl, gmp, libmpc, mpfr, openssl
2, ncurses
3, libelf
4, utillinux
5, writeTextFile, ubootTools
6, callPackage
7}:
8
9{ stdenv, buildPackages, perl, buildLinux
10
11, # The kernel source tarball.
12 src
13
14, # The kernel version.
15 version
16
17, # Overrides to the kernel config.
18 extraConfig ? ""
19
20, # The version number used for the module directory
21 modDirVersion ? version
22
23, # An attribute set whose attributes express the availability of
24 # certain features in this kernel. E.g. `{iwlwifi = true;}'
25 # indicates a kernel that provides Intel wireless support. Used in
26 # NixOS to implement kernel-specific behaviour.
27 features ? {}
28
29, # A list of patches to apply to the kernel. Each element of this list
30 # should be an attribute set {name, patch} where `name' is a
31 # symbolic name and `patch' is the actual patch. The patch may
32 # optionally be compressed with gzip or bzip2.
33 kernelPatches ? []
34, ignoreConfigErrors ? hostPlatform.platform.name != "pc" ||
35 hostPlatform != stdenv.buildPlatform
36, extraMeta ? {}
37, hostPlatform
38, ...
39} @ args:
40
41assert stdenv.isLinux;
42
43let
44
45 lib = stdenv.lib;
46
47 # Combine the `features' attribute sets of all the kernel patches.
48 kernelFeatures = lib.fold (x: y: (x.features or {}) // y) ({
49 iwlwifi = true;
50 efiBootStub = true;
51 needsCifsUtils = true;
52 netfilterRPFilter = true;
53 } // features) kernelPatches;
54
55 config = import ./common-config.nix {
56 inherit stdenv version ;
57 # append extraConfig for backwards compatibility but also means the user can't override the kernelExtraConfig part
58 extraConfig = extraConfig + lib.optionalString (hostPlatform.platform ? kernelExtraConfig) hostPlatform.platform.kernelExtraConfig;
59
60 features = kernelFeatures; # Ensure we know of all extra patches, etc.
61 };
62
63 kernelConfigFun = baseConfig:
64 let
65 configFromPatches =
66 map ({extraConfig ? "", ...}: extraConfig) kernelPatches;
67 in lib.concatStringsSep "\n" ([baseConfig] ++ configFromPatches);
68
69 configfile = stdenv.mkDerivation {
70 inherit ignoreConfigErrors;
71 name = "linux-config-${version}";
72
73 generateConfig = ./generate-config.pl;
74
75 kernelConfig = kernelConfigFun config;
76
77 depsBuildBuild = [ buildPackages.stdenv.cc ];
78 nativeBuildInputs = [ perl ]
79 ++ lib.optionals (stdenv.lib.versionAtLeast version "4.16") [ bison flex ];
80
81 platformName = hostPlatform.platform.name;
82 # e.g. "defconfig"
83 kernelBaseConfig = hostPlatform.platform.kernelBaseConfig;
84 # e.g. "bzImage"
85 kernelTarget = hostPlatform.platform.kernelTarget;
86 autoModules = hostPlatform.platform.kernelAutoModules;
87 preferBuiltin = hostPlatform.platform.kernelPreferBuiltin or false;
88 arch = hostPlatform.platform.kernelArch;
89
90 prePatch = kernel.prePatch + ''
91 # Patch kconfig to print "###" after every question so that
92 # generate-config.pl from the generic builder can answer them.
93 sed -e '/fflush(stdout);/i\printf("###");' -i scripts/kconfig/conf.c
94 '';
95
96 inherit (kernel) src patches preUnpack;
97
98 buildPhase = ''
99 export buildRoot="''${buildRoot:-build}"
100
101 # Get a basic config file for later refinement with $generateConfig.
102 make HOSTCC=${buildPackages.stdenv.cc.targetPrefix}gcc -C . O="$buildRoot" $kernelBaseConfig ARCH=$arch
103
104 # Create the config file.
105 echo "generating kernel configuration..."
106 echo "$kernelConfig" > "$buildRoot/kernel-config"
107 DEBUG=1 ARCH=$arch KERNEL_CONFIG="$buildRoot/kernel-config" AUTO_MODULES=$autoModules \
108 PREFER_BUILTIN=$preferBuiltin BUILD_ROOT="$buildRoot" SRC=. perl -w $generateConfig
109 '';
110
111 installPhase = "mv $buildRoot/.config $out";
112
113 enableParallelBuilding = true;
114 };
115
116 kernel = (callPackage ./manual-config.nix {}) {
117 inherit version modDirVersion src kernelPatches stdenv extraMeta configfile hostPlatform;
118
119 config = { CONFIG_MODULES = "y"; CONFIG_FW_LOADER = "m"; };
120 };
121
122 passthru = {
123 features = kernelFeatures;
124 passthru = kernel.passthru // (removeAttrs passthru [ "passthru" ]);
125 };
126
127in lib.extendDerivation true passthru kernel