How to add a custom kernel module package in configuration.nix


#1

I’m trying to add a custom kernel module added to my configuration.nix.

So far I have a file called “rtl8821cu.nix”, which looks like this (not finished yet):

{ stdenv, fetchFromGitHub, pkgs, kernel }:

stdenv.mkDerivation rec {

  name = "rtl8821cu-${version}-${kernel.version}";
  version = "0278eaa";

  src = fetchFromGitHub {
    owner = "whitebatman2";
    repo = "rtl8821cu";
    rev = version;
    sha256 = "1ydx1gy5xcc0q6gx1y9v3by5blk3xlqqsgx81m0bsm1b9d6bprq2";
  };

  buildInputs = [ pkgs.bc ];

  configurePhase = ''
    cd src
    kernel_version=${kernel.modDirVersion}
    sed -i -e 's|/lib/modules|${kernel.dev}/lib/modules|' Makefile
    export makeFlags="BUILD_KERNEL=$kernel_version"
  '';
}

I’m struggling on how to correctly embed it in my configuration.nix. I currently don’t want to add it to the nixpkgs tree.

This is part of my configuration.nix:

{ config, lib, pkgs, ... }:

let
  mykern = pkgs.linuxPackages.extend (self: super: {
    rtl8821cu = (pkgs.linuxPackagesFor (pkgs.callPackage ./rtl8821cu.nix { }));
  });
in
{
  boot.extraModulePackages = [ mykern.rtl8821cu ];
}

What’s the correct way to add my kernel module to linuxPackages?