How to build the kernel faster?

I’d like to do some kernel patch testing and bisecting, in other words, I want to rebuild the kernel 20 times in a row.

My CPU is AMD Ryzen 7 7840U, so not the worst out there, but the build takes like half an hour, which, I think, is kinda insane, and I am not really sure why. Am I right in thinking that the default NixOS config is just really heavy and builds way too many modules? I was expecting it to take a couple of minutes.

Is there some best-practice / guide on making the kernel build fast? I think I am inclined to simply start from a defconfig and tweak it to make sure it runs on my hardware.

it answers “m” if possible

So yes.

But indeed you can pass your own configfile

See this:

4 Likes

You can use CCache using an overlay, and it will cache partial build products between runs. This may speed up kernel bisects.

There’s also this thread discussing how to get it working for kernel builds.

1 Like

@kirelagin I’m curious if CCache ended up working for you. How did you go?

It is indeed insanely heavy as any distro config is (and should be) since it’s building huge numbers of drivers.

I think I am inclined to simply start from a defconfig and tweak it to make sure it runs on my hardware.

The biggest speedup will probably just come from using make localmodconfig - see this StackExchange answer. This is not a well-lit path you will need to do a bunch of fiddling to get it working for you but hopefully a useful pointer at least.

Also note there are a bunch of big subsystems that can save you a lot of time if you don’t need them, e.g. localmodconfig will say “they have a GPU so obviously they need Nouveau” or whatever but if your system is actually headless you can save time by disabling CONFIG_DRM. Similar for USB and WIRELESS. (Although, you need USB more often than you think, IIRC last time I disabled I thought “I’ve got nothing plugged into the USB ports so I can disable USB”, but then it turned out the mobo was using a soldered USB connection for something I needed).

Another warning: localmodconfig will disable everything you aren’t currently using which can disable stuff other than drivers. E.g. there is network stuff that gets dynamically loaded. So ideally if you have a workload to test, have that workload running while you run make localmodconfig. But if you get this wrong it’s not too bad - if you disabled an important driver the system will probably fail to boot in an annoying way. If you disable some network feature then you’ll usually get a nice “protocol not suppported” message or whatever.