1{ config, lib, pkgs, ... }:
2
3with lib;
4
5let
6
7 inherit (config.boot) kernelPatches;
8
9 inherit (config.boot.kernelPackages) kernel;
10
11 kernelModulesConf = pkgs.writeText "nixos.conf"
12 ''
13 ${concatStringsSep "\n" config.boot.kernelModules}
14 '';
15
16in
17
18{
19
20 ###### interface
21
22 options = {
23
24 boot.kernelPackages = mkOption {
25 default = pkgs.linuxPackages;
26 apply = kernelPackages: kernelPackages.extend (self: super: {
27 kernel = super.kernel.override {
28 kernelPatches = super.kernel.kernelPatches ++ kernelPatches;
29 };
30 });
31 # We don't want to evaluate all of linuxPackages for the manual
32 # - some of it might not even evaluate correctly.
33 defaultText = "pkgs.linuxPackages";
34 example = literalExample "pkgs.linuxPackages_2_6_25";
35 description = ''
36 This option allows you to override the Linux kernel used by
37 NixOS. Since things like external kernel module packages are
38 tied to the kernel you're using, it also overrides those.
39 This option is a function that takes Nixpkgs as an argument
40 (as a convenience), and returns an attribute set containing at
41 the very least an attribute <varname>kernel</varname>.
42 Additional attributes may be needed depending on your
43 configuration. For instance, if you use the NVIDIA X driver,
44 then it also needs to contain an attribute
45 <varname>nvidia_x11</varname>.
46 '';
47 };
48
49 boot.kernelPatches = mkOption {
50 type = types.listOf types.attrs;
51 default = [];
52 example = literalExample "[ pkgs.kernelPatches.ubuntu_fan_4_4 ]";
53 description = "A list of additional patches to apply to the kernel.";
54 };
55
56 boot.kernelParams = mkOption {
57 type = types.listOf types.str;
58 default = [ ];
59 description = "Parameters added to the kernel command line.";
60 };
61
62 boot.consoleLogLevel = mkOption {
63 type = types.int;
64 default = 4;
65 description = ''
66 The kernel console log level. Log messages with a priority
67 numerically less than this will not appear on the console.
68 '';
69 };
70
71 boot.vesa = mkOption {
72 type = types.bool;
73 default = false;
74 description = ''
75 Whether to activate VESA video mode on boot.
76 '';
77 };
78
79 boot.extraModulePackages = mkOption {
80 type = types.listOf types.package;
81 default = [];
82 example = literalExample "[ pkgs.linuxPackages.nvidia_x11 ]";
83 description = "A list of additional packages supplying kernel modules.";
84 };
85
86 boot.kernelModules = mkOption {
87 type = types.listOf types.str;
88 default = [];
89 description = ''
90 The set of kernel modules to be loaded in the second stage of
91 the boot process. Note that modules that are needed to
92 mount the root file system should be added to
93 <option>boot.initrd.availableKernelModules</option> or
94 <option>boot.initrd.kernelModules</option>.
95 '';
96 };
97
98 boot.initrd.availableKernelModules = mkOption {
99 type = types.listOf types.str;
100 default = [];
101 example = [ "sata_nv" "ext3" ];
102 description = ''
103 The set of kernel modules in the initial ramdisk used during the
104 boot process. This set must include all modules necessary for
105 mounting the root device. That is, it should include modules
106 for the physical device (e.g., SCSI drivers) and for the file
107 system (e.g., ext3). The set specified here is automatically
108 closed under the module dependency relation, i.e., all
109 dependencies of the modules list here are included
110 automatically. The modules listed here are available in the
111 initrd, but are only loaded on demand (e.g., the ext3 module is
112 loaded automatically when an ext3 filesystem is mounted, and
113 modules for PCI devices are loaded when they match the PCI ID
114 of a device in your system). To force a module to be loaded,
115 include it in <option>boot.initrd.kernelModules</option>.
116 '';
117 };
118
119 boot.initrd.kernelModules = mkOption {
120 type = types.listOf types.str;
121 default = [];
122 description = "List of modules that are always loaded by the initrd.";
123 };
124
125 system.modulesTree = mkOption {
126 type = types.listOf types.path;
127 internal = true;
128 default = [];
129 description = ''
130 Tree of kernel modules. This includes the kernel, plus modules
131 built outside of the kernel. Combine these into a single tree of
132 symlinks because modprobe only supports one directory.
133 '';
134 # Convert the list of path to only one path.
135 apply = pkgs.aggregateModules;
136 };
137
138 system.requiredKernelConfig = mkOption {
139 default = [];
140 example = literalExample ''
141 with config.lib.kernelConfig; [
142 (isYes "MODULES")
143 (isEnabled "FB_CON_DECOR")
144 (isEnabled "BLK_DEV_INITRD")
145 ]
146 '';
147 internal = true;
148 type = types.listOf types.attrs;
149 description = ''
150 This option allows modules to specify the kernel config options that
151 must be set (or unset) for the module to work. Please use the
152 lib.kernelConfig functions to build list elements.
153 '';
154 };
155
156 };
157
158
159 ###### implementation
160
161 config = mkIf (!config.boot.isContainer) {
162
163 system.build = { inherit kernel; };
164
165 system.modulesTree = [ kernel ] ++ config.boot.extraModulePackages;
166
167 # Implement consoleLogLevel both in early boot and using sysctl
168 # (so you don't need to reboot to have changes take effect).
169 boot.kernelParams =
170 [ "loglevel=${toString config.boot.consoleLogLevel}" ] ++
171 optionals config.boot.vesa [ "vga=0x317" ];
172
173 boot.kernel.sysctl."kernel.printk" = config.boot.consoleLogLevel;
174
175 boot.kernelModules = [ "loop" "atkbd" ];
176
177 boot.initrd.availableKernelModules =
178 [ # Note: most of these (especially the SATA/PATA modules)
179 # shouldn't be included by default since nixos-generate-config
180 # detects them, but I'm keeping them for now for backwards
181 # compatibility.
182
183 # Some SATA/PATA stuff.
184 "ahci"
185 "sata_nv"
186 "sata_via"
187 "sata_sis"
188 "sata_uli"
189 "ata_piix"
190 "pata_marvell"
191
192 # Standard SCSI stuff.
193 "sd_mod"
194 "sr_mod"
195
196 # Standard IDE stuff.
197 "ide_cd"
198 "ide_disk"
199 "ide_generic"
200
201 # SD cards and internal eMMC drives.
202 "mmc_block"
203
204 # Support USB keyboards, in case the boot fails and we only have
205 # a USB keyboard.
206 "uhci_hcd"
207 "ehci_hcd"
208 "ehci_pci"
209 "ohci_hcd"
210 "ohci_pci"
211 "xhci_hcd"
212 "xhci_pci"
213 "usbhid"
214 "hid_generic" "hid_lenovo"
215 "hid_apple" "hid_logitech_dj" "hid_lenovo_tpkbd" "hid_roccat"
216
217 # Misc. keyboard stuff.
218 "pcips2" "atkbd" "i8042"
219
220 # Temporary fix for https://github.com/NixOS/nixpkgs/issues/18451
221 # Remove as soon as upstream gets fixed - marking it:
222 # TODO
223 # FIXME
224 "i8042"
225
226 # To wait for SCSI devices to appear.
227 "scsi_wait_scan"
228
229 # Needed by the stage 2 init script.
230 "rtc_cmos"
231 ];
232
233 boot.initrd.kernelModules =
234 [ # For LVM.
235 "dm_mod"
236 ];
237
238 # The Linux kernel >= 2.6.27 provides firmware.
239 hardware.firmware = [ kernel ];
240
241 # Create /etc/modules-load.d/nixos.conf, which is read by
242 # systemd-modules-load.service to load required kernel modules.
243 environment.etc = singleton
244 { target = "modules-load.d/nixos.conf";
245 source = kernelModulesConf;
246 };
247
248 systemd.services."systemd-modules-load" =
249 { wantedBy = [ "multi-user.target" ];
250 restartTriggers = [ kernelModulesConf ];
251 serviceConfig =
252 { # Ignore failed module loads. Typically some of the
253 # modules in ‘boot.kernelModules’ are "nice to have but
254 # not required" (e.g. acpi-cpufreq), so we don't want to
255 # barf on those.
256 SuccessExitStatus = "0 1";
257 };
258 };
259
260 lib.kernelConfig = {
261 isYes = option: {
262 assertion = config: config.isYes option;
263 message = "CONFIG_${option} is not yes!";
264 configLine = "CONFIG_${option}=y";
265 };
266
267 isNo = option: {
268 assertion = config: config.isNo option;
269 message = "CONFIG_${option} is not no!";
270 configLine = "CONFIG_${option}=n";
271 };
272
273 isModule = option: {
274 assertion = config: config.isModule option;
275 message = "CONFIG_${option} is not built as a module!";
276 configLine = "CONFIG_${option}=m";
277 };
278
279 ### Usually you will just want to use these two
280 # True if yes or module
281 isEnabled = option: {
282 assertion = config: config.isEnabled option;
283 message = "CONFIG_${option} is not enabled!";
284 configLine = "CONFIG_${option}=y";
285 };
286
287 # True if no or omitted
288 isDisabled = option: {
289 assertion = config: config.isDisabled option;
290 message = "CONFIG_${option} is not disabled!";
291 configLine = "CONFIG_${option}=n";
292 };
293 };
294
295 # The config options that all modules can depend upon
296 system.requiredKernelConfig = with config.lib.kernelConfig; [
297 # !!! Should this really be needed?
298 (isYes "MODULES")
299 (isYes "BINFMT_ELF")
300 ];
301
302 # nixpkgs kernels are assumed to have all required features
303 assertions = if config.boot.kernelPackages.kernel ? features then [] else
304 let cfg = config.boot.kernelPackages.kernel.config; in map (attrs:
305 { assertion = attrs.assertion cfg; inherit (attrs) message; }
306 ) config.system.requiredKernelConfig;
307
308 };
309
310}