at 23.05-pre 221 lines 7.8 kB view raw
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 modDirVersion ? version 33 34, # An attribute set whose attributes express the availability of 35 # certain features in this kernel. E.g. `{iwlwifi = true;}' 36 # indicates a kernel that provides Intel wireless support. Used in 37 # NixOS to implement kernel-specific behaviour. 38 features ? {} 39 40, # Custom seed used for CONFIG_GCC_PLUGIN_RANDSTRUCT if enabled. This is 41 # automatically extended with extra per-version and per-config values. 42 randstructSeed ? "" 43 44, # A list of patches to apply to the kernel. Each element of this list 45 # should be an attribute set {name, patch} where `name' is a 46 # symbolic name and `patch' is the actual patch. The patch may 47 # optionally be compressed with gzip or bzip2. 48 kernelPatches ? [] 49, ignoreConfigErrors ? stdenv.hostPlatform.linux-kernel.name != "pc" || 50 stdenv.hostPlatform != stdenv.buildPlatform 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 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 platformName = stdenv.hostPlatform.linux-kernel.name; 132 # e.g. "defconfig" 133 kernelBaseConfig = if defconfig != null then defconfig else stdenv.hostPlatform.linux-kernel.baseConfig; 134 # e.g. "bzImage" 135 kernelTarget = stdenv.hostPlatform.linux-kernel.target; 136 137 makeFlags = lib.optionals (stdenv.hostPlatform.linux-kernel ? makeFlags) stdenv.hostPlatform.linux-kernel.makeFlags 138 ++ extraMakeFlags; 139 140 postPatch = kernel.postPatch + '' 141 # Patch kconfig to print "###" after every question so that 142 # generate-config.pl from the generic builder can answer them. 143 sed -e '/fflush(stdout);/i\printf("###");' -i scripts/kconfig/conf.c 144 ''; 145 146 preUnpack = kernel.preUnpack or ""; 147 148 inherit (kernel) src patches; 149 150 buildPhase = '' 151 export buildRoot="''${buildRoot:-build}" 152 export HOSTCC=$CC_FOR_BUILD 153 export HOSTCXX=$CXX_FOR_BUILD 154 export HOSTAR=$AR_FOR_BUILD 155 export HOSTLD=$LD_FOR_BUILD 156 157 # Get a basic config file for later refinement with $generateConfig. 158 make $makeFlags \ 159 -C . O="$buildRoot" $kernelBaseConfig \ 160 ARCH=$kernelArch \ 161 HOSTCC=$HOSTCC HOSTCXX=$HOSTCXX HOSTAR=$HOSTAR HOSTLD=$HOSTLD \ 162 CC=$CC OBJCOPY=$OBJCOPY OBJDUMP=$OBJDUMP READELF=$READELF \ 163 $makeFlags 164 165 # Create the config file. 166 echo "generating kernel configuration..." 167 ln -s "$kernelConfigPath" "$buildRoot/kernel-config" 168 DEBUG=1 ARCH=$kernelArch KERNEL_CONFIG="$buildRoot/kernel-config" AUTO_MODULES=$autoModules \ 169 PREFER_BUILTIN=$preferBuiltin BUILD_ROOT="$buildRoot" SRC=. MAKE_FLAGS="$makeFlags" \ 170 perl -w $generateConfig 171 ''; 172 173 installPhase = "mv $buildRoot/.config $out"; 174 175 enableParallelBuilding = true; 176 177 passthru = rec { 178 module = import ../../../../nixos/modules/system/boot/kernel_config.nix; 179 # used also in apache 180 # { modules = [ { options = res.options; config = svc.config or svc; } ]; 181 # check = false; 182 # The result is a set of two attributes 183 moduleStructuredConfig = (lib.evalModules { 184 modules = [ 185 module 186 { settings = commonStructuredConfig; _file = "pkgs/os-specific/linux/kernel/common-config.nix"; } 187 { settings = structuredExtraConfig; _file = "structuredExtraConfig"; } 188 ] 189 ++ structuredConfigFromPatches 190 ; 191 }).config; 192 193 structuredConfig = moduleStructuredConfig.settings; 194 }; 195 }; # end of configfile derivation 196 197 kernel = (callPackage ./manual-config.nix { inherit buildPackages; }) (basicArgs // { 198 inherit modDirVersion kernelPatches randstructSeed lib stdenv extraMakeFlags extraMeta configfile; 199 pos = builtins.unsafeGetAttrPos "version" args; 200 201 config = { CONFIG_MODULES = "y"; CONFIG_FW_LOADER = "m"; }; 202 }); 203 204 passthru = basicArgs // { 205 features = kernelFeatures; 206 inherit commonStructuredConfig structuredExtraConfig extraMakeFlags isZen isHardened isLibre modDirVersion; 207 isXen = lib.warn "The isXen attribute is deprecated. All Nixpkgs kernels that support it now have Xen enabled." true; 208 passthru = kernel.passthru // (removeAttrs passthru [ "passthru" ]); 209 tests = let 210 overridableKernel = finalKernel // { 211 override = args: 212 lib.warn ( 213 "override is stubbed for NixOS kernel tests, not applying changes these arguments: " 214 + toString (lib.attrNames (if lib.isAttrs args then args else args {})) 215 ) overridableKernel; 216 }; 217 in [ (nixosTests.kernel-generic.testsForKernel overridableKernel) ] ++ kernelTests; 218 }; 219 220 finalKernel = lib.extendDerivation true passthru kernel; 221in finalKernel