What's the right way to make a custom kernel module available?

I’m trying to set up a kernel module that provides some additional hardware support for my laptop. The module compiles and loads fine, but I’m struggling with how to make the module available in a relatively generic fashion so that it (1) gets rebuilt automatically when my kernel gets upgraded, and (2) is available to (relatively) arbitrary kernel packages. Right now, all of the solutions I’ve been able to find work by extending a particular version of linuxPackages. For example:

nixpkgs.overlays = [
  (self: super: {
    linuxPackages = super.linuxPackages.extend (lpself: lpsuper: {
      my-new-module = lpself.callPackage ./my-new-module {};

(This particular example follows the system76-nixos repository, but another post on the NixOS Discourse uses a similar approach.)

The problem is that this extends linuxPackages, but not say linuxPackages_latest or linuxPackages_hardened. Ideally, I’d like to have some way to make the kernel module automatically available whichever Linux kernel I install. Naively, I’d think that maybe what I want to do is to extend config.boot.kernelPackages, but doing so doesn’t seem to work (it results in an attribute 'my-new-module' missing error).

Is there a canonical way to do this, or a more flexible example than the above that I could study?


1 Like

I went this way:

  boot.extraModulePackages = [
    (config.boot.kernelPackages.callPackage ./newmod {})

seems to work.

Is there any update on this? The solution provided by @t184256 is not as generic as I like… I’d like to provide a flake providing some packages and/or overlays that allows me to install a module like:

  boot.extraModulePackages = [