Issues with Applying Patches to a Custom Kernel Build


So I’ve been working on trying to apply some patches to a custom kernel build
for NixOS. I’ve gotten pretty far, but run into a few issues. First off, code for the build in my configuration.nix.

Initially, it would download, unpack, and begin building the kernel without issue. However, since adding the patches to my srcs, I’ve started getting this
error when I try to build the configuration:

/nix/store/wgap303sj9zqz63gw7nqxvf4dqz2hgai-stdenv-linux/setup: line 81: cd: tools/power/cpupower: No such file or directory

Any help is appreciated, as I am quite new to NixOS and NixPkg, and not quite sure whats going on here.


Do you have a copy of the previous working config? As it stands, it looks to me like the problem might be that you’re trying to reference the patch from a local directory in the line

patch = "linux-surface/patches/5.10/0001-surface-oemb.patch";

when all the implementations I see in nixpkgs import it directly as in : nixpkgs/linux-rt-5.10.nix at d600f006643e074c2ef1d72e462e218b647a096c · NixOS/nixpkgs · GitHub

At the same time, I haven’t compiled a kernel though, so I’m not sure.

The only difference between the previous configuration and this one is that the
patch variable was set to [], and the srcs variable only fetched the kernel source.

As far as how I’m referencing the patch files, removing the quotes presents this error and halts the build before it can get to the error in the OP:

error: getting attributes of path '/linux-surface/linux-surface/patches/5.10/0001-surface3-oemb.patch': No such file or directory

My guess is that buildLinux doesn’t support having multiple srcs. So you can instead make a let variabled just with the patches and just reference the files in that in kernelPatches.

Like this:

boot.kernelPackages = let
      linux_sgx_pkg = {fetchurl, buildLinux, ...} @ args: let
          patches = fetchurl {
              url = "";
              sha256 = "7923813cbc519b692ced9b620ebe7b804c0767322791358f70e3c20bc62e6eae"; 
        in buildLinux (args // rec {
          version = "5.10.24";
          modDirVersion = version;
          src = fetchurl {
            url = "";
            sha256 = "c6dcd04e5893c5d68b637188f904528e91f28d790cd49cf8a7fb817423bd763f";
        kernelPatches = [ { 
          name = "surface-config";
          # figure out how to apply patches HERE
          patch = "${patches}/linux-surface/patches/5.10/0001-surface3-oemb.patch";
          extraConfig = ''
        } ];
	extraMeta.branch = "5.4";
       } // (args.argsOverride or {}));
     linux_sgx = pkgs.callPackage linux_sgx_pkg{};
     pkgs.recurseIntoAttrs (pkgs.linuxPackagesFor linux_sgx);

Alright, just tested it out - and it works now. There were actually two issues:

  1. buildLinux indeed does not support multiple sources, so I did need to
    make patches a let variable.

  2. A side effect (from what I understand) of it not supporting multiple sources is that it won’t unpack patches. Instead of calling fetchurl, I switched it to fetchzip, and the patches apply without issue.

Thanks for the help!

EDIT: One other thing I forgot to mention is that for setting kernel options, it doesn’t follow the same convention as the .config file you’d find in a standard kernel build does. Instead of, say, CONFIG_SURFACE_AGGREGATOR=m, you would write CONFIG_SURFACE_AGGREGATOR m. In this case, I also needed to add ignoreConfigErrors = true;, otherwise it would halt building because it didn’t know (?) what the new options the patches added were.