Linux kernel: Difference between revisions

imported>Mth
m Header capitalization
imported>Mth
Merge content from Choose your kernel on NixOS to here.
Line 1: Line 1:
== Configuring the linux kernel ==
== Configuration ==
See:


* [https://nixos.org/nixos/manual/index.html#sec-kernel-config NixOS Manual "Linux Kernel"].
You can choose your kernel simply by setting the <code>boot.kernelPackages</code> option
* [[Choose your kernel on NixOS]]
 
For example by adding this to  <code>/etc/nixos/configuration.nix</code>:
<syntaxHighlight lang="nix">
  boot.kernelPackages = pkgs.linuxPackages_latest;
</syntaxHighlight>
And rebuild your system and reboot to use your new kernel:
<syntaxHighlight lang="console">
  $ sudo nixos-rebuild boot
  $ sudo reboot
</syntaxHighlight>
 
=== List available kernels ===
You can list available kernels using  <code>nix repl</code> (previously <code>nix-repl</code>) :
 
<syntaxHighlight lang="console">
$ nix repl
</syntaxHighlight>
 
  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 <code>config.boot.kernelPackages</code> to select the correct module set:
 
<syntaxHighlight lang="nix">
{...}:
{
  boot.extraModulePackages = with config.boot.kernelPackages; [ wireguard ];
}
</syntaxHighlight>
 
=== 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 {{nixos:option|boot.kernelPatches}},<ref>[https://logs.nix.samueldr.com/nixos/2018-05-09#1525898458-1525898534; #nixos 2018-05-09]</ref><ref>[https://github.com/NixOS/nixpkgs/blob/03d4694e6110fa9c16e88ee74085ea2068c27494/nixos/modules/misc/crashdump.nix#L63-L73 <tt>nixos/modules/misc/crashdump.nix#L63-L73</tt>]</ref> which will then be merged and applied to the current kernel. As with kernel configuration with NixOS, drop the <tt>CONFIG_</tt> prefix from the kernel configuration names.
 
This example is from the {{nixos:option|boot.crashDump.enable}} option:
 
<syntaxhighlight lang=nix>
{
      boot.kernelPatches = [ {
        name = "crashdump-config";
        patch = null;
        extraConfig = ''
                CRASH_DUMP y
                DEBUG_INFO y
                PROC_VMCORE y
                LOCKUP_DETECTOR y
                HARDLOCKUP_DETECTOR y
              '';
        } ];
}
</syntaxhighlight>
 
=== 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
{{bc|note: keeping build directory '/tmp/nix-build-linux-config-4.19.0-mptcp_v0.94.1.drv-0'}}
by opening {{ic|/tmp/nix-build-linux-config-4.19.0-mptcp_v0.94.1.drv-0/.attr-0}}.


== Developing kernel modules ==
== Developing kernel modules ==
Line 49: Line 118:
If the source was unpacked and an initial config exists, you can run <code>make xconfig KCONFIG_CONFIG=build/.config</code>
If the source was unpacked and an initial config exists, you can run <code>make xconfig KCONFIG_CONFIG=build/.config</code>


== Adding <code>extraConfig</code> ==
== References ==
 
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 {{nixos:option|boot.kernelPatches}}<ref>[https://logs.nix.samueldr.com/nixos/2018-05-09#1525898458-1525898534; #nixos 2018-05-09]</ref><ref>[https://github.com/NixOS/nixpkgs/blob/03d4694e6110fa9c16e88ee74085ea2068c27494/nixos/modules/misc/crashdump.nix#L63-L73 <tt>nixos/modules/misc/crashdump.nix#L63-L73</tt>]</ref>, which will then be merged and applied to the current kernel. As with kernel configuration with NixOS, drop the <tt>CONFIG_</tt> from the kernel configuration names.
 
This example is from the {{nixos:option|boot.crashDump.enable}} option:
 
<syntaxhighlight lang=nix>
{
      boot.kernelPatches = [ {
        name = "crashdump-config";
        patch = null;
        extraConfig = ''
                CRASH_DUMP y
                DEBUG_INFO y
                PROC_VMCORE y
                LOCKUP_DETECTOR y
                HARDLOCKUP_DETECTOR y
              '';
        } ];
}
</syntaxhighlight>
 
== Debug 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
<references />
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 .