From e2cc36197053c087b16009750fd3e60029600e25 Mon Sep 17 00:00:00 2001 From: Erik Arvstedt Date: Thu, 25 Aug 2022 14:03:57 +0200 Subject: [PATCH] lib.modules: support strings with absolute paths in `disabledModules` This is particularly useful for disabling modules defined in a flake. Example: disabledModules = [ "${flake}/modules/mymodule.nix" ]; Previously, absolute string paths were internally prepended with `modulesPath`, which caused the module filtering to fail. --- lib/modules.nix | 4 +++- lib/tests/modules.sh | 1 + lib/tests/modules/disable-define-enable-string-path.nix | 5 +++++ 3 files changed, 9 insertions(+), 1 deletion(-) create mode 100644 lib/tests/modules/disable-define-enable-string-path.nix diff --git a/lib/modules.nix b/lib/modules.nix index 7f1646e9b8bc..6a1b0bfdc4fa 100644 --- a/lib/modules.nix +++ b/lib/modules.nix @@ -433,7 +433,9 @@ rec { # modules recursively. It returns the final list of unique-by-key modules filterModules = modulesPath: { disabled, modules }: let - moduleKey = m: if isString m then toString modulesPath + "/" + m else toString m; + moduleKey = m: if isString m && (builtins.substring 0 1 m != "/") + then toString modulesPath + "/" + m + else toString m; disabledKeys = map moduleKey disabled; keyFilter = filter (attrs: ! elem attrs.key disabledKeys); in map (attrs: attrs.module) (builtins.genericClosure { diff --git a/lib/tests/modules.sh b/lib/tests/modules.sh index c92cc62023b5..2ef7c4806595 100755 --- a/lib/tests/modules.sh +++ b/lib/tests/modules.sh @@ -130,6 +130,7 @@ checkConfigOutput '^true$' "$@" ./define-enable.nix ./define-attrsOfSub-foo-enab set -- config.enable ./define-enable.nix ./declare-enable.nix checkConfigOutput '^true$' "$@" checkConfigOutput '^false$' "$@" ./disable-define-enable.nix +checkConfigOutput '^false$' "$@" ./disable-define-enable-string-path.nix checkConfigError "The option .*enable.* does not exist. Definition values:\n\s*- In .*: true" "$@" ./disable-declare-enable.nix checkConfigError "attribute .*enable.* in selection path .*config.enable.* not found" "$@" ./disable-define-enable.nix ./disable-declare-enable.nix checkConfigError "attribute .*enable.* in selection path .*config.enable.* not found" "$@" ./disable-enable-modules.nix diff --git a/lib/tests/modules/disable-define-enable-string-path.nix b/lib/tests/modules/disable-define-enable-string-path.nix new file mode 100644 index 000000000000..6429a6d6354a --- /dev/null +++ b/lib/tests/modules/disable-define-enable-string-path.nix @@ -0,0 +1,5 @@ +{ lib, ... }: + +{ + disabledModules = [ (toString ./define-enable.nix) ]; +}