Maximilian Bosch 5645cd22d4
linux: expose make flags for modules in linuxKernel.packages.linux_X_Y
Closes #376820

The issue with providing `moduleMakeFlags` via `passthru` of the kernel
package is that when this package gets overriden[1], the `moduleMakeFlags`
list still references the kernel without the overrides.

This broke e.g. kernel modules of linux-rpi4 which can be reproduced
with

    nix-build --argstr system aarch64-linux -A linuxKernel.packages.linux_rpi4.zfs_2_3

This used to break with

    Error: modDirVersion 6.6.51 specified in the Nix expression is wrong, it should be: 6.6.51-v8

since KBUILD_OUTPUT referenced the kernel without the changes from
`overrideDerivation` that also changes the `modDirVersion`.

The new approach is to add the build flags right into
`linuxKernel.packages.linux_X_Y`: that way we don't need any hacks to
update `moduleMakeFlags` when the derivation with the passthru gets
overridden.

By using the fixpoint of the package-set, the `kernelModuleMakeFlags`
list is correctly updated. E.g. given

    with import ./. {};

    linuxKernel.packages.linux_6_6.extend (self: super: {
      kernel = super.kernel.overrideAttrs (_: {
        name = "linux-snens";
      });
    })

the `makeFlags` is correctly updated:

    $ nix-instantiate snenskek.nix -A zfs_2_3.makeFlags --eval --strict
    [ "ARCH=x86_64" "CROSS_COMPILE=" "KBUILD_OUTPUT=/nix/store/gsp68549k1aqbwxwczpgw67w5jjn4shw-linux-snens-dev/lib/modules/6.6.74/build" ]

[1] E.g. `linux-rpi4`.
2025-01-27 16:09:50 +01:00

56 lines
1.4 KiB
Nix

{
lib,
stdenv,
fetchFromGitHub,
kernel,
kernelModuleMakeFlags,
}:
let
isKernelRT =
(kernel.structuredExtraConfig ? PREEMPT_RT)
&& (kernel.structuredExtraConfig.PREEMPT_RT == lib.kernel.yes);
in
stdenv.mkDerivation (finalAttrs: {
name = "${finalAttrs.pname}-${finalAttrs.version}-${kernel.version}";
pname = "lkrg";
version = "0.9.9";
src = fetchFromGitHub {
owner = "lkrg-org";
repo = "lkrg";
rev = "v${finalAttrs.version}";
hash = "sha256-dxgkEj8HGOX4AMZRNbhv3utrNjKDFpp7kZmj17Wp2HE=";
};
hardeningDisable = [ "pic" ];
nativeBuildInputs = kernel.moduleBuildDependencies;
makeFlags = kernelModuleMakeFlags ++ [
"KERNEL=${kernel.dev}/lib/modules/${kernel.modDirVersion}/build"
];
enableParallelBuilding = true;
dontConfigure = true;
prePatch = ''
substituteInPlace Makefile --replace "KERNEL := " "KERNEL ?= "
'';
installPhase = ''
runHook preInstall
install -D lkrg.ko $out/lib/modules/${kernel.modDirVersion}/extra/lkrg.ko
runHook postInstall
'';
meta = with lib; {
description = "LKRG Linux Kernel module";
longDescription = "LKRG performs runtime integrity checking of the Linux kernel and detection of security vulnerability exploits against the kernel.";
homepage = "https://lkrg.org/";
license = licenses.gpl2Only;
maintainers = with maintainers; [ chivay ];
platforms = platforms.linux;
broken = kernel.kernelOlder "5.10" || isKernelRT;
};
})