Build aarch64 image kernel with extra modules

I want to add to the default nixos kernel modules extra mkt_soc_eth module, which is not enabled in default kernel config.

Currently, only solution I found is to add extra NET_MEDIATEK_SOC=m kernel config option to the boot.kernelPatches[].extraConfig attribute. However it leads to full kernel recompile which takes a log of time. Is it possible to take default kernel from binary cache and only compile one extra module?

I think you could try this.

{ config, ... }:
{
  boot.extraModulePackages = [
    (config.boot.kernelPackages.callPackage ./mtk-eth.nix { })
  ];
}

mtk-eth.nix:

{ lib, stdenv, kernel }:

stdenv.mkDerivation rec {
  pname = "mkt_soc_eth";
  version = kernel.version;

  src = kernel.src;

  setSourceRoot = ''
    export sourceRoot=$(pwd)/${kernel.name}/drivers/net/ethernet/mediatek
  '';

  nativeBuildInputs = kernel.moduleBuildDependencies;

  makeFlags = [
    "-C"
    "${kernel.dev}/lib/modules/${kernel.modDirVersion}/build"
    "M=$(sourceRoot)"
    "CONFIG_NET_MEDIATEK_SOC=m"
  ];

  buildFlags = [ "modules" ];
  installFlags = [ "INSTALL_MOD_PATH=${placeholder "out"}" ];
  installTargets = [ "modules_install" ];

  meta = {
    inherit (kernel.meta) license platforms;
  };
}
1 Like

I think it would be nice if there was a derivation per kernel module by default, this would help with making small images for embedded devices without extra compiling.

You might have to change the setSourceRoot part to something else if you have a different kernel, I am not sure if that source directory layout is followed by all kernel versions.

Yes, this method did the trick, thank you!