diff --git a/lib/default.nix b/lib/default.nix index e671fcf9546d9..19316addb8cb6 100644 --- a/lib/default.nix +++ b/lib/default.nix @@ -446,6 +446,7 @@ let fixupOptionType mkIf mkAssert + mkDefinition mkMerge mkOverride mkOptionDefault diff --git a/lib/modules.nix b/lib/modules.nix index a9ddaf7bda02f..7716e855ebb29 100644 --- a/lib/modules.nix +++ b/lib/modules.nix @@ -1097,10 +1097,16 @@ let # Process mkMerge and mkIf properties. defs' = concatMap ( m: - map (value: { - inherit (m) file; - inherit value; - }) (addErrorContext "while evaluating definitions from `${m.file}':" (dischargeProperties m.value)) + map ( + value: + if value._type or null == "definition" then + value + else + { + inherit (m) file; + inherit value; + } + ) (addErrorContext "while evaluating definitions from `${m.file}':" (dischargeProperties m.value)) ) defs; # Process mkOverride properties. @@ -1365,6 +1371,11 @@ let inherit contents; }; + /** + Return a definition with file location information. + */ + mkDefinition = args@{ file, value, ... }: args // { _type = "definition"; }; + mkOverride = priority: content: { _type = "override"; inherit priority content; @@ -2095,6 +2106,7 @@ private mkBefore mkChangedOptionModule mkDefault + mkDefinition mkDerivedConfig mkFixStrictness mkForce diff --git a/lib/tests/modules.sh b/lib/tests/modules.sh index e623c0fb55b86..9df6e61797b7f 100755 --- a/lib/tests/modules.sh +++ b/lib/tests/modules.sh @@ -673,6 +673,14 @@ checkConfigError 'The option .conflictingPathOptionType. in .*/pathWith.nix. is # types.pathWith { inStore = true; absolute = false; } checkConfigError 'In pathWith, inStore means the path must be absolute' config.impossiblePathOptionType ./pathWith.nix +# mkDefinition +# check that mkDefinition 'file' is printed in the error message +checkConfigError 'Cannot merge definitions.*\n\s*- In .file.*\n\s*- In .other.*' config.conflict ./mkDefinition.nix +checkConfigError 'A definition for option .viaOptionDefault. is not of type .boolean.*' config.viaOptionDefault ./mkDefinition.nix +checkConfigOutput '^true$' config.viaConfig ./mkDefinition.nix +checkConfigOutput '^true$' config.mkMerge ./mkDefinition.nix +checkConfigOutput '^true$' config.mkForce ./mkDefinition.nix + cat <