1# Linux Kernel {#sec-kernel-config}
2
3You can override the Linux kernel and associated packages using the
4option `boot.kernelPackages`. For instance, this selects the Linux 3.10
5kernel:
6
7```nix
8boot.kernelPackages = pkgs.linuxKernel.packages.linux_3_10;
9```
10
11Note that this not only replaces the kernel, but also packages that are
12specific to the kernel version, such as the NVIDIA video drivers. This
13ensures that driver packages are consistent with the kernel.
14
15While `pkgs.linuxKernel.packages` contains all available kernel packages,
16you may want to use one of the unversioned `pkgs.linuxPackages_*` aliases
17such as `pkgs.linuxPackages_latest`, that are kept up to date with new
18versions.
19
20The default Linux kernel configuration should be fine for most users.
21You can see the configuration of your current kernel with the following
22command:
23
24```ShellSession
25zcat /proc/config.gz
26```
27
28If you want to change the kernel configuration, you can use the
29`packageOverrides` feature (see [](#sec-customising-packages)). For
30instance, to enable support for the kernel debugger KGDB:
31
32```nix
33nixpkgs.config.packageOverrides = pkgs: pkgs.lib.recursiveUpdate pkgs {
34 linuxKernel.kernels.linux_5_10 = pkgs.linuxKernel.kernels.linux_5_10.override {
35 extraConfig = ''
36 KGDB y
37 '';
38 };
39};
40```
41
42`extraConfig` takes a list of Linux kernel configuration options, one
43per line. The name of the option should not include the prefix
44`CONFIG_`. The option value is typically `y`, `n` or `m` (to build
45something as a kernel module).
46
47Kernel modules for hardware devices are generally loaded automatically
48by `udev`. You can force a module to be loaded via
49[](#opt-boot.kernelModules), e.g.
50
51```nix
52boot.kernelModules = [ "fuse" "kvm-intel" "coretemp" ];
53```
54
55If the module is required early during the boot (e.g. to mount the root
56file system), you can use [](#opt-boot.initrd.kernelModules):
57
58```nix
59boot.initrd.kernelModules = [ "cifs" ];
60```
61
62This causes the specified modules and their dependencies to be added to
63the initial ramdisk.
64
65Kernel runtime parameters can be set through
66[](#opt-boot.kernel.sysctl), e.g.
67
68```nix
69boot.kernel.sysctl."net.ipv4.tcp_keepalive_time" = 120;
70```
71
72sets the kernel's TCP keepalive time to 120 seconds. To see the
73available parameters, run `sysctl -a`.
74
75## Customize your kernel {#sec-linux-config-customizing}
76
77The first step before compiling the kernel is to generate an appropriate
78`.config` configuration. Either you pass your own config via the
79`configfile` setting of `linuxKernel.manualConfig`:
80
81```nix
82custom-kernel = let base_kernel = linuxKernel.kernels.linux_4_9;
83 in super.linuxKernel.manualConfig {
84 inherit (super) stdenv hostPlatform;
85 inherit (base_kernel) src;
86 version = "${base_kernel.version}-custom";
87
88 configfile = /home/me/my_kernel_config;
89 allowImportFromDerivation = true;
90};
91```
92
93You can edit the config with this snippet (by default `make
94 menuconfig` won\'t work out of the box on nixos):
95
96```ShellSession
97nix-shell -E 'with import <nixpkgs> {}; kernelToOverride.overrideAttrs (o: {nativeBuildInputs=o.nativeBuildInputs ++ [ pkg-config ncurses ];})'
98```
99
100or you can let nixpkgs generate the configuration. Nixpkgs generates it
101via answering the interactive kernel utility `make config`. The answers
102depend on parameters passed to
103`pkgs/os-specific/linux/kernel/generic.nix` (which you can influence by
104overriding `extraConfig, autoModules,
105 modDirVersion, preferBuiltin, extraConfig`).
106
107```nix
108mptcp93.override ({
109 name="mptcp-local";
110
111 ignoreConfigErrors = true;
112 autoModules = false;
113 kernelPreferBuiltin = true;
114
115 enableParallelBuilding = true;
116
117 extraConfig = ''
118 DEBUG_KERNEL y
119 FRAME_POINTER y
120 KGDB y
121 KGDB_SERIAL_CONSOLE y
122 DEBUG_INFO y
123 '';
124});
125```
126
127## Developing kernel modules {#sec-linux-config-developing-modules}
128
129When developing kernel modules it\'s often convenient to run
130edit-compile-run loop as quickly as possible. See below snippet as an
131example of developing `mellanox` drivers.
132
133```ShellSession
134$ nix-build '<nixpkgs>' -A linuxPackages.kernel.dev
135$ nix-shell '<nixpkgs>' -A linuxPackages.kernel
136$ unpackPhase
137$ cd linux-*
138$ make -C $dev/lib/modules/*/build M=$(pwd)/drivers/net/ethernet/mellanox modules
139# insmod ./drivers/net/ethernet/mellanox/mlx5/core/mlx5_core.ko
140```