Load automatically kernel module, and deal with parameters

Thanks to clever help, I clarified everything.

So basically, when you put a module inside boot.extraModulePackages, it is used to make the modules available to modprobe, but it will not load them automatically (modprobe will search for module names inside inside /run/current-system/kernel-modules/lib/modules/$KER_VER). So for example, we can load our module manually with:

sudo modprobe v4l2loopback-dc

or if the module has options:

sudo modprobe v4l2loopback-dc width=320 height=240

Now, if we want the module to autoload, we need to insert it inside boot.kernelModules:

boot.kernelModules = [ "v4l2loopback-dc" ];

if we want to autoload snd-aloop, we can just add it to the list:

boot.kernelModules = [ "v4l2loopback-dc" "snd-aloop" ];

We can also define default options by adding an entry in boot.extraModprobeConfig (the module must still be in kernelModules) in order to specify default options if necessary:

boot.extraModprobeConfig = ''
  options v4l2loopback-dc width=320 height=240
'';

Beware, the modules will only be loaded at reboot time, and the default options will only apply if you reload a module, for example using:

sudo modprobe -r v4l2loopback-dc
sudo modprobe v4l2loopback-dc

so you should manually load the module using:

sudo modprobe v4l2loopback-dc

or create a systemd daemon that loads the module for you (wireguard’s for example does modprobe wg at startup automatically).

Finally, some modules have parameters that can be changed automatically without reloading the module. See the parameters in:

ls /sys/module/zfs/parameters/ -al

for example to see some parameters that are writable. Note however that it’s up to the module to take these live-change of parameter into account.

5 Likes