From 2c1ec14d126c03d2555d1680ca67c82aa64ebd7b Mon Sep 17 00:00:00 2001 From: Ali Jamadi Date: Sat, 14 Jun 2025 15:04:49 +0700 Subject: [PATCH] lib/modules: extract isDisabled from filterModules Co-authored-by: Shahar "Dawn" Or --- lib/modules.nix | 53 +++++++++++++++++++++++++++++-------------------- 1 file changed, 31 insertions(+), 22 deletions(-) diff --git a/lib/modules.nix b/lib/modules.nix index c55d276d4970..76e6de892e82 100644 --- a/lib/modules.nix +++ b/lib/modules.nix @@ -424,8 +424,37 @@ let else m: m; + # isDisabled :: String -> [ { disabled, file } ] -> StructuredModule -> bool + # + # Figures out whether a `StructuredModule` is disabled. + isDisabled = + modulesPath: disabledList: + let + moduleKey = + file: m: + if isString m then + if substring 0 1 m == "/" then m else toString modulesPath + "/" + m + + else if isConvertibleWithToString m then + if m ? key && m.key != toString m then + 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." + else + toString m + + else if m ? key then + m.key + + else if isAttrs m then + 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." + else + 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}."; + + disabledKeys = concatMap ({ file, disabled }: map (moduleKey file) disabled) disabledList; + in + structuredModule: elem structuredModule.key disabledKeys; + /** - Collects all modules recursively into the form + Collects all modules recursively into a `[ StructuredModule ]` and a list of disabled modules: { disabled = [ ]; @@ -493,27 +522,7 @@ let modulesPath: { disabled, modules }: let - moduleKey = - file: m: - if isString m then - if substring 0 1 m == "/" then m else toString modulesPath + "/" + m - - else if isConvertibleWithToString m then - if m ? key && m.key != toString m then - 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." - else - toString m - - else if m ? key then - m.key - - else if isAttrs m then - 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." - else - 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}."; - - disabledKeys = concatMap ({ file, disabled }: map (moduleKey file) disabled) disabled; - keyFilter = filter (attrs: !elem attrs.key disabledKeys); + keyFilter = filter (attrs: !isDisabled modulesPath disabled attrs); in map (attrs: attrs.module) (genericClosure { startSet = keyFilter modules;