1{ config, lib, ... }:
2
3with lib;
4
5{
6 meta = {
7 maintainers = [ maintainers.joachifm ];
8 };
9
10 options = {
11 security.lockKernelModules = mkOption {
12 type = types.bool;
13 default = false;
14 description = lib.mdDoc ''
15 Disable kernel module loading once the system is fully initialised.
16 Module loading is disabled until the next reboot. Problems caused
17 by delayed module loading can be fixed by adding the module(s) in
18 question to {option}`boot.kernelModules`.
19 '';
20 };
21 };
22
23 config = mkIf config.security.lockKernelModules {
24 boot.kernelModules = concatMap (x:
25 optionals (x.device != null) (
26 if x.fsType == "vfat"
27 then [ "vfat" "nls-cp437" "nls-iso8859-1" ]
28 else [ x.fsType ])
29 ) config.system.build.fileSystems;
30
31 systemd.services.disable-kernel-module-loading = {
32 description = "Disable kernel module loading";
33
34 wants = [ "systemd-udevd.service" ];
35 wantedBy = [ config.systemd.defaultUnit ];
36
37 after =
38 [ "firewall.service"
39 "systemd-modules-load.service"
40 config.systemd.defaultUnit
41 ];
42
43 unitConfig.ConditionPathIsReadWrite = "/proc/sys/kernel";
44
45 serviceConfig =
46 { Type = "oneshot";
47 RemainAfterExit = true;
48 TimeoutSec = 180;
49 };
50
51 script = ''
52 ${config.systemd.package}/bin/udevadm settle
53 echo -n 1 >/proc/sys/kernel/modules_disabled
54 '';
55 };
56 };
57}