1{ buildPackages
2, callPackage
3, perl
4, bison ? null
5, flex ? null
6, gmp ? null
7, libmpc ? null
8, mpfr ? null
9, pahole
10, lib
11, stdenv
12
13, # The kernel source tarball.
14 src
15
16, # The kernel version.
17 version
18
19, # Allows overriding the default defconfig
20 defconfig ? null
21
22, # Legacy overrides to the intermediate kernel config, as string
23 extraConfig ? ""
24
25 # Additional make flags passed to kbuild
26, extraMakeFlags ? []
27
28, # kernel intermediate config overrides, as a set
29 structuredExtraConfig ? {}
30
31, # The version number used for the module directory
32 # If unspecified, this is determined automatically from the version.
33 modDirVersion ? null
34
35, # An attribute set whose attributes express the availability of
36 # certain features in this kernel. E.g. `{iwlwifi = true;}'
37 # indicates a kernel that provides Intel wireless support. Used in
38 # NixOS to implement kernel-specific behaviour.
39 features ? {}
40
41, # Custom seed used for CONFIG_GCC_PLUGIN_RANDSTRUCT if enabled. This is
42 # automatically extended with extra per-version and per-config values.
43 randstructSeed ? ""
44
45, # A list of patches to apply to the kernel. Each element of this list
46 # should be an attribute set {name, patch} where `name' is a
47 # symbolic name and `patch' is the actual patch. The patch may
48 # optionally be compressed with gzip or bzip2.
49 kernelPatches ? []
50, ignoreConfigErrors ? stdenv.hostPlatform.linux-kernel.name or "" != "pc"
51, extraMeta ? {}
52
53, isZen ? false
54, isLibre ? false
55, isHardened ? false
56
57# easy overrides to stdenv.hostPlatform.linux-kernel members
58, autoModules ? stdenv.hostPlatform.linux-kernel.autoModules or true
59, preferBuiltin ? stdenv.hostPlatform.linux-kernel.preferBuiltin or false
60, kernelArch ? stdenv.hostPlatform.linuxArch
61, kernelTests ? []
62, nixosTests
63, ...
64}@args:
65
66# Note: this package is used for bootstrapping fetchurl, and thus
67# cannot use fetchpatch! All mutable patches (generated by GitHub or
68# cgit) that are needed here should be included directly in Nixpkgs as
69# files.
70
71assert stdenv.isLinux;
72
73let
74 # Dirty hack to make sure that `version` & `src` have
75 # `<nixpkgs/pkgs/os-specific/linux/kernel/linux-x.y.nix>` as position
76 # when using `builtins.unsafeGetAttrPos`.
77 #
78 # This is to make sure that ofborg actually detects changes in the kernel derivation
79 # and pings all maintainers.
80 #
81 # For further context, see https://github.com/NixOS/nixpkgs/pull/143113#issuecomment-953319957
82 basicArgs = builtins.removeAttrs
83 args
84 (lib.filter (x: ! (builtins.elem x [ "version" "src" ])) (lib.attrNames args));
85
86 # Combine the `features' attribute sets of all the kernel patches.
87 kernelFeatures = lib.foldr (x: y: (x.features or {}) // y) ({
88 iwlwifi = true;
89 efiBootStub = true;
90 needsCifsUtils = true;
91 netfilterRPFilter = true;
92 ia32Emulation = true;
93 } // features) kernelPatches;
94
95 commonStructuredConfig = import ./common-config.nix {
96 inherit lib stdenv version;
97
98 features = kernelFeatures; # Ensure we know of all extra patches, etc.
99 };
100
101 intermediateNixConfig = configfile.moduleStructuredConfig.intermediateNixConfig
102 # extra config in legacy string format
103 + extraConfig
104 + stdenv.hostPlatform.linux-kernel.extraConfig or "";
105
106 structuredConfigFromPatches =
107 map ({extraStructuredConfig ? {}, ...}: {settings=extraStructuredConfig;}) kernelPatches;
108
109 # appends kernel patches extraConfig
110 kernelConfigFun = baseConfigStr:
111 let
112 configFromPatches =
113 map ({extraConfig ? "", ...}: extraConfig) kernelPatches;
114 in lib.concatStringsSep "\n" ([baseConfigStr] ++ configFromPatches);
115
116 configfile = stdenv.mkDerivation {
117 inherit ignoreConfigErrors autoModules preferBuiltin kernelArch extraMakeFlags;
118 pname = "linux-config";
119 inherit version;
120
121 generateConfig = ./generate-config.pl;
122
123 kernelConfig = kernelConfigFun intermediateNixConfig;
124 passAsFile = [ "kernelConfig" ];
125
126 depsBuildBuild = [ buildPackages.stdenv.cc ];
127 nativeBuildInputs = [ perl gmp libmpc mpfr ]
128 ++ lib.optionals (lib.versionAtLeast version "4.16") [ bison flex ]
129 ++ lib.optional (lib.versionAtLeast version "5.2") pahole;
130
131 # e.g. "defconfig"
132 kernelBaseConfig = if defconfig != null then defconfig else stdenv.hostPlatform.linux-kernel.baseConfig or "defconfig";
133 # e.g. "bzImage"
134 kernelTarget = stdenv.hostPlatform.linux-kernel.target or "vmlinux";
135
136 makeFlags = lib.optionals (stdenv.hostPlatform.linux-kernel ? makeFlags) stdenv.hostPlatform.linux-kernel.makeFlags
137 ++ extraMakeFlags;
138
139 postPatch = kernel.postPatch + ''
140 # Patch kconfig to print "###" after every question so that
141 # generate-config.pl from the generic builder can answer them.
142 sed -e '/fflush(stdout);/i\printf("###");' -i scripts/kconfig/conf.c
143 '';
144
145 preUnpack = kernel.preUnpack or "";
146
147 inherit (kernel) src patches;
148
149 buildPhase = ''
150 export buildRoot="''${buildRoot:-build}"
151 export HOSTCC=$CC_FOR_BUILD
152 export HOSTCXX=$CXX_FOR_BUILD
153 export HOSTAR=$AR_FOR_BUILD
154 export HOSTLD=$LD_FOR_BUILD
155
156 # Get a basic config file for later refinement with $generateConfig.
157 make $makeFlags \
158 -C . O="$buildRoot" $kernelBaseConfig \
159 ARCH=$kernelArch \
160 HOSTCC=$HOSTCC HOSTCXX=$HOSTCXX HOSTAR=$HOSTAR HOSTLD=$HOSTLD \
161 CC=$CC OBJCOPY=$OBJCOPY OBJDUMP=$OBJDUMP READELF=$READELF \
162 $makeFlags
163
164 # Create the config file.
165 echo "generating kernel configuration..."
166 ln -s "$kernelConfigPath" "$buildRoot/kernel-config"
167 DEBUG=1 ARCH=$kernelArch KERNEL_CONFIG="$buildRoot/kernel-config" AUTO_MODULES=$autoModules \
168 PREFER_BUILTIN=$preferBuiltin BUILD_ROOT="$buildRoot" SRC=. MAKE_FLAGS="$makeFlags" \
169 perl -w $generateConfig
170 '';
171
172 installPhase = "mv $buildRoot/.config $out";
173
174 enableParallelBuilding = true;
175
176 passthru = rec {
177 module = import ../../../../nixos/modules/system/boot/kernel_config.nix;
178 # used also in apache
179 # { modules = [ { options = res.options; config = svc.config or svc; } ];
180 # check = false;
181 # The result is a set of two attributes
182 moduleStructuredConfig = (lib.evalModules {
183 modules = [
184 module
185 { settings = commonStructuredConfig; _file = "pkgs/os-specific/linux/kernel/common-config.nix"; }
186 { settings = structuredExtraConfig; _file = "structuredExtraConfig"; }
187 ]
188 ++ structuredConfigFromPatches
189 ;
190 }).config;
191
192 structuredConfig = moduleStructuredConfig.settings;
193 };
194 }; # end of configfile derivation
195
196 kernel = (callPackage ./manual-config.nix { inherit lib stdenv buildPackages; }) (basicArgs // {
197 inherit kernelPatches randstructSeed extraMakeFlags extraMeta configfile;
198 pos = builtins.unsafeGetAttrPos "version" args;
199
200 config = { CONFIG_MODULES = "y"; CONFIG_FW_LOADER = "m"; };
201 } // lib.optionalAttrs (modDirVersion != null) { inherit modDirVersion; });
202
203 passthru = basicArgs // {
204 features = kernelFeatures;
205 inherit commonStructuredConfig structuredExtraConfig extraMakeFlags isZen isHardened isLibre;
206 isXen = lib.warn "The isXen attribute is deprecated. All Nixpkgs kernels that support it now have Xen enabled." true;
207
208 # Adds dependencies needed to edit the config:
209 # nix-shell '<nixpkgs>' -A linux.configEnv --command 'make nconfig'
210 configEnv = kernel.overrideAttrs (old: {
211 nativeBuildInputs = old.nativeBuildInputs or [] ++ (with buildPackages; [
212 pkg-config ncurses
213 ]);
214 });
215
216 passthru = kernel.passthru // (removeAttrs passthru [ "passthru" ]);
217 tests = let
218 overridableKernel = finalKernel // {
219 override = args:
220 lib.warn (
221 "override is stubbed for NixOS kernel tests, not applying changes these arguments: "
222 + toString (lib.attrNames (if lib.isAttrs args then args else args {}))
223 ) overridableKernel;
224 };
225 in [ (nixosTests.kernel-generic.testsForKernel overridableKernel) ] ++ kernelTests;
226 };
227
228 finalKernel = lib.extendDerivation true passthru kernel;
229in finalKernel