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