nixpkgs/pkgs/os-specific/linux/kernel/generic.nix

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

294 lines
10 KiB
Nix
Raw Normal View History

{ buildPackages
, callPackage
, perl
, bison ? null
, flex ? null
, gmp ? null
, libmpc ? null
, mpfr ? null
, pahole
2021-01-15 14:45:37 +00:00
, lib
, stdenv
, rustc
, rustPlatform
, rust-bindgen
2024-04-25 15:42:22 +01:00
# testing
, emptyFile
, nixos
, nixosTests
2024-03-22 13:34:47 +00:00
}@args':
let overridableKernel =
lib.makeOverridable ({ # 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 ? ""
# Additional make flags passed to kbuild
, extraMakeFlags ? []
, # enables the options in ./common-config.nix; if `false` then only
# `structuredExtraConfig` is used
enableCommonConfig ? true
, # kernel intermediate config overrides, as a set
structuredExtraConfig ? {}
, # The version number used for the module directory
# If unspecified, this is determined automatically from the version.
modDirVersion ? null
, # An attribute set whose attributes express the availability of
2023-11-30 15:23:54 +00:00
# certain features in this kernel. E.g. `{ia32Emulation = 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 ? []
, ignoreConfigErrors ? stdenv.hostPlatform.linux-kernel.name != "pc"
, extraMeta ? {}
2020-10-29 08:15:05 +00:00
, isZen ? false
, isLibre ? false
, isHardened ? false
# easy overrides to stdenv.hostPlatform.linux-kernel members
, autoModules ? stdenv.hostPlatform.linux-kernel.autoModules
, preferBuiltin ? stdenv.hostPlatform.linux-kernel.preferBuiltin or false
, kernelArch ? stdenv.hostPlatform.linuxArch
2024-10-20 11:42:34 +01:00
, kernelTests ? {}
2024-03-22 13:34:47 +00:00
, stdenv ? args'.stdenv
, buildPackages ? args'.buildPackages
, ...
}@args:
# 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.
assert stdenv.hostPlatform.isLinux;
let
# 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" "pname" "src" ])) (lib.attrNames args));
# Combine the `features' attribute sets of all the kernel patches.
2021-01-25 06:57:48 +00:00
kernelFeatures = lib.foldr (x: y: (x.features or {}) // y) ({
efiBootStub = true;
netfilterRPFilter = true;
ia32Emulation = true;
} // features) kernelPatches;
commonStructuredConfig = import ./common-config.nix {
2021-01-15 14:45:37 +00:00
inherit lib stdenv version;
rustAvailable =
lib.any (lib.meta.platformMatch stdenv.hostPlatform) rustc.targetPlatforms
&& lib.all (p: !lib.meta.platformMatch stdenv.hostPlatform p) rustc.badTargetPlatforms
# Known to be broken: https://lore.kernel.org/lkml/31885EDD-EF6D-4EF1-94CA-276BA7A340B7@kernel.org/T/
&& !(stdenv.hostPlatform.isRiscV && stdenv.cc.isGNU);
features = kernelFeatures; # Ensure we know of all extra patches, etc.
};
intermediateNixConfig = configfile.moduleStructuredConfig.intermediateNixConfig
# extra config in legacy string format
+ extraConfig
+ stdenv.hostPlatform.linux-kernel.extraConfig or "";
structuredConfigFromPatches =
map ({extraStructuredConfig ? {}, ...}: {settings=extraStructuredConfig;}) kernelPatches;
# appends kernel patches extraConfig
kernelConfigFun = baseConfigStr:
let
configFromPatches =
map ({extraConfig ? "", ...}: extraConfig) kernelPatches;
in lib.concatStringsSep "\n" ([baseConfigStr] ++ configFromPatches);
withRust = ((configfile.moduleStructuredConfig.settings.RUST or {}).tristate or null) == "y";
configfile = stdenv.mkDerivation {
inherit ignoreConfigErrors autoModules preferBuiltin kernelArch extraMakeFlags;
2019-08-13 22:52:01 +01:00
pname = "linux-config";
inherit version;
generateConfig = ./generate-config.pl;
kernelConfig = kernelConfigFun intermediateNixConfig;
passAsFile = [ "kernelConfig" ];
2018-01-16 03:28:45 +00:00
depsBuildBuild = [ buildPackages.stdenv.cc ];
nativeBuildInputs = [ perl gmp libmpc mpfr bison flex ]
++ lib.optional (lib.versionAtLeast version "5.2") pahole
++ lib.optionals withRust [ rust-bindgen rustc ]
;
RUST_LIB_SRC = lib.optionalString withRust rustPlatform.rustLibSrc;
platformName = stdenv.hostPlatform.linux-kernel.name;
# e.g. "defconfig"
kernelBaseConfig = if defconfig != null then defconfig else stdenv.hostPlatform.linux-kernel.baseConfig;
2017-10-28 20:09:54 +01:00
makeFlags = lib.optionals (stdenv.hostPlatform.linux-kernel ? makeFlags) stdenv.hostPlatform.linux-kernel.makeFlags
++ extraMakeFlags;
postPatch = kernel.postPatch + ''
Greatly reduce kernel closure size Based on access analysis with strace, I determined an essentially minimal required set of files from the kernel source that was needed to build all current kernel packages on 3.10, which ultimately resulted in keeping 30M of source. Generalizing from that minimal set, which required ad-hoc specifications of which headers outside of include/ and arch/*/include and which files in the scripts/ directory should be kept, to a policy of keeping all non-arch-specific headers that aren't part of the drivers/ directory and the entire scripts/ directory added an additional 17M, but there was nothing in the analysis that indicated that that ad-hoc specification was at all complete so I think the extra hit is worth the likely greater compatibility. For reference, we now keep: * All headers that are NOT in arch/${notTargetArch}/include or drivers/ * The scripts/ directory * Makefile * arch/${targetArch}/Makefile IMO the most likely cause of future problems are the headers in drivers/, but hopefully they won't actually be needed as they add 50M Ideally kernel packages would only use include and arch/${targetArch}/include, but alas this is observably not the case. master: * $out * size: 234M * references-closure: linux-headers, glibc, attr, acl, zlib, gcc, coreutils, perl, bash merge-kernel-builds: * $out * size: 152M * references-closure: none * $dev * size: 57M * references-closure: linux-headers, glibc, zlib, gcc So even with the non-minimal set we still beat out master. Keeping the drivers headers would make us only slightly bigger. Signed-off-by: Shea Levy <shea@shealevy.com>
2014-01-05 11:55:47 +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
'';
2018-07-24 20:37:22 +01:00
preUnpack = kernel.preUnpack or "";
inherit (kernel) src patches;
buildPhase = ''
export buildRoot="''${buildRoot:-build}"
export HOSTCC=$CC_FOR_BUILD
export HOSTCXX=$CXX_FOR_BUILD
export HOSTAR=$AR_FOR_BUILD
export HOSTLD=$LD_FOR_BUILD
# Get a basic config file for later refinement with $generateConfig.
make $makeFlags \
-C . O="$buildRoot" $kernelBaseConfig \
ARCH=$kernelArch \
HOSTCC=$HOSTCC HOSTCXX=$HOSTCXX HOSTAR=$HOSTAR HOSTLD=$HOSTLD \
CC=$CC OBJCOPY=$OBJCOPY OBJDUMP=$OBJDUMP READELF=$READELF \
$makeFlags
# 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 \
PREFER_BUILTIN=$preferBuiltin BUILD_ROOT="$buildRoot" SRC=. MAKE_FLAGS="$makeFlags" \
perl -w $generateConfig
'';
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
] ++ lib.optionals enableCommonConfig [
{ settings = commonStructuredConfig; _file = "pkgs/os-specific/linux/kernel/common-config.nix"; }
] ++ [
{ settings = structuredExtraConfig; _file = "structuredExtraConfig"; }
]
++ structuredConfigFromPatches
;
}).config;
structuredConfig = moduleStructuredConfig.settings;
};
}; # end of configfile derivation
kernel = (callPackage ./manual-config.nix { inherit lib stdenv buildPackages; }) (basicArgs // {
inherit kernelPatches randstructSeed extraMakeFlags extraMeta configfile modDirVersion;
pos = builtins.unsafeGetAttrPos "version" args;
config = {
CONFIG_MODULES = "y";
CONFIG_FW_LOADER = "y";
CONFIG_RUST = if withRust then "y" else "n";
};
});
in
kernel.overrideAttrs (finalAttrs: previousAttrs: {
passthru = previousAttrs.passthru or { } // basicArgs // {
features = kernelFeatures;
inherit commonStructuredConfig structuredExtraConfig extraMakeFlags isZen isHardened isLibre;
isXen = lib.warn "The isXen attribute is deprecated. All Nixpkgs kernels that support it now have Xen enabled." true;
# 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
]);
});
tests = let
overridableKernel = finalAttrs.finalPackage // {
override = args:
lib.warn (
"override is stubbed for NixOS kernel tests, not applying changes these arguments: "
+ toString (lib.attrNames (lib.toFunction args { }))
) overridableKernel;
};
2024-04-25 15:42:22 +01:00
/* Certain arguments must be evaluated lazily; so that only the output(s) depend on them.
Original reproducer / simplified use case:
*/
versionDoesNotDependOnPatchesEtcNixOS =
builtins.seq
(nixos ({ config, pkgs, ... }: {
boot.kernelPatches = [
(builtins.seq config.boot.kernelPackages.kernel.version { patch = pkgs.emptyFile; })
];
})).config.boot.kernelPackages.kernel.outPath
emptyFile;
versionDoesNotDependOnPatchesEtc =
builtins.seq
(import ./generic.nix args' (args // (
let explain = attrName:
''
The ${attrName} attribute must be able to access the kernel.version attribute without an infinite recursion.
That means that the kernel attrset (attrNames) and the kernel.version attribute must not depend on the ${attrName} argument.
The fact that this exception is raised shows that such a dependency does exist.
This is a problem for the configurability of ${attrName} in version-aware logic such as that in NixOS.
Strictness can creep in through optional attributes, or assertions and warnings that run as part of code that shouldn't access what is checked.
'';
in {
kernelPatches = throw (explain "kernelPatches");
structuredExtraConfig = throw (explain "structuredExtraConfig");
modDirVersion = throw (explain "modDirVersion");
}))).version
emptyFile;
2024-10-20 11:42:34 +01:00
in {
inherit versionDoesNotDependOnPatchesEtc;
testsForKernel = nixosTests.kernel-generic.passthru.testsForKernel overridableKernel;
2024-04-25 15:42:22 +01:00
# Disabled by default, because the infinite recursion is hard to understand. The other test's error is better and produces a shorter trace.
2024-10-20 11:42:34 +01:00
# inherit versionDoesNotDependOnPatchesEtcNixOS;
} // kernelTests;
};
}));
in overridableKernel