Building a Linux Kernel on NixOS

Posted on March 30, 2026
Tags: ,

Table of Contents

  1. Introduction
  2. Get the kernel sources
  3. Prepare a reproducible build shell
  4. Configure and build the kernel
  5. Wrap the kernel as a Nix package
  6. Configure NixOS to use the custom kernel
  7. Test and rollback
  8. Short tips and safety notes

Introduction

Compiling a Linux kernel on NixOS follows the familiar configure/build steps, but the integration step should use Nix so the kernel lives in the Nix store and is managed by nixos-rebuild. This short guide keeps to the essentials: fetch sources, build (using gcc), wrap as a Nix package, and activate with nixos-rebuild.

Get the kernel sources

git clone https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git
cd linux
git checkout v5.x.y    # pick the tag/commit you need

Prepare a reproducible build shell

Enter a nix-shell from the kernel tree (or your development shell that contains required tools). The examples below assume you prefer gcc over clang.

# from the kernel source dir
nix-shell .

If you want to ensure the build uses system gcc (and not clang/LLVM), start a clean env:

env -u CC -u LD -u LLVM -u LLVM_IAS bash
which gcc
gcc --version

Configure and build the kernel

Use an out-of-tree build to keep the source tree clean and reuse /proc/config.gz as a starting point.

# cleanup and prepare
make O=build mrproper
mkdir -p build
zcat /proc/config.gz > build/.config
make O=build olddefconfig

# build with parallel jobs
make O=build -j$(nproc)

This produces the kernel image and modules under build/. You can test by loading the vmlinuz from the build tree (VM) or proceed to wrap the kernel as a Nix package.

Wrap the kernel as a Nix package

Create a small override that points kernelPackages at your source/commit. Example (conceptual):

{ pkgs, lib, stdenv }:

pkgs.linuxPackages.overrideAttrs (old: {
  src = pkgs.fetchFromGitHub {
    owner = "torvalds";
    repo  = "linux";
    rev   = "v5.x.y";
    sha256 = "<hash>";
  };
  # optional: add patches or config fragments via postPatch / configureFlags
})

Save this as e.g. /path/to/my-kernel.nix.

Configure NixOS to use the custom kernel

In /etc/nixos/configuration.nix point boot.kernelPackages to your package:

{ config, pkgs, ... }:
{
  boot.kernelPackages = pkgs.callPackage /path/to/my-kernel.nix { };
}

Then build and switch:

# without flakes
sudo nixos-rebuild switch

# with flakes (example)
sudo nixos-rebuild switch --flake .#yourSystem

nixos-rebuild will build the kernel in the Nix store and update boot entries.

Test and rollback

  • Reboot and select the new kernel.

  • If it fails, choose an older generation from the boot menu or run:

    sudo nixos-rebuild switch –rollback

Short tips and safety notes

  • Do not use sudo make install on a running NixOS system — it bypasses the Nix store and system configuration.
  • For quick tests use a VM or boot the vmlinuz directly (non-Nix bootloader).
  • Prefer flakes and pinned nixpkgs for reproducible builds.
  • Keep a known-good configuration.nix before experimenting.

Wrap-up: fetch sources, build reproducibly (use nix-shell + out-of-tree build), package the kernel as a Nix package, set boot.kernelPackages, then nixos-rebuild switch and reboot. This keeps kernels managed, reproducible, and rollbackable under Nix.