From 61ab7e84de28ec85dc026a88301f7aa63d2aa1d7 Mon Sep 17 00:00:00 2001 From: Bernardo Meurer Date: Sun, 11 Jul 2021 22:11:21 -0700 Subject: [PATCH 1/4] linuxManualConfig: patch all shebangs in scripts/ There are many scripts in `scripts/` which may be called by the build, depending on how the user chooses to configure the kernel. For example, `scripts/jobserver-exec` is called whenever the kernel is being built with LLVM tooling, and without this patch that build will fail due to the broken shebang. This patch makes us fix _all_ scripts, as well as add a dependency on python3Minimal, since a lot of the aforementioned scripts are written in Python3 instead of shell. --- pkgs/os-specific/linux/kernel/manual-config.nix | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pkgs/os-specific/linux/kernel/manual-config.nix b/pkgs/os-specific/linux/kernel/manual-config.nix index fda2881a8d2a..d40a36936a26 100644 --- a/pkgs/os-specific/linux/kernel/manual-config.nix +++ b/pkgs/os-specific/linux/kernel/manual-config.nix @@ -1,5 +1,5 @@ { lib, buildPackages, runCommand, nettools, bc, bison, flex, perl, rsync, gmp, libmpc, mpfr, openssl -, libelf, cpio, elfutils, zstd, gawk +, libelf, cpio, elfutils, zstd, gawk, python3Minimal , writeTextFile }: @@ -121,7 +121,7 @@ let # See also https://kernelnewbies.org/BuildId sed -i Makefile -e 's|--build-id=[^ ]*|--build-id=none|' - patchShebangs scripts/ld-version.sh + patchShebangs scripts ''; postPatch = '' @@ -307,7 +307,7 @@ stdenv.mkDerivation ((drvAttrs config stdenv.hostPlatform.linux-kernel kernelPat enableParallelBuilding = true; depsBuildBuild = [ buildPackages.stdenv.cc ]; - nativeBuildInputs = [ perl bc nettools openssl rsync gmp libmpc mpfr gawk zstd ] + nativeBuildInputs = [ perl bc nettools openssl rsync gmp libmpc mpfr gawk zstd python3Minimal ] ++ optional (stdenv.hostPlatform.linux-kernel.target == "uImage") buildPackages.ubootTools ++ optional (lib.versionAtLeast version "4.14" && lib.versionOlder version "5.8") libelf # Removed util-linuxMinimal since it should not be a dependency. From 89deec56234511babe7478a44f684afcec83b394 Mon Sep 17 00:00:00 2001 From: Bernardo Meurer Date: Sun, 11 Jul 2021 21:55:40 -0700 Subject: [PATCH 2/4] buildLinux: apply hostPlatform.linux-kernel.makeFlags to generate-config.pl This enforces that the configuration generated will obey any/all flags set in the platform/stdenv configuration. This is crucial, for example, if you'd like to build a kernel using clang. Without this patch, anything you set in `stdenv.hostPlatform.linux-kernel.makeFlags` is wholly ignored during config generation, causing (for example) any changes in the desired toolchain (e.g. `LLVM`, `LLVM_IAS`) to not be reflected in the generated config, and for the subsequent build to fail. --- pkgs/os-specific/linux/kernel/generate-config.pl | 3 ++- pkgs/os-specific/linux/kernel/generic.nix | 8 ++++++-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/pkgs/os-specific/linux/kernel/generate-config.pl b/pkgs/os-specific/linux/kernel/generate-config.pl index 82e1cb66e2be..df807188f14f 100644 --- a/pkgs/os-specific/linux/kernel/generate-config.pl +++ b/pkgs/os-specific/linux/kernel/generate-config.pl @@ -19,6 +19,7 @@ my $autoModules = $ENV{'AUTO_MODULES'}; my $preferBuiltin = $ENV{'PREFER_BUILTIN'}; my $ignoreConfigErrors = $ENV{'ignoreConfigErrors'}; my $buildRoot = $ENV{'BUILD_ROOT'}; +my $makeFlags = $ENV{'MAKE_FLAGS'}; $SIG{PIPE} = 'IGNORE'; # Read the answers. @@ -40,7 +41,7 @@ close ANSWERS; sub runConfig { # Run `make config'. - my $pid = open2(\*IN, \*OUT, "make -C $ENV{SRC} O=$buildRoot config SHELL=bash ARCH=$ENV{ARCH} CC=$ENV{CC} HOSTCC=$ENV{HOSTCC} HOSTCXX=$ENV{HOSTCXX}"); + my $pid = open2(\*IN, \*OUT, "make -C $ENV{SRC} O=$buildRoot config SHELL=bash ARCH=$ENV{ARCH} CC=$ENV{CC} HOSTCC=$ENV{HOSTCC} HOSTCXX=$ENV{HOSTCXX} $makeFlags"); # Parse the output, look for questions and then send an # appropriate answer. diff --git a/pkgs/os-specific/linux/kernel/generic.nix b/pkgs/os-specific/linux/kernel/generic.nix index b35c84513e69..f208da7f30ec 100644 --- a/pkgs/os-specific/linux/kernel/generic.nix +++ b/pkgs/os-specific/linux/kernel/generic.nix @@ -116,6 +116,8 @@ let # e.g. "bzImage" kernelTarget = stdenv.hostPlatform.linux-kernel.target; + makeFlags = lib.optionals (stdenv.hostPlatform.linux-kernel ? makeFlags) stdenv.hostPlatform.linux-kernel.makeFlags; + prePatch = kernel.prePatch + '' # Patch kconfig to print "###" after every question so that # generate-config.pl from the generic builder can answer them. @@ -137,13 +139,15 @@ let make -C . O="$buildRoot" $kernelBaseConfig \ ARCH=$kernelArch \ HOSTCC=$HOSTCC HOSTCXX=$HOSTCXX HOSTAR=$HOSTAR HOSTLD=$HOSTLD \ - CC=$CC OBJCOPY=$OBJCOPY OBJDUMP=$OBJDUMP READELF=$READELF + 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=. perl -w $generateConfig + PREFER_BUILTIN=$preferBuiltin BUILD_ROOT="$buildRoot" SRC=. MAKE_FLAGS="$makeFlags" \ + perl -w $generateConfig ''; installPhase = "mv $buildRoot/.config $out"; From ea167e8ccbcae6a3378e622255d9705d26f33058 Mon Sep 17 00:00:00 2001 From: Bernardo Meurer Date: Sun, 11 Jul 2021 21:58:16 -0700 Subject: [PATCH 3/4] buildLinux: take and propagate extraMakeFlags This is just for practicity, as it allows users of buildLinux to pass along extra flags they need in the kernel's make invocation. This makes, for example, supporting LLVM _much_ easier, and could enable us in the future to provide clang-built kernels. --- pkgs/os-specific/linux/kernel/generic.nix | 13 +++++++++---- pkgs/os-specific/linux/kernel/manual-config.nix | 8 ++++++-- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/pkgs/os-specific/linux/kernel/generic.nix b/pkgs/os-specific/linux/kernel/generic.nix index f208da7f30ec..29557b8d9e2e 100644 --- a/pkgs/os-specific/linux/kernel/generic.nix +++ b/pkgs/os-specific/linux/kernel/generic.nix @@ -21,6 +21,9 @@ , # Legacy overrides to the intermediate kernel config, as string extraConfig ? "" + # Additional make flags passed to kbuild +, extraMakeFlags ? [] + , # kernel intermediate config overrides, as a set structuredExtraConfig ? {} @@ -97,7 +100,7 @@ let in lib.concatStringsSep "\n" ([baseConfigStr] ++ configFromPatches); configfile = stdenv.mkDerivation { - inherit ignoreConfigErrors autoModules preferBuiltin kernelArch; + inherit ignoreConfigErrors autoModules preferBuiltin kernelArch extraMakeFlags; pname = "linux-config"; inherit version; @@ -116,7 +119,8 @@ let # e.g. "bzImage" kernelTarget = stdenv.hostPlatform.linux-kernel.target; - makeFlags = lib.optionals (stdenv.hostPlatform.linux-kernel ? makeFlags) stdenv.hostPlatform.linux-kernel.makeFlags; + makeFlags = lib.optionals (stdenv.hostPlatform.linux-kernel ? makeFlags) stdenv.hostPlatform.linux-kernel.makeFlags + ++ extraMakeFlags; prePatch = kernel.prePatch + '' # Patch kconfig to print "###" after every question so that @@ -136,7 +140,8 @@ let export HOSTLD=$LD_FOR_BUILD # Get a basic config file for later refinement with $generateConfig. - make -C . O="$buildRoot" $kernelBaseConfig \ + make $makeFlags \ + -C . O="$buildRoot" $kernelBaseConfig \ ARCH=$kernelArch \ HOSTCC=$HOSTCC HOSTCXX=$HOSTCXX HOSTAR=$HOSTAR HOSTLD=$HOSTLD \ CC=$CC OBJCOPY=$OBJCOPY OBJDUMP=$OBJDUMP READELF=$READELF \ @@ -176,7 +181,7 @@ let }; # end of configfile derivation kernel = (callPackage ./manual-config.nix {}) { - inherit version modDirVersion src kernelPatches randstructSeed lib stdenv extraMeta configfile; + inherit version modDirVersion src kernelPatches randstructSeed lib stdenv extraMakeFlags extraMeta configfile; config = { CONFIG_MODULES = "y"; CONFIG_FW_LOADER = "m"; }; }; diff --git a/pkgs/os-specific/linux/kernel/manual-config.nix b/pkgs/os-specific/linux/kernel/manual-config.nix index d40a36936a26..77add0aef539 100644 --- a/pkgs/os-specific/linux/kernel/manual-config.nix +++ b/pkgs/os-specific/linux/kernel/manual-config.nix @@ -19,6 +19,8 @@ in { stdenv, # The kernel version version, + # Additional kernel make flags + extraMakeFlags ? [], # The version of the kernel module directory modDirVersion ? version, # The kernel source (tarball, git checkout, etc.) @@ -173,7 +175,9 @@ let "KBUILD_BUILD_VERSION=1-NixOS" kernelConf.target "vmlinux" # for "perf" and things like that - ] ++ optional isModular "modules"; + ] + ++ optional isModular "modules" + ++ extraMakeFlags; installFlags = [ "INSTALLKERNEL=${installkernel}" @@ -325,7 +329,7 @@ stdenv.mkDerivation ((drvAttrs config stdenv.hostPlatform.linux-kernel kernelPat "ARCH=${stdenv.hostPlatform.linuxArch}" ] ++ lib.optional (stdenv.hostPlatform != stdenv.buildPlatform) [ "CROSS_COMPILE=${stdenv.cc.targetPrefix}" - ]; + ] ++ extraMakeFlags; karch = stdenv.hostPlatform.linuxArch; }) From e21183a95d81e0cd5b7d15d73373ac2e9195114f Mon Sep 17 00:00:00 2001 From: Bernardo Meurer Date: Tue, 13 Jul 2021 02:18:32 -0700 Subject: [PATCH 4/4] buildLinux: preserve structuredExtraConfig and extraMakeFlags in passthru This allows users to override custom kernel packages (e.g. linux_xanmod) that set their own structuredExtraConfig with ease. --- pkgs/os-specific/linux/kernel/generic.nix | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/pkgs/os-specific/linux/kernel/generic.nix b/pkgs/os-specific/linux/kernel/generic.nix index 29557b8d9e2e..aa0f19858b84 100644 --- a/pkgs/os-specific/linux/kernel/generic.nix +++ b/pkgs/os-specific/linux/kernel/generic.nix @@ -160,7 +160,6 @@ let 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; } ]; @@ -188,7 +187,7 @@ let passthru = { features = kernelFeatures; - inherit commonStructuredConfig isZen isHardened isLibre modDirVersion; + inherit commonStructuredConfig structuredExtraConfig extraMakeFlags isZen isHardened isLibre modDirVersion; isXen = lib.warn "The isXen attribute is deprecated. All Nixpkgs kernels that support it now have Xen enabled." true; kernelOlder = lib.versionOlder version; kernelAtLeast = lib.versionAtLeast version;