diff options
Diffstat (limited to 'lib/types.nix')
-rw-r--r-- | lib/types.nix | 38 |
1 files changed, 28 insertions, 10 deletions
diff --git a/lib/types.nix b/lib/types.nix index 3fcac9c31b313..00d97bf572372 100644 --- a/lib/types.nix +++ b/lib/types.nix @@ -368,13 +368,21 @@ rec { emptyValue = { value = {}; }; }; - # derivation is a reserved keyword. + # A package is a top-level store path (/nix/store/hash-name). This includes: + # - derivations + # - more generally, attribute sets with an `outPath` or `__toString` attribute + # pointing to a store path, e.g. flake inputs + # - strings with context, e.g. "${pkgs.foo}" or (toString pkgs.foo) + # - hardcoded store path literals (/nix/store/hash-foo) or strings without context + # ("/nix/store/hash-foo"). These get a context added to them using builtins.storePath. package = mkOptionType { name = "package"; check = x: isDerivation x || isStorePath x; merge = loc: defs: let res = mergeOneOption loc defs; - in if isDerivation res then res else toDerivation res; + in if builtins.isPath res || (builtins.isString res && ! builtins.hasContext res) + then toDerivation res + else res; }; shellPackage = package // { @@ -535,7 +543,9 @@ rec { description = "optionType"; check = value: value._type or null == "option-type"; merge = loc: defs: - let + if length defs == 1 + then (head defs).value + else let # Prepares the type definitions for mergeOptionDecls, which # annotates submodules types with file locations optionModules = map ({ value, file }: @@ -562,14 +572,18 @@ 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); + shorthandToModule = if shorthandOnlyDefinesConfig == false + then value: value + else value: { config = value; }; allModules = defs: 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 + if isFunction value + then setFunctionArgs + (args: lib.modules.unifyModuleSyntax file "${toString file}-${toString n}" (value args)) + (functionArgs value) + else if isAttrs value + then + lib.modules.unifyModuleSyntax file "${toString file}-${toString n}" (shorthandToModule value) else value ) defs; @@ -637,7 +651,11 @@ rec { then lhs.specialArgs // rhs.specialArgs else throw "A submoduleWith option is declared multiple times with the same specialArgs \"${toString (attrNames intersecting)}\""; shorthandOnlyDefinesConfig = - if lhs.shorthandOnlyDefinesConfig == rhs.shorthandOnlyDefinesConfig + if lhs.shorthandOnlyDefinesConfig == null + then rhs.shorthandOnlyDefinesConfig + else if rhs.shorthandOnlyDefinesConfig == null + then lhs.shorthandOnlyDefinesConfig + else if lhs.shorthandOnlyDefinesConfig == rhs.shorthandOnlyDefinesConfig then lhs.shorthandOnlyDefinesConfig else throw "A submoduleWith option is declared multiple times with conflicting shorthandOnlyDefinesConfig values"; }; |