From 63908e733fcf3f20ce961f868d58b06b35bc1a93 Mon Sep 17 00:00:00 2001 From: Silvan Mosberger Date: Sun, 27 Sep 2020 17:15:57 +0200 Subject: [PATCH 1/2] lib/tests: Add submodule file propagation test --- lib/tests/modules.sh | 4 ++++ lib/tests/modules/submoduleFiles.nix | 21 +++++++++++++++++++++ 2 files changed, 25 insertions(+) create mode 100644 lib/tests/modules/submoduleFiles.nix diff --git a/lib/tests/modules.sh b/lib/tests/modules.sh index 309c5311361c1..176c20a28419c 100755 --- a/lib/tests/modules.sh +++ b/lib/tests/modules.sh @@ -179,6 +179,10 @@ checkConfigOutput "true" config.submodule.outer ./declare-submoduleWith-modules. ## Paths should be allowed as values and work as expected checkConfigOutput "true" config.submodule.enable ./declare-submoduleWith-path.nix +# Check the file location information is propagated into submodules +checkConfigOutput the-file.nix config.submodule.internalFiles.0 ./submoduleFiles.nix + + # Check that disabledModules works recursively and correctly checkConfigOutput "true" config.enable ./disable-recursive/main.nix checkConfigOutput "true" config.enable ./disable-recursive/{main.nix,disable-foo.nix} diff --git a/lib/tests/modules/submoduleFiles.nix b/lib/tests/modules/submoduleFiles.nix new file mode 100644 index 0000000000000..c0d9b2cef3e8d --- /dev/null +++ b/lib/tests/modules/submoduleFiles.nix @@ -0,0 +1,21 @@ +{ lib, ... }: { + options.submodule = lib.mkOption { + default = {}; + type = lib.types.submoduleWith { + modules = [ ({ options, ... }: { + options.value = lib.mkOption {}; + + options.internalFiles = lib.mkOption { + default = options.value.files; + }; + })]; + }; + }; + + imports = [ + { + _file = "the-file.nix"; + submodule.value = 10; + } + ]; +} From e1aa9a8ab5e39838dc110b663e89658ca1e83c37 Mon Sep 17 00:00:00 2001 From: Silvan Mosberger Date: Sun, 27 Sep 2020 16:39:57 +0200 Subject: [PATCH 2/2] lib/types: Simplify submoduleWith shorthandOnlyDefinesConfig handling The module system already uses the parent modules _type as a fallback, so we don't need to inject the file in a weird way --- lib/types.nix | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/lib/types.nix b/lib/types.nix index ee891f8231b60..cbf82a3760ed2 100644 --- a/lib/types.nix +++ b/lib/types.nix @@ -467,15 +467,10 @@ rec { let inherit (lib.modules) evalModules; - coerce = unify: value: if isFunction value - then setFunctionArgs (args: unify (value args)) (functionArgs value) - else unify (if shorthandOnlyDefinesConfig then { config = value; } else value); - allModules = defs: modules ++ imap1 (n: { value, file }: - if isAttrs value || isFunction value then - # Annotate the value with the location of its definition for better error messages - coerce (lib.modules.unifyModuleSyntax file "${toString file}-${toString n}") value - else value + if isAttrs value && shorthandOnlyDefinesConfig + then { _file = file; config = value; } + else { _file = file; imports = [ value ]; } ) defs; freeformType = (evalModules {