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