Kernel Module Flake - a small flake for kernel module development

Hi everyone! I wanted to share a flake I made recently when I wanted to write linux kernel modules without fear of breaking my system.

What is it?

A flake for getting started with kernel module development. Clone the flake and immediately get started with developing and testing your kernel module. Builds a minimally configured linux kernel (only a few minutes to compile), an initrd, and a command for starting up a QEMU machine with your module included. (Plus you can use eBPF!).

Why?

Many getting started guides for module development, compiling the linux kernel, and setting up QEMU (in the context of kernel dev) are not very helpful for Nix users. This is because compiling the Linux kernel and creating initrds using Nix binaries has specialized Nix tools/functions.

I had to spend hours digging into these functions to get everything up and running. This is a huge investment if you just want to jump in and start writing some kernel modules. The flake let’s you focus on learning to write kernel modules without all the extra tooling (eg using The Linux Kernel Module Programming Guide).

7 Likes

Sharing an update, there is now rust kernel module support! You can’t really do much but the official Linux rust sample module is buildable and loadable. Next step is to figure out how to get rust-analyzer working to make editing easier.

edit: Also added an option to use the Rust for Linux branch for the ability to actually write stuff with Rust.

4 Likes

Another update! All the builders I wrote for the flake are now exposed as a library. So rather than cloning the entire repository, you can now add it as a flake input. Then you can build modules, custom kernels, minimal initramfs, etc through the builder functions. More info is in the readme.

A new feature I added is remote GDB debugging of the kernel/modules. The gdb scripts can enabled for the kernel dev build. And a single command will run gdb, load the scripts, add the search paths, and connect to qemu.

3 Likes