Linux kernel: Difference between revisions

From NixOS Wiki
imported>Mth
Merge content from Choose your kernel on NixOS to here.
imported>Mth
mNo edit summary
Line 5: Line 5:
For example by adding this to  <code>/etc/nixos/configuration.nix</code>:
For example by adding this to  <code>/etc/nixos/configuration.nix</code>:
<syntaxHighlight lang="nix">
<syntaxHighlight lang="nix">
  boot.kernelPackages = pkgs.linuxPackages_latest;
boot.kernelPackages = pkgs.linuxPackages_latest;
</syntaxHighlight>
</syntaxHighlight>
And rebuild your system and reboot to use your new kernel:
And rebuild your system and reboot to use your new kernel:
<syntaxHighlight lang="console">
<syntaxHighlight lang="console">
  $ sudo nixos-rebuild boot
$ sudo nixos-rebuild boot
  $ sudo reboot
$ sudo reboot
</syntaxHighlight>
</syntaxHighlight>



Revision as of 06:33, 22 April 2019

Configuration

You can choose your kernel simply by setting the boot.kernelPackages option

For example by adding this to /etc/nixos/configuration.nix:

boot.kernelPackages = pkgs.linuxPackages_latest;

And rebuild your system and reboot to use your new kernel:

$ sudo nixos-rebuild boot
$ sudo reboot

List available kernels

You can list available kernels using nix repl (previously nix-repl) :

$ nix repl
 nix-repl> :l <nixpkgs>
 Added 8557 variables.
 
 nix-repl> pkgs.linuxPackages
 pkgs.linuxPackages                           pkgs.linuxPackages_latest_hardened
 pkgs.linuxPackagesFor                        pkgs.linuxPackages_latest_xen_dom0
 pkgs.linuxPackages_4_14                      pkgs.linuxPackages_latest_xen_dom0_hardened
 pkgs.linuxPackages_4_15                      pkgs.linuxPackages_mptcp
 pkgs.linuxPackages_4_4                       pkgs.linuxPackages_rpi
 pkgs.linuxPackages_4_9                       pkgs.linuxPackages_samus_4_12
 pkgs.linuxPackages_beagleboard               pkgs.linuxPackages_samus_latest
 pkgs.linuxPackages_copperhead_hardened       pkgs.linuxPackages_testing
 pkgs.linuxPackages_custom                    pkgs.linuxPackages_testing_bcachefs
 pkgs.linuxPackages_hardened                  pkgs.linuxPackages_xen_dom0

Custom kernel modules

Note that if you deviate from the default kernel version, you should also take extra care that extra kernel modules must match the same version. The safest way to do this is to use config.boot.kernelPackages to select the correct module set:

{...}:
{
  boot.extraModulePackages = with config.boot.kernelPackages; [ wireguard ];
}

Custom configuration

It is sometimes desirable to change the configuration of your kernel, while keeping the kernel version itself managed through Nixpkgs. To do so, you can add the configuration to a dummy boot.kernelPatches,[1][2] which will then be merged and applied to the current kernel. As with kernel configuration with NixOS, drop the CONFIG_ prefix from the kernel configuration names.

This example is from the boot.crashDump.enable option:

{
      boot.kernelPatches = [ {
        name = "crashdump-config";
        patch = null;
        extraConfig = ''
                CRASH_DUMP y
                DEBUG_INFO y
                PROC_VMCORE y
                LOCKUP_DETECTOR y
                HARDLOCKUP_DETECTOR y
              '';
        } ];
}

Debugging a failed configuration

As dependencies between kernel configurations items need to be addressed manually, you can inspect the intermediate nix config file after for instance the error

note: keeping build directory '/tmp/nix-build-linux-config-4.19.0-mptcp_v0.94.1.drv-0'

by opening /tmp/nix-build-linux-config-4.19.0-mptcp_v0.94.1.drv-0/.attr-0.

Developing kernel modules

See also: NixOS Manual "Developing kernel modules"

If you work on an out-of-tree kernel module the workflow could look as follow:

#include <linux/module.h>
#define MODULE_NAME "hello"
static int __init hello_init(void)
{
    printk(KERN_INFO "hello world!");
    return 0;
}
static void __exit hello_cleanup(void) {}
module_init(hello_init);
module_exit(hello_cleanup);
$ nix-shell '<nixpkgs>' -A linux.dev
$ make -C $(nix-build -E '(import <nixpkgs> {}).linux.dev' --no-out-link)/lib/modules/*/build M=$(pwd) modules
$ insmod ./hello.ko
$ dmesg | grep hello
[   82.027229] hello world!

make menuconfig

It is (currently) not possible to run make menuconfig in the checked out linux kernel sources. This is because ncurses is not part of your working environment when you start it with nix-shell '<nixpkgs>' -A linuxPackages.kernel.

This nix-shell hack adds ncurses as a build dependency to the kernel:

$ nix-shell -E 'with import <nixpkgs> {}; linux.overrideAttrs (o: {nativeBuildInputs=o.nativeBuildInputs ++ [ pkgconfig ncurses ];})'
[nix-shell] $ unpackPhase && cd linux-*
[nix-shell] $ make menuconfig

(thanks to sphalerite)

make xconfig

Similarly to make menuconfig, you need to import qt in the environment:

$ nix-shell -E 'with import <nixpkgs> {}; linux.overrideAttrs (o: {nativeBuildInputs=o.nativeBuildInputs ++ [ pkgconfig qt5.qtbase ];})'

If the source was unpacked and an initial config exists, you can run make xconfig KCONFIG_CONFIG=build/.config

References