Merge pull request #76861 from Infinisil/paths-as-submodules

lib/types: Allow paths as submodule values
This commit is contained in:
Robert Hensing 2020-01-12 14:19:04 +01:00 committed by GitHub
commit 9884cb3ed0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 21 additions and 10 deletions

View file

@ -174,8 +174,7 @@ checkConfigOutput "true" config.submodule.inner ./declare-submoduleWith-modules.
checkConfigOutput "true" config.submodule.outer ./declare-submoduleWith-modules.nix checkConfigOutput "true" config.submodule.outer ./declare-submoduleWith-modules.nix
## Paths should be allowed as values and work as expected ## Paths should be allowed as values and work as expected
# Temporarily disabled until https://github.com/NixOS/nixpkgs/pull/76861 checkConfigOutput "true" config.submodule.enable ./declare-submoduleWith-path.nix
#checkConfigOutput "true" config.submodule.enable ./declare-submoduleWith-path.nix
# Check that disabledModules works recursively and correctly # Check that disabledModules works recursively and correctly
checkConfigOutput "true" config.enable ./disable-recursive/main.nix checkConfigOutput "true" config.enable ./disable-recursive/main.nix

View file

@ -430,14 +430,16 @@ rec {
else unify (if shorthandOnlyDefinesConfig then { config = value; } else value); else unify (if shorthandOnlyDefinesConfig then { config = value; } else value);
allModules = defs: modules ++ imap1 (n: { value, file }: allModules = defs: modules ++ imap1 (n: { value, file }:
# Annotate the value with the location of its definition for better error messages if isAttrs value || isFunction value then
coerce (lib.modules.unifyModuleSyntax file "${toString file}-${toString n}") value # 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
) defs; ) defs;
in in
mkOptionType rec { mkOptionType rec {
name = "submodule"; name = "submodule";
check = x: isAttrs x || isFunction x; check = x: isAttrs x || isFunction x || path.check x;
merge = loc: defs: merge = loc: defs:
(evalModules { (evalModules {
modules = allModules defs; modules = allModules defs;

View file

@ -257,9 +257,9 @@
<listitem> <listitem>
<para> <para>
A set of sub options <replaceable>o</replaceable>. A set of sub options <replaceable>o</replaceable>.
<replaceable>o</replaceable> can be an attribute set or a function <replaceable>o</replaceable> can be an attribute set, a function
returning an attribute set. Submodules are used in composed types to returning an attribute set, or a path to a file containing such a value. Submodules are used in
create modular options. This is equivalent to composed types to create modular options. This is equivalent to
<literal>types.submoduleWith { modules = toList o; shorthandOnlyDefinesConfig = true; }</literal>. <literal>types.submoduleWith { modules = toList o; shorthandOnlyDefinesConfig = true; }</literal>.
Submodules are detailed in Submodules are detailed in
<xref <xref

View file

@ -391,6 +391,16 @@ users.users.me =
<link xlink:href="https://github.com/NixOS/nixpkgs/pull/63103">PR #63103</link>. <link xlink:href="https://github.com/NixOS/nixpkgs/pull/63103">PR #63103</link>.
</para> </para>
</listitem> </listitem>
<listitem>
<para>
For NixOS modules, the types <literal>types.submodule</literal> and <literal>types.submoduleWith</literal> now support
paths as allowed values, similar to how <literal>imports</literal> supports paths.
Because of this, if you have a module that defines an option of type
<literal>either (submodule ...) path</literal>, it will break since a path
is now treated as the first type instead of the second. To fix this, change
the type to <literal>either path (submodule ...)</literal>.
</para>
</listitem>
</itemizedlist> </itemizedlist>
</section> </section>

View file

@ -113,7 +113,7 @@ in
otherCert = "/var/certmgr/specs/other-cert.json"; otherCert = "/var/certmgr/specs/other-cert.json";
} }
''; '';
type = with types; attrsOf (either (submodule { type = with types; attrsOf (either path (submodule {
options = { options = {
service = mkOption { service = mkOption {
type = nullOr str; type = nullOr str;
@ -148,7 +148,7 @@ in
description = "certmgr spec request object."; description = "certmgr spec request object.";
}; };
}; };
}) path); }));
description = '' description = ''
Certificate specs as described by: Certificate specs as described by:
<link xlink:href="https://github.com/cloudflare/certmgr#certificate-specs" /> <link xlink:href="https://github.com/cloudflare/certmgr#certificate-specs" />