Kernel build uses more than 16 GB of space

The default kernel configuration in NixOS is insanely bloated, don’t even try to compile it unless you have some server as a dedicated build farm. It can easily take hours to build on a desktop, even if you manage to get enough disk and memory to do it. So, you can’t simply add your patch to boot.kernelPatches and call it a day, but there is a way around.

You can generate a kernel configuration that enables only the modules you really need by looking at what is currently loaded: you have to run make localmodconfig in the kernel source tree, starting from a base configuration. There’s also this tool to help maintain the list up-to-date.

After you’ve got a tailored configuration, you can build your kernel with something like this:

boot.kernelPackages =
  let
    kernel = pkgs.linuxManualConfig {
      inherit (pkgs.linux_6_15) src version;
        configfile = ./kernel.config;
        allowImportFromDerivation = true;
        kernelPatches = lib.singleton
          { name = "my-important-patch";
            patch = pkgs.fetchurl { ... };
          };
      };
  in pkgs.linuxPackagesFor kernel;

Don’t worry too much about getting everything right the first time, because the compilation time will be of the order of a few minutes, so you can quickly iterate.

6 Likes