-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
lib.types.submoduleWith: add onlyDefinesConfig parameter #437972
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| { lib, ... }: | ||
| let | ||
| sub = { | ||
| # An option named `config`; reason to have `onlyDefinesConfig = true;` | ||
| options.config = lib.mkOption { | ||
| type = lib.types.bool; | ||
| default = false; | ||
| }; | ||
| config._module.args.bar = true; | ||
| }; | ||
| in | ||
| { | ||
| options.submodule = lib.mkOption { | ||
| type = lib.types.submoduleWith { | ||
| modules = [ sub ]; | ||
| specialArgs.foo = true; | ||
| onlyDefinesConfig = true; | ||
| }; | ||
| default = { }; | ||
| }; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| { foo, bar, ... }: | ||
| { | ||
| config = foo && bar; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| { | ||
| submodule = ./define-submoduleWith-onlydefinesconfig-path-file.nix; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| { | ||
| submodule = | ||
| { foo, bar, ... }: | ||
roberth marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| { | ||
| config = foo && bar; | ||
| }; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1184,6 +1184,7 @@ let | |
| { | ||
| modules, | ||
| specialArgs ? { }, | ||
| onlyDefinesConfig ? false, | ||
| shorthandOnlyDefinesConfig ? false, | ||
| description ? null, | ||
| class ? null, | ||
|
|
@@ -1195,11 +1196,21 @@ let | |
| defs: | ||
| map ( | ||
| { value, file }: | ||
| if isAttrs value && shorthandOnlyDefinesConfig then | ||
| if isAttrs value && (onlyDefinesConfig || shorthandOnlyDefinesConfig) then | ||
| { | ||
| _file = file; | ||
| config = value; | ||
| } | ||
| else if isFunction value && onlyDefinesConfig then | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am not sure if we should do this here because we have module import logic in module.nix already.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Absolutely agree. Did you mean something like this? args: {
_file = file;
config = lib.applyModuleArgsIfFunction "somekey" value args;
}
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
True, but I haven't made up my mind on this yet. Maybe a different perspective: this option makes it impossible to declare any options or imports. This is a lot like
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I don't think so. The initial module can still use config to create options, right? |
||
| lib.setFunctionArgs (args: { | ||
| _file = file; | ||
| config = value args; | ||
| }) (lib.functionArgs value) | ||
| else if builtins.isPath value && onlyDefinesConfig then | ||
| lib.setFunctionArgs (args: { | ||
| _file = file; | ||
| config = import value args; | ||
| }) (lib.functionArgs (import value)) | ||
| else | ||
| { | ||
| _file = file; | ||
|
|
@@ -1273,12 +1284,10 @@ let | |
| getSubOptions = | ||
| prefix: | ||
| let | ||
| docsEval = ( | ||
| base.extendModules { | ||
| inherit prefix; | ||
| modules = [ noCheckForDocsModule ]; | ||
| } | ||
| ); | ||
| docsEval = base.extendModules { | ||
| inherit prefix; | ||
| modules = [ noCheckForDocsModule ]; | ||
| }; | ||
| # Intentionally shadow the freeformType from the possibly *checked* | ||
| # configuration. See `noCheckForDocsModule` comment. | ||
| inherit (docsEval._module) freeformType; | ||
|
|
@@ -1309,6 +1318,7 @@ let | |
| modules | ||
| class | ||
| specialArgs | ||
| onlyDefinesConfig | ||
| shorthandOnlyDefinesConfig | ||
| description | ||
| ; | ||
|
|
@@ -1334,6 +1344,15 @@ let | |
| lhs.specialArgs // rhs.specialArgs | ||
| else | ||
| throw "A submoduleWith option is declared multiple times with the same specialArgs \"${toString (attrNames intersecting)}\""; | ||
| onlyDefinesConfig = | ||
| if lhs.onlyDefinesConfig == null then | ||
| rhs.onlyDefinesConfig | ||
| else if rhs.onlyDefinesConfig == null then | ||
| lhs.onlyDefinesConfig | ||
| else if lhs.onlyDefinesConfig == rhs.onlyDefinesConfig then | ||
| lhs.onlyDefinesConfig | ||
| else | ||
| throw "A submoduleWith option is declared multiple times with conflicting onlyDefinesConfig values"; | ||
| shorthandOnlyDefinesConfig = | ||
| if lhs.shorthandOnlyDefinesConfig == null then | ||
| rhs.shorthandOnlyDefinesConfig | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -251,7 +251,7 @@ Submodules are detailed in [Submodule](#section-option-types-submodule). | |||||||||||||||||||||||||||||||||||||||||
| options. This is equivalent to | ||||||||||||||||||||||||||||||||||||||||||
| `types.submoduleWith { modules = toList o; shorthandOnlyDefinesConfig = true; }`. | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| `types.submoduleWith` { *`modules`*, *`specialArgs`* ? {}, *`shorthandOnlyDefinesConfig`* ? false } | ||||||||||||||||||||||||||||||||||||||||||
| `types.submoduleWith` { *`modules`*, *`specialArgs`* ? {}, *`onlyDefinesConfig`* ? false, *`shorthandOnlyDefinesConfig`* ? false } | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| : Like `types.submodule`, but more flexible and with better defaults. | ||||||||||||||||||||||||||||||||||||||||||
| It has parameters | ||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -274,6 +274,12 @@ Submodules are detailed in [Submodule](#section-option-types-submodule). | |||||||||||||||||||||||||||||||||||||||||
| because `lib` itself is used to define `_module.args`, which | ||||||||||||||||||||||||||||||||||||||||||
| makes using `_module.args` to define it impossible. | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| - *`onlyDefinesConfig`* Whether definitions of this type should | ||||||||||||||||||||||||||||||||||||||||||
| always default to the `config` section of a module. In contrast to | ||||||||||||||||||||||||||||||||||||||||||
| `shorthandOnlyDefinesConfig`, this applies to all definition values, | ||||||||||||||||||||||||||||||||||||||||||
| including functions and paths. When a value is a function, it is invoked | ||||||||||||||||||||||||||||||||||||||||||
| with the same arguments as the module itself. | ||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+277
to
+281
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The use of definitions was a bit confusing to me here, and I wasn't sure whether it referred to the "outermost" context (definitions which happen to be modules because we're talking about
Suggested change
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm, the original first part was indeed incorrect, thanks! What do you think about this version?
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| - *`shorthandOnlyDefinesConfig`* Whether definitions of this type | ||||||||||||||||||||||||||||||||||||||||||
| should default to the `config` section of a module (see | ||||||||||||||||||||||||||||||||||||||||||
| [Example: Structure of NixOS Modules](#ex-module-syntax)) | ||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -290,6 +296,8 @@ Submodules are detailed in [Submodule](#section-option-types-submodule). | |||||||||||||||||||||||||||||||||||||||||
| With this option enabled, defining a non-`config` section | ||||||||||||||||||||||||||||||||||||||||||
| requires using a function: | ||||||||||||||||||||||||||||||||||||||||||
| `the-submodule = { ... }: { options = { ... }; }`. | ||||||||||||||||||||||||||||||||||||||||||
| Note that this behavior does *not* apply to [path values](https://nix.dev/manual/nix/latest/language/types.html#type-path), | ||||||||||||||||||||||||||||||||||||||||||
| similar to how it does not apply to [function values](https://nix.dev/manual/nix/latest/language/types.html#type-function). | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| `types.deferredModule` | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
Uh oh!
There was an error while loading. Please reload this page.