To build and use an out-of-tree kernel module in NixOS using your module.nix file, follow these steps:
Step 1: Create a Nix Shell
You can create a Nix shell environment that provides the necessary dependencies (like the kernel and build tools) for building the module. You can do this by creating a shell.nix file:
{ pkgs ? import <nixpkgs> {} }:
pkgs.mkShell {
buildInputs = [
pkgs.stdenv
pkgs.linuxPackages // {
# Ensure to build against the desired kernel version
kernel = pkgs.linuxPackages.kernel;
}
pkgs.pkgconfig # If you need pkg-config for specific libraries
pkgs.git # If you need Git
];
}
Step 2: Enter the Nix Shell
Open a terminal and navigate to the directory containing your module.nix and shell.nix files. Then enter the Nix shell using the following command:
nix-shell
Step 3: Build the Module
Once in the Nix shell, you can build your module using the module.nix file:
nix-build module.nix
This will compile the module and create a package in the result directory. After a successful build, you should see a result symlink in your terminal that points to the output of the build.
Step 4: Install the Module
To install the module, you can copy the built module files to the appropriate directory. After building, you would typically use modprobe to load it, but you first need to ensure that the kernel sees the new module:
sudo cp result/lib/modules/${kernel.modDirVersion}/extra/amd-gpu-i2c.ko /lib/modules/${kernel.modDirVersion}/
After copying the module, update the module dependencies and load it:
sudo depmod -a
sudo modprobe amd-gpu-i2c
Step 5: Verify the Module is Loaded
You can verify that your module has been loaded successfully using:
lsmod | grep amd-gpu-i2c
If your module is listed, it means it has been loaded successfully.
Additional Notes
- Kernel Version: Ensure that
kernel references the correct kernel version for which you're building the module.
- Debugging Builds: If there are any build issues, review the output logs, and ensure all necessary dependencies are accounted for in the
nativeBuildInputs.
- Rebuilding After Changes: If you make any changes to the module source code, you will need to repeat the
nix-build and installation steps.
Using these steps, you should be able to build and use your custom kernel module in NixOS. Adjust the paths based on your module structure and requirements as necessary.