Skip to content

Commit 2c1ec14

Browse files
A-jay98mightyiam
andcommitted
lib/modules: extract isDisabled from filterModules
Co-authored-by: Shahar "Dawn" Or <[email protected]>
1 parent ff774a4 commit 2c1ec14

File tree

1 file changed

+31
-22
lines changed

1 file changed

+31
-22
lines changed

lib/modules.nix

Lines changed: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -424,8 +424,37 @@ let
424424
else
425425
m: m;
426426

427+
# isDisabled :: String -> [ { disabled, file } ] -> StructuredModule -> bool
428+
#
429+
# Figures out whether a `StructuredModule` is disabled.
430+
isDisabled =
431+
modulesPath: disabledList:
432+
let
433+
moduleKey =
434+
file: m:
435+
if isString m then
436+
if substring 0 1 m == "/" then m else toString modulesPath + "/" + m
437+
438+
else if isConvertibleWithToString m then
439+
if m ? key && m.key != toString m then
440+
throw "Module `${file}` contains a disabledModules item that is an attribute set that can be converted to a string (${toString m}) but also has a `.key` attribute (${m.key}) with a different value. This makes it ambiguous which module should be disabled."
441+
else
442+
toString m
443+
444+
else if m ? key then
445+
m.key
446+
447+
else if isAttrs m then
448+
throw "Module `${file}` contains a disabledModules item that is an attribute set, presumably a module, that does not have a `key` attribute. This means that the module system doesn't have any means to identify the module that should be disabled. Make sure that you've put the correct value in disabledModules: a string path relative to modulesPath, a path value, or an attribute set with a `key` attribute."
449+
else
450+
throw "Each disabledModules item must be a path, string, or a attribute set with a key attribute, or a value supported by toString. However, one of the disabledModules items in `${toString file}` is none of that, but is of type ${typeOf m}.";
451+
452+
disabledKeys = concatMap ({ file, disabled }: map (moduleKey file) disabled) disabledList;
453+
in
454+
structuredModule: elem structuredModule.key disabledKeys;
455+
427456
/**
428-
Collects all modules recursively into the form
457+
Collects all modules recursively into a `[ StructuredModule ]` and a list of disabled modules:
429458
430459
{
431460
disabled = [ <list of disabled modules> ];
@@ -493,27 +522,7 @@ let
493522
modulesPath:
494523
{ disabled, modules }:
495524
let
496-
moduleKey =
497-
file: m:
498-
if isString m then
499-
if substring 0 1 m == "/" then m else toString modulesPath + "/" + m
500-
501-
else if isConvertibleWithToString m then
502-
if m ? key && m.key != toString m then
503-
throw "Module `${file}` contains a disabledModules item that is an attribute set that can be converted to a string (${toString m}) but also has a `.key` attribute (${m.key}) with a different value. This makes it ambiguous which module should be disabled."
504-
else
505-
toString m
506-
507-
else if m ? key then
508-
m.key
509-
510-
else if isAttrs m then
511-
throw "Module `${file}` contains a disabledModules item that is an attribute set, presumably a module, that does not have a `key` attribute. This means that the module system doesn't have any means to identify the module that should be disabled. Make sure that you've put the correct value in disabledModules: a string path relative to modulesPath, a path value, or an attribute set with a `key` attribute."
512-
else
513-
throw "Each disabledModules item must be a path, string, or a attribute set with a key attribute, or a value supported by toString. However, one of the disabledModules items in `${toString file}` is none of that, but is of type ${typeOf m}.";
514-
515-
disabledKeys = concatMap ({ file, disabled }: map (moduleKey file) disabled) disabled;
516-
keyFilter = filter (attrs: !elem attrs.key disabledKeys);
525+
keyFilter = filter (attrs: !isDisabled modulesPath disabled attrs);
517526
in
518527
map (attrs: attrs.module) (genericClosure {
519528
startSet = keyFilter modules;

0 commit comments

Comments
 (0)