Last active
January 12, 2025 11:11
-
-
Save hacker1024/0d4475a34868edc2def1785d9e2d09ca to your computer and use it in GitHub Desktop.
A Nix shell to aid in kernel development with QEMU. Allows building a kernel and then launching a NixOS VM using it.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Usage: | |
# | |
# $ genconfig | |
# $ launch | |
# | |
# In QEMU, press CTRL+X, h for help. | |
{ pkgs ? import <nixpkgs> { } }: | |
let | |
vmSystem = pkgs.nixos ({ modulesPath, config, ... }: { | |
imports = [ (modulesPath + "/profiles/minimal.nix") ]; | |
system.name = "kernelvm"; | |
virtualisation.vmVariant.virtualisation = { | |
memorySize = 512; | |
cores = 4; | |
diskImage = null; | |
graphics = false; | |
}; | |
boot = { | |
kernelParams = [ "earlyprintk=serial" ]; | |
consoleLogLevel = 8; | |
}; | |
networking.firewall.enable = false; | |
services.getty.autologinUser = config.users.users.root.name; | |
}); | |
genConfig = '' | |
make defconfig kvm_guest.config | |
# Required NixOS features | |
./scripts/config \ | |
--enable OVERLAY_FS | |
# Large unnecessary features | |
./scripts/config \ | |
--disable DRM_I915 | |
make olddefconfig | |
''; | |
in | |
with vmSystem.pkgs; | |
mkShell { | |
inputsFrom = [ vmSystem.config.boot.kernelPackages.kernel ]; | |
packages = [ | |
pkg-config | |
ncurses | |
qt6.qtbase | |
qt6.qtwayland | |
(writeShellApplication { | |
name = "genconfig"; | |
text = genConfig; | |
}) | |
(writeShellApplication { | |
name = "launch"; | |
text = '' | |
make -j"$(nproc)" '${vmSystem.pkgs.hostPlatform.linux-kernel.target}' | |
NIXPKGS_QEMU_KERNEL_${vmSystem.config.system.name}="$PWD/arch/${vmSystem.pkgs.hostPlatform.linuxArch}/boot/${vmSystem.pkgs.hostPlatform.linux-kernel.target}" \ | |
'${pkgs.lib.getExe vmSystem.vm}' | |
''; | |
}) | |
]; | |
shellHook = '' | |
# Reproducability (helps speed up rebuilds) | |
export KBUILD_BUILD_TIMESTAMP="$(date -u -d @$SOURCE_DATE_EPOCH)" | |
export KBUILD_BUILD_VERSION=1-NixOS | |
export KBUILD_BUILD_USER=user | |
export KBUILD_BUILD_HOST=machine | |
''; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment