lib.types.submoduleWith: add onlyDefinesConfig parameter#437972
lib.types.submoduleWith: add onlyDefinesConfig parameter#437972yunfachi wants to merge 3 commits intoNixOS:masterfrom
Conversation
f222f92 to
da2a718
Compare
| { | ||
| _file = file; | ||
| config = value; | ||
| } | ||
| else if isFunction value && onlyDefinesConfig then |
There was a problem hiding this comment.
I am not sure if we should do this here because we have module import logic in module.nix already.
That also handles i.e. string types and might have other divergent logic. It would be a bit suprising to get different import logic
There was a problem hiding this comment.
Absolutely agree. Did you mean something like this?
args: {
_file = file;
config = lib.applyModuleArgsIfFunction "somekey" value args;
}There was a problem hiding this comment.
It would be a bit suprising to get different import logic
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.
Perhaps we should reframe it as "definitions only" evaluation? I don't think we can make it an evalModules flag; instead evalModules would have to be made aware of two sets of modules: the ones that declare (and define) stuff, and the ones that only define.
This is a lot like types.record (as of yet WIP, #334680) and if we were to use that instead, it would also yield better performance, as it won't have to re-evaluate the options hierarchy for each instance of the submodule (i.e. when it's in an attrsOf or similar where multiple of those attributes contain the submodule, as is usually the case).
There was a problem hiding this comment.
this option makes it impossible to declare any options or imports. Perhaps we should reframe it as "definitions only" evaluation?
I don't think so. The initial module can still use config to create options, right?
| - *`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. |
There was a problem hiding this comment.
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 submodule) or the inner context, definitions in config.
I think we can reframe this as interpreting the module attributes differently. To be precise, we're interpreting differently the attributes that would take the place of the module attributes, but I think that's unnecessary precision that makes it more confusing again.
Anyway, here's what I came up with as an alternative description.
| - *`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. | |
| - *`onlyDefinesConfig`* Whether all module attributes are interpreted | |
| exclusively as `config` definitions. | |
| This is similar to `shorthandOnlyDefinesConfig`, but applies to all module | |
| attributes, including those that occur in function bodies and module files. | |
| Module arguments are still available and behave as usual. |
There was a problem hiding this comment.
Hmm, the original first part was indeed incorrect, thanks! What do you think about this version?
| - *`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. | |
| - *`onlyDefinesConfig`* Whether all module attributes are interpreted | |
| exclusively as `config` definitions. | |
| This is similar to `shorthandOnlyDefinesConfig`, but it applies to all | |
| option definition values, including [paths](https://nix.dev/manual/nix/latest/language/types.html#type-path) to modules and [functions](https://nix.dev/manual/nix/latest/language/types.html#type-function) | |
| that are invoked with module arguments. |
…es to shorthandOnlyDefinesConfig
Both approaches are valid for different use cases. |
Added the
onlyDefinesConfigparameter, which works likeshorthandOnlyDefinesConfigbut applies not only toattrsetvalues, but also tofunctionandpathvalues. This is especially useful when a submodule hasspecialArgsand definesoptions.{options, config}.Things done
passthru.tests.nixpkgs-reviewon this PR. See nixpkgs-review usage../result/bin/.Add a 👍 reaction to pull requests you find important.