Get started by cloning this repository.
git clone git@github.com:jordanisaacs/kernel-module-flake
cd kernel-module-flake
# nix develop .# or direnv allow to get into the dev environment
runvm # Calls QEMU with the necessary commands, uses sudo for enabling kvm
#### Inside QEMU
# insmod module/helloworld.ko # Load the kernel module
# rmmod module/helloworld.ko # Unload the module
#### C^A+X to exit
cd helloworld
bear -- make # generate the compile_commands.json
vim helloworld.c # Start editing!
# exit and then nix develop .# or just direnv reload
# to rebuild and update the runvm command
Time to build (on Ryzen 5 3600 - 6 cores):
A custom kernel is built according to Chris Done's Build and run minimal Linux / Busybox systems in Qemu. Also some extra config is added which I got through Kaiwan Billimoria's Linux Kernel Programming. In order to do this in Nix a custom config file is generated using a modified version of the configfile
derivation in the generic kernel builder also known as the buildLinux
function. This was necessary as the default NixOS distribution defaults needed to be removed. More documentation is inside the flake. A new package set called linuxDev
is then added as an overlay using linuxPackagesFor
.
The initial ram disk is built using the new make-initrd-ng. It is called through its nix wrapper which safely copies the nix store packages needed over. Busybox is included and the helloworld kernel module.
The kernel module is built using nix. You can build it manually with nix build .#helloworld
. It is included in the initramfs.
A neovim editor is provided that is set up for Nix and C (CCLS) with LSPs for both and autoformatting. See my neovim-flake for more details. The clang-format was copied over from the linux source tree. To get CCLS working correctly call bear -- make
to get a compile_commands.json
.
If you have nix-direnv enabled a shell with everything you need should open when you cd
into the directory after calling direnv allow
The flake.nix is documented so it should be self explanatory for editing it to your needs. I will keep updating it with more features as I learn more about kernel development.