A proof-of-concept Linux kernel module written in Zig, demonstrating how to write kernel modules using Zig instead of C.
This project shows that it's possible to write Linux kernel modules in Zig by:
- Using Zig's C interop capabilities to interface with kernel functions
- Leveraging Zig's build system to handle kernel module compilation
- Automatically detecting kernel configuration and compiler
- Hello World kernel module - Simple module that prints messages on load/unload
- Automatic kernel detection - Detects kernel version and compiler (GCC/Clang)
- Cross-compiler support - Works with both GCC and Clang-built kernels
- Convenient build commands - Easy-to-use Zig build steps
- Linux system with kernel headers installed
- Zig compiler (latest stable version recommended)
sudo
access for module installation- Build tools (
make
,gcc
orclang
)
# Ubuntu/Debian
sudo apt install linux-headers-$(uname -r)
# Fedora/RHEL
sudo dnf install kernel-devel
# Arch Linux
sudo pacman -S linux-headers
-
Clone and build:
git clone <repo-url> cd zigko zig build
-
Install the module:
zig build install-mod
-
Check if it's loaded:
zig build status
-
View kernel messages:
zig build logs
-
Unload the module:
zig build uninstall-mod
Command | Description |
---|---|
zig build |
Compile the kernel module |
zig build install-mod |
Load the module into the kernel |
zig build uninstall-mod |
Remove the module from the kernel |
zig build status |
Check if module is currently loaded |
zig build logs |
Show recent kernel log messages |
zig build clean |
Clean all build artifacts |
zigko/
├── src/
│ └── main.zig # Main kernel module source
├── build.zig # Zig build configuration
└── README.md # This file
- Zig Compilation: The Zig code is compiled to an object file with kernel-compatible settings
- Kernel Build: A Makefile is generated and used to build the final
.ko
module - Module Loading: Standard Linux module loading mechanisms are used
The build system automatically:
- Detects your kernel version and compiler
- Sets up proper include paths
- Configures compiler flags for kernel compatibility
- Handles the two-stage build process (Zig → Make → .ko)
When the module is loaded, you should see:
[zigko] Hello World from Zig kernel module!
When unloaded:
[zigko] Goodbye from Zig kernel module!
This is a proof-of-concept with several limitations:
- Only basic kernel functions are exposed
- No comprehensive kernel API bindings
- Limited error handling
- Minimal kernel module features implemented
- The module uses
_printk
for kernel logging - Module metadata is embedded using inline assembly
- Kernel configuration is auto-detected from
/proc/version
- Both GCC and Clang kernels are supported
This is an experimental project. Feel free to:
- Report issues
- Suggest improvements
- Add more kernel API bindings
- Improve error handling