Getting EFI Stub Included in NixOS: Linux Kernel Build Guide
In this article, we will cover the process of building a custom Linux kernel with EFI stub support in NixOS. This guide is intended for users who are familiar with NixOS and want to learn how to build a custom kernel with specific configurations. We will cover the key concepts, subtitles, and detailed context of the topic. By the end of this guide, you will have a custom Linux kernel with EFI stub support built and ready to use.
Prerequisites
Before we begin, it is assumed that you have a working NixOS installation and are familiar with the Nix package manager. You should also have a basic understanding of the Linux kernel and its configuration options. It is recommended that you have a separate partition or virtual machine to test the custom kernel.
Introduction to EFI Stub
EFI (Extensible Firmware Interface) is a modern firmware interface that replaces the traditional BIOS. EFI stub is a feature of the Linux kernel that allows it to be loaded directly by the EFI firmware, without the need for a separate bootloader such as GRUB. This can simplify the boot process and improve boot times.
Building the Custom Kernel
To build a custom kernel with EFI stub support in NixOS, we will use the nix-build command with a custom Nix expression. The Nix expression will define the kernel configuration, build options, and package dependencies.
Here is an example Nix expression for building a custom kernel with EFI stub support:
{pkgs, lib, config, ...}:
let
llvm = pkgs.llvm_18clang_18lld_18;
in
{
environment.systemPackages = pkgs;
boot.kernelPackages = let
kernelSrc = pkgs.linuxPackages_latest_5_15.src;
kernelConfig = pkgs.writeText "custom-kernel-config" ''
# Add your custom kernel configuration options here
CONFIG_EFI_STUB=y
''
in
pkgs.buildPackages.stdenv.mkDerivation rec {
name = "custom-kernel-${version}";
version = "5.15";
src = kernelSrc;
buildInputs = [
llvm
pkgs.gcc
pkgs.make
pkgs.flex
pkgs.bison
];
configurePhase = ''
cp ${kernelConfig} .config
make oldconfig
'';
buildPhase = ''
make
'';
installPhase = ''
mkdir -p $out
cp arch/x86/boot/bzImage $out
'';
};
}In this example, we define the boot.kernelPackages attribute to build a custom kernel. We use the pkgs.linuxPackages_latest_5_15.src to get the source code of the latest 5.15 kernel. We then define a custom kernel configuration in the kernelConfig variable. In this example, we enable the EFI stub feature by setting the CONFIG_EFI_STUB option to y.
We then define the build and install phases to build the kernel and install it to the $out directory. We also include the necessary build dependencies such as the LLVM compiler, GCC, make, flex, and bison.
Testing the Custom Kernel
To test the custom kernel, we need to update the bootloader configuration to boot from the new kernel. In NixOS, we can use the systemd-boot bootloader, which is simple and easy to configure.
Here is an example configuration.nix file that boots from the custom kernel:
{ config, pkgs, ... }:
let
customKernel = pkgs.buildPackages.stdenv.mkDerivation rec {
name = "custom-kernel-${version}";
version = "5.15";
src = pkgs.linuxPackages_latest_5_15.src;
buildInputs = [
pkgs.gcc
pkgs.make
pkgs.flex
pkgs.bison
];
configurePhase = ''
cp ${pkgs.writeText "custom-kernel-config" ''
# Add your custom kernel configuration options here
CONFIG_EFI_STUB=y
''} .config
make oldconfig
'';
buildPhase = ''
make
'';
installPhase = ''
mkdir -p $out
cp arch/x86/boot/bzImage $out
'';
};
in
{
environment = {
systemPackages = [
customKernel
];
};
boot.loader.systemd-boot.extraEntries = ''
entry custom-kernel {
description="Custom Kernel 5.15"
linux=${customKernel}/bzImage
initrd=""
options="root=PARTUUID= rw"
}
'';
} In this example, we define the customKernel derivation to build the custom kernel. We then define an entry in the boot.loader.systemd-boot.extraEntries attribute to boot from the custom kernel. We set the linux option to the path of the custom kernel and the options option to the kernel command line options.
After updating the configuration.nix file, we can run the nixos-rebuild switch command to apply the changes and reboot into the custom kernel.
- EFI stub is a feature of the Linux kernel that allows it to be loaded directly by the EFI firmware, without the need for a separate bootloader.
- To build a custom kernel with EFI stub support in NixOS, we can use a custom Nix expression to define the kernel configuration, build options, and package dependencies.
- We can test the custom kernel by updating the bootloader configuration to boot from the new kernel. In NixOS, we can use the
systemd-bootbootloader, which is simple and easy to configure.