Skip to content

Commit e920050

Browse files
nixos/kernel: Allow controlling modules with attrsets
1 parent c80f6a7 commit e920050

File tree

5 files changed

+64
-15
lines changed

5 files changed

+64
-15
lines changed

lib/types.nix

+7
Original file line numberDiff line numberDiff line change
@@ -1162,6 +1162,13 @@ rec {
11621162
nestedTypes.finalType = finalType;
11631163
};
11641164

1165+
# A list of attrnames is coerced into an attrset of bools by
1166+
# setting the values to true.
1167+
attrNamesToTrue = coercedTo
1168+
(types.listOf types.str)
1169+
(enabled: lib.listToAttrs (map (mod: lib.nameValuePair mod true) enabled))
1170+
(types.attrsOf types.bool);
1171+
11651172
# Augment the given type with an additional type check function.
11661173
addCheck = elemType: check: elemType // { check = x: elemType.check x && check x; };
11671174

nixos/doc/manual/development/option-types.section.md

+23
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,29 @@ merging is handled.
135135
problems.
136136
:::
137137

138+
`types.attrNamesToTrue`
139+
140+
: Either a list of attribute names, or an attribute set of
141+
booleans. A list will be coerced into an attribute set with those
142+
names, whose values are set to `true`. This is useful when it is
143+
convenient to be able to write definitions as a simple list, but
144+
still need to be able to override and disable individual values.
145+
146+
::: {#ex-types-attrNamesToTrue .example}
147+
### `types.attrNamesToTrue`
148+
```
149+
{
150+
foo = [ "bar" ];
151+
}
152+
```
153+
154+
```
155+
{
156+
foo.bar = true;
157+
}
158+
```
159+
:::
160+
138161
`types.pkgs`
139162

140163
: A type for the top level Nixpkgs package set.

nixos/modules/system/boot/kernel.nix

+26-7
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,14 @@ let
88
inherit (config.boot.kernel) features randstructSeed;
99
inherit (config.boot.kernelPackages) kernel;
1010

11+
modulesTypeDesc = ''
12+
This can either be a list of modules, or an attrset. In an
13+
attrset, names that are set to `true` represent modules that will
14+
be included. Note that setting these names to `false` does not
15+
prevent the module from being loaded. For that, use
16+
{option}`boot.blacklistedKernelModules`.
17+
'';
18+
1119
kernelModulesConf = pkgs.writeText "nixos.conf"
1220
''
1321
${concatStringsSep "\n" config.boot.kernelModules}
@@ -175,20 +183,23 @@ in
175183
};
176184

177185
boot.kernelModules = mkOption {
178-
type = types.listOf types.str;
179-
default = [];
186+
type = types.attrNamesToTrue;
187+
default = { };
180188
description = ''
181189
The set of kernel modules to be loaded in the second stage of
182190
the boot process. Note that modules that are needed to
183191
mount the root file system should be added to
184192
{option}`boot.initrd.availableKernelModules` or
185193
{option}`boot.initrd.kernelModules`.
194+
195+
${modulesTypeDesc}
186196
'';
197+
apply = mods: lib.attrNames (lib.filterAttrs (_: v: v) mods);
187198
};
188199

189200
boot.initrd.availableKernelModules = mkOption {
190-
type = types.listOf types.str;
191-
default = [];
201+
type = types.attrNamesToTrue;
202+
default = { };
192203
example = [ "sata_nv" "ext3" ];
193204
description = ''
194205
The set of kernel modules in the initial ramdisk used during the
@@ -204,13 +215,21 @@ in
204215
modules for PCI devices are loaded when they match the PCI ID
205216
of a device in your system). To force a module to be loaded,
206217
include it in {option}`boot.initrd.kernelModules`.
218+
219+
${modulesTypeDesc}
207220
'';
221+
apply = mods: lib.attrNames (lib.filterAttrs (_: v: v) mods);
208222
};
209223

210224
boot.initrd.kernelModules = mkOption {
211-
type = types.listOf types.str;
212-
default = [];
213-
description = "List of modules that are always loaded by the initrd.";
225+
type = types.attrNamesToTrue;
226+
default = { };
227+
description = ''
228+
Set of modules that are always loaded by the initrd.
229+
230+
${modulesTypeDesc}
231+
'';
232+
apply = mods: lib.attrNames (lib.filterAttrs (_: v: v) mods);
214233
};
215234

216235
boot.initrd.includeDefaultModules = mkOption {

nixos/modules/system/boot/modprobe.nix

+7-4
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,19 @@ with lib;
2525
};
2626

2727
boot.blacklistedKernelModules = mkOption {
28-
type = types.listOf types.str;
29-
default = [ ];
28+
type = types.attrNamesToTrue;
29+
default = { };
3030
example = [
3131
"cirrusfb"
3232
"i2c_piix4"
3333
];
3434
description = ''
35-
List of names of kernel modules that should not be loaded
36-
automatically by the hardware probing code.
35+
Set of names of kernel modules that should not be loaded
36+
automatically by the hardware probing code. This can either be
37+
a list of modules or an attrset. In an attrset, names that are
38+
set to `true` represent modules that will be blacklisted.
3739
'';
40+
apply = mods: lib.attrNames (lib.filterAttrs (_: v: v) mods);
3841
};
3942

4043
boot.extraModprobeConfig = mkOption {

nixos/modules/tasks/filesystems.nix

+1-4
Original file line numberDiff line numberDiff line change
@@ -273,10 +273,7 @@ in
273273
zfs = lib.mkForce false;
274274
}
275275
'';
276-
type = types.coercedTo
277-
(types.listOf types.str)
278-
(enabled: lib.listToAttrs (map (fs: lib.nameValuePair fs true) enabled))
279-
(types.attrsOf types.bool);
276+
type = types.attrNamesToTrue;
280277
description = ''
281278
Names of supported filesystem types, or an attribute set of file system types
282279
and their state. The set form may be used together with `lib.mkForce` to

0 commit comments

Comments
 (0)