2020-04-24 23:36:52 +00:00
|
|
|
{ buildPackages
|
|
|
|
, callPackage
|
|
|
|
, perl
|
|
|
|
, bison ? null
|
|
|
|
, flex ? null
|
|
|
|
, gmp ? null
|
|
|
|
, libmpc ? null
|
|
|
|
, mpfr ? null
|
2022-08-12 12:06:08 +00:00
|
|
|
, pahole
|
2021-02-05 17:12:51 +00:00
|
|
|
, lib
|
2020-04-24 23:36:52 +00:00
|
|
|
, stdenv
|
|
|
|
|
|
|
|
, # The kernel source tarball.
|
|
|
|
src
|
|
|
|
|
|
|
|
, # The kernel version.
|
|
|
|
version
|
|
|
|
|
|
|
|
, # Allows overriding the default defconfig
|
|
|
|
defconfig ? null
|
|
|
|
|
|
|
|
, # Legacy overrides to the intermediate kernel config, as string
|
|
|
|
extraConfig ? ""
|
|
|
|
|
2021-07-16 19:40:57 +00:00
|
|
|
# Additional make flags passed to kbuild
|
|
|
|
, extraMakeFlags ? []
|
|
|
|
|
2020-04-24 23:36:52 +00:00
|
|
|
, # kernel intermediate config overrides, as a set
|
|
|
|
structuredExtraConfig ? {}
|
|
|
|
|
|
|
|
, # The version number used for the module directory
|
2022-12-28 21:21:41 +00:00
|
|
|
# If unspecified, this is determined automatically from the version.
|
|
|
|
modDirVersion ? null
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
, # An attribute set whose attributes express the availability of
|
|
|
|
# certain features in this kernel. E.g. `{iwlwifi = true;}'
|
|
|
|
# indicates a kernel that provides Intel wireless support. Used in
|
|
|
|
# NixOS to implement kernel-specific behaviour.
|
|
|
|
features ? {}
|
|
|
|
|
|
|
|
, # Custom seed used for CONFIG_GCC_PLUGIN_RANDSTRUCT if enabled. This is
|
|
|
|
# automatically extended with extra per-version and per-config values.
|
|
|
|
randstructSeed ? ""
|
|
|
|
|
|
|
|
, # A list of patches to apply to the kernel. Each element of this list
|
|
|
|
# should be an attribute set {name, patch} where `name' is a
|
|
|
|
# symbolic name and `patch' is the actual patch. The patch may
|
|
|
|
# optionally be compressed with gzip or bzip2.
|
|
|
|
kernelPatches ? []
|
2023-07-15 17:15:38 +00:00
|
|
|
, ignoreConfigErrors ? stdenv.hostPlatform.linux-kernel.name or "" != "pc"
|
2020-04-24 23:36:52 +00:00
|
|
|
, extraMeta ? {}
|
|
|
|
|
2020-11-03 02:18:15 +00:00
|
|
|
, isZen ? false
|
|
|
|
, isLibre ? false
|
|
|
|
, isHardened ? false
|
|
|
|
|
2021-02-05 17:12:51 +00:00
|
|
|
# easy overrides to stdenv.hostPlatform.linux-kernel members
|
2023-07-15 17:15:38 +00:00
|
|
|
, autoModules ? stdenv.hostPlatform.linux-kernel.autoModules or true
|
2021-02-05 17:12:51 +00:00
|
|
|
, preferBuiltin ? stdenv.hostPlatform.linux-kernel.preferBuiltin or false
|
|
|
|
, kernelArch ? stdenv.hostPlatform.linuxArch
|
2021-04-08 16:26:57 +00:00
|
|
|
, kernelTests ? []
|
2021-08-25 08:27:29 +00:00
|
|
|
, nixosTests
|
2020-04-24 23:36:52 +00:00
|
|
|
, ...
|
2021-12-06 16:07:01 +00:00
|
|
|
}@args:
|
2020-04-24 23:36:52 +00:00
|
|
|
|
2020-08-20 17:08:02 +00:00
|
|
|
# Note: this package is used for bootstrapping fetchurl, and thus
|
|
|
|
# cannot use fetchpatch! All mutable patches (generated by GitHub or
|
|
|
|
# cgit) that are needed here should be included directly in Nixpkgs as
|
|
|
|
# files.
|
|
|
|
|
2020-04-24 23:36:52 +00:00
|
|
|
assert stdenv.isLinux;
|
|
|
|
|
|
|
|
let
|
2021-12-06 16:07:01 +00:00
|
|
|
# Dirty hack to make sure that `version` & `src` have
|
|
|
|
# `<nixpkgs/pkgs/os-specific/linux/kernel/linux-x.y.nix>` as position
|
|
|
|
# when using `builtins.unsafeGetAttrPos`.
|
|
|
|
#
|
|
|
|
# This is to make sure that ofborg actually detects changes in the kernel derivation
|
|
|
|
# and pings all maintainers.
|
|
|
|
#
|
|
|
|
# For further context, see https://github.com/NixOS/nixpkgs/pull/143113#issuecomment-953319957
|
|
|
|
basicArgs = builtins.removeAttrs
|
|
|
|
args
|
|
|
|
(lib.filter (x: ! (builtins.elem x [ "version" "src" ])) (lib.attrNames args));
|
|
|
|
|
2020-04-24 23:36:52 +00:00
|
|
|
# Combine the `features' attribute sets of all the kernel patches.
|
2021-08-05 21:33:18 +00:00
|
|
|
kernelFeatures = lib.foldr (x: y: (x.features or {}) // y) ({
|
2020-04-24 23:36:52 +00:00
|
|
|
iwlwifi = true;
|
|
|
|
efiBootStub = true;
|
|
|
|
needsCifsUtils = true;
|
|
|
|
netfilterRPFilter = true;
|
|
|
|
ia32Emulation = true;
|
|
|
|
} // features) kernelPatches;
|
|
|
|
|
|
|
|
commonStructuredConfig = import ./common-config.nix {
|
2021-02-05 17:12:51 +00:00
|
|
|
inherit lib stdenv version;
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
features = kernelFeatures; # Ensure we know of all extra patches, etc.
|
|
|
|
};
|
|
|
|
|
|
|
|
intermediateNixConfig = configfile.moduleStructuredConfig.intermediateNixConfig
|
|
|
|
# extra config in legacy string format
|
|
|
|
+ extraConfig
|
2021-02-05 17:12:51 +00:00
|
|
|
+ stdenv.hostPlatform.linux-kernel.extraConfig or "";
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
structuredConfigFromPatches =
|
|
|
|
map ({extraStructuredConfig ? {}, ...}: {settings=extraStructuredConfig;}) kernelPatches;
|
|
|
|
|
|
|
|
# appends kernel patches extraConfig
|
|
|
|
kernelConfigFun = baseConfigStr:
|
|
|
|
let
|
|
|
|
configFromPatches =
|
|
|
|
map ({extraConfig ? "", ...}: extraConfig) kernelPatches;
|
|
|
|
in lib.concatStringsSep "\n" ([baseConfigStr] ++ configFromPatches);
|
|
|
|
|
|
|
|
configfile = stdenv.mkDerivation {
|
2021-07-16 19:40:57 +00:00
|
|
|
inherit ignoreConfigErrors autoModules preferBuiltin kernelArch extraMakeFlags;
|
2020-04-24 23:36:52 +00:00
|
|
|
pname = "linux-config";
|
|
|
|
inherit version;
|
|
|
|
|
|
|
|
generateConfig = ./generate-config.pl;
|
|
|
|
|
|
|
|
kernelConfig = kernelConfigFun intermediateNixConfig;
|
|
|
|
passAsFile = [ "kernelConfig" ];
|
|
|
|
|
|
|
|
depsBuildBuild = [ buildPackages.stdenv.cc ];
|
|
|
|
nativeBuildInputs = [ perl gmp libmpc mpfr ]
|
2022-08-12 12:06:08 +00:00
|
|
|
++ lib.optionals (lib.versionAtLeast version "4.16") [ bison flex ]
|
|
|
|
++ lib.optional (lib.versionAtLeast version "5.2") pahole;
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
# e.g. "defconfig"
|
2023-07-15 17:15:38 +00:00
|
|
|
kernelBaseConfig = if defconfig != null then defconfig else stdenv.hostPlatform.linux-kernel.baseConfig or "defconfig";
|
2020-04-24 23:36:52 +00:00
|
|
|
|
2021-07-16 19:40:57 +00:00
|
|
|
makeFlags = lib.optionals (stdenv.hostPlatform.linux-kernel ? makeFlags) stdenv.hostPlatform.linux-kernel.makeFlags
|
|
|
|
++ extraMakeFlags;
|
|
|
|
|
2022-11-02 22:02:43 +00:00
|
|
|
postPatch = kernel.postPatch + ''
|
2020-04-24 23:36:52 +00:00
|
|
|
# Patch kconfig to print "###" after every question so that
|
|
|
|
# generate-config.pl from the generic builder can answer them.
|
|
|
|
sed -e '/fflush(stdout);/i\printf("###");' -i scripts/kconfig/conf.c
|
|
|
|
'';
|
|
|
|
|
|
|
|
preUnpack = kernel.preUnpack or "";
|
|
|
|
|
|
|
|
inherit (kernel) src patches;
|
|
|
|
|
|
|
|
buildPhase = ''
|
|
|
|
export buildRoot="''${buildRoot:-build}"
|
2021-05-28 09:39:13 +00:00
|
|
|
export HOSTCC=$CC_FOR_BUILD
|
|
|
|
export HOSTCXX=$CXX_FOR_BUILD
|
|
|
|
export HOSTAR=$AR_FOR_BUILD
|
|
|
|
export HOSTLD=$LD_FOR_BUILD
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
# Get a basic config file for later refinement with $generateConfig.
|
2021-07-16 19:40:57 +00:00
|
|
|
make $makeFlags \
|
|
|
|
-C . O="$buildRoot" $kernelBaseConfig \
|
2020-04-24 23:36:52 +00:00
|
|
|
ARCH=$kernelArch \
|
2021-05-28 09:39:13 +00:00
|
|
|
HOSTCC=$HOSTCC HOSTCXX=$HOSTCXX HOSTAR=$HOSTAR HOSTLD=$HOSTLD \
|
2021-07-16 19:40:57 +00:00
|
|
|
CC=$CC OBJCOPY=$OBJCOPY OBJDUMP=$OBJDUMP READELF=$READELF \
|
|
|
|
$makeFlags
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
# Create the config file.
|
|
|
|
echo "generating kernel configuration..."
|
|
|
|
ln -s "$kernelConfigPath" "$buildRoot/kernel-config"
|
|
|
|
DEBUG=1 ARCH=$kernelArch KERNEL_CONFIG="$buildRoot/kernel-config" AUTO_MODULES=$autoModules \
|
2021-07-16 19:40:57 +00:00
|
|
|
PREFER_BUILTIN=$preferBuiltin BUILD_ROOT="$buildRoot" SRC=. MAKE_FLAGS="$makeFlags" \
|
|
|
|
perl -w $generateConfig
|
2020-04-24 23:36:52 +00:00
|
|
|
'';
|
|
|
|
|
|
|
|
installPhase = "mv $buildRoot/.config $out";
|
|
|
|
|
|
|
|
enableParallelBuilding = true;
|
|
|
|
|
|
|
|
passthru = rec {
|
|
|
|
module = import ../../../../nixos/modules/system/boot/kernel_config.nix;
|
|
|
|
# used also in apache
|
|
|
|
# { modules = [ { options = res.options; config = svc.config or svc; } ];
|
|
|
|
# check = false;
|
|
|
|
# The result is a set of two attributes
|
|
|
|
moduleStructuredConfig = (lib.evalModules {
|
|
|
|
modules = [
|
|
|
|
module
|
|
|
|
{ settings = commonStructuredConfig; _file = "pkgs/os-specific/linux/kernel/common-config.nix"; }
|
|
|
|
{ settings = structuredExtraConfig; _file = "structuredExtraConfig"; }
|
|
|
|
]
|
|
|
|
++ structuredConfigFromPatches
|
|
|
|
;
|
|
|
|
}).config;
|
|
|
|
|
|
|
|
structuredConfig = moduleStructuredConfig.settings;
|
|
|
|
};
|
|
|
|
}; # end of configfile derivation
|
|
|
|
|
2022-12-28 21:21:41 +00:00
|
|
|
kernel = (callPackage ./manual-config.nix { inherit lib stdenv buildPackages; }) (basicArgs // {
|
|
|
|
inherit kernelPatches randstructSeed extraMakeFlags extraMeta configfile;
|
2021-12-06 16:07:01 +00:00
|
|
|
pos = builtins.unsafeGetAttrPos "version" args;
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
config = { CONFIG_MODULES = "y"; CONFIG_FW_LOADER = "m"; };
|
2022-12-28 21:21:41 +00:00
|
|
|
} // lib.optionalAttrs (modDirVersion != null) { inherit modDirVersion; });
|
2020-04-24 23:36:52 +00:00
|
|
|
|
2021-12-06 16:07:01 +00:00
|
|
|
passthru = basicArgs // {
|
2020-04-24 23:36:52 +00:00
|
|
|
features = kernelFeatures;
|
2022-12-28 21:21:41 +00:00
|
|
|
inherit commonStructuredConfig structuredExtraConfig extraMakeFlags isZen isHardened isLibre;
|
2021-04-26 19:14:03 +00:00
|
|
|
isXen = lib.warn "The isXen attribute is deprecated. All Nixpkgs kernels that support it now have Xen enabled." true;
|
2022-12-28 21:21:41 +00:00
|
|
|
|
|
|
|
# Adds dependencies needed to edit the config:
|
|
|
|
# nix-shell '<nixpkgs>' -A linux.configEnv --command 'make nconfig'
|
|
|
|
configEnv = kernel.overrideAttrs (old: {
|
|
|
|
nativeBuildInputs = old.nativeBuildInputs or [] ++ (with buildPackages; [
|
|
|
|
pkg-config ncurses
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
2020-04-24 23:36:52 +00:00
|
|
|
passthru = kernel.passthru // (removeAttrs passthru [ "passthru" ]);
|
2021-08-25 08:27:29 +00:00
|
|
|
tests = let
|
|
|
|
overridableKernel = finalKernel // {
|
|
|
|
override = args:
|
|
|
|
lib.warn (
|
|
|
|
"override is stubbed for NixOS kernel tests, not applying changes these arguments: "
|
|
|
|
+ toString (lib.attrNames (if lib.isAttrs args then args else args {}))
|
|
|
|
) overridableKernel;
|
|
|
|
};
|
2023-08-10 07:59:29 +00:00
|
|
|
in [ (nixosTests.kernel-generic.passthru.testsForKernel overridableKernel) ] ++ kernelTests;
|
2020-04-24 23:36:52 +00:00
|
|
|
};
|
|
|
|
|
2021-08-25 08:27:29 +00:00
|
|
|
finalKernel = lib.extendDerivation true passthru kernel;
|
|
|
|
in finalKernel
|