-
-
Notifications
You must be signed in to change notification settings - Fork 18.7k
[exploratory] Module system types.record, types.fix
#257511
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,25 @@ | ||
| { lib, ... }: | ||
|
|
||
| let | ||
| inherit (lib) mkOption types; | ||
|
|
||
| person = types.record { | ||
| fields = { | ||
| nixerSince = mkOption { | ||
| type = types.int; | ||
| }; | ||
| name = mkOption { | ||
| type = types.str; | ||
| }; | ||
| isCool = mkOption { | ||
| type = types.bool; | ||
| default = "yeah"; | ||
| }; | ||
| }; | ||
| }; | ||
|
|
||
| in { | ||
| options.people = mkOption { | ||
| type = types.attrsOf person; | ||
| }; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| { lib, ... }: | ||
|
|
||
| let | ||
| inherit (lib) mkOption types; | ||
|
|
||
| person = types.record { | ||
| fields = { | ||
| nixerSince = mkOption { | ||
| type = types.int; | ||
| }; | ||
| name = mkOption { | ||
| type = types.str; | ||
| }; | ||
| }; | ||
| wildcard = mkOption { | ||
| type = types.bool; | ||
| }; | ||
| }; | ||
|
|
||
| in { | ||
| options.people = mkOption { | ||
| type = types.attrsOf person; | ||
| }; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| { lib, ... }: | ||
|
|
||
| let | ||
| inherit (lib) mkOption types; | ||
|
|
||
| person = types.record { | ||
| fields = { | ||
| nixerSince = mkOption { | ||
| type = types.int; | ||
| }; | ||
| name = mkOption { | ||
| type = types.str; | ||
| }; | ||
| isCool = mkOption { | ||
| type = types.bool; | ||
| default = true; | ||
| }; | ||
| }; | ||
| }; | ||
|
|
||
| in { | ||
| options.people = mkOption { | ||
| type = types.attrsOf person; | ||
| }; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| { lib, ... }: { | ||
| people.alice = { | ||
| nixerSince = 2016; | ||
| name = "Alice"; | ||
| hiRes = true; | ||
| mechKeyboard = true; | ||
| spiders = false; | ||
| }; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| { | ||
| people.alice = { | ||
| nixerSince = 2016; | ||
| name = "Alice"; | ||
| }; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| { | ||
| people.bob = { | ||
| nixerSince = 2019; | ||
| name = "Bob"; | ||
| }; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| { | ||
| people.mallory = { | ||
| nixerSince = "beginning of time"; | ||
| name = "Bobby"; | ||
| }; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| { lib, ... }: | ||
| let | ||
| inherit (lib) types mkOption; | ||
|
|
||
| person = types.record { | ||
| fields = { | ||
| name = mkOption { type = lib.types.str; }; | ||
| age = mkOption { type = lib.types.int; }; | ||
| }; | ||
| }; | ||
|
|
||
| bobber = { value }: { | ||
| name = if value.age < 10 then "bobby" else "bob"; | ||
| }; | ||
| in | ||
| { | ||
| options = { | ||
| people = mkOption { | ||
| type = types.attrsOf (types.fix person); | ||
| default = {}; | ||
| }; | ||
| }; | ||
| config = { | ||
| people.bob = lib.mkMerge [ { age = 23; } bobber ]; | ||
| people.lilbob = lib.mkMerge [ { age = 3; } bobber ]; | ||
| }; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| { lib }: | ||
|
|
||
| let | ||
| inherit (lib) toFunction mkOptionType | ||
| optionDescriptionPhrase isFunction mergeDefinitions defaultFunctor; | ||
|
|
||
| # TODO: should this be a type or a "property"? (i.e. one of `mkIf`, `mkMerge`, etc) | ||
| fix = t: mkOptionType { | ||
| name = "fixpoint of ${t.name}"; | ||
| description = t.description; | ||
| descriptionClass = "composite"; | ||
| check = v: isFunction v || t.check v; | ||
| merge = loc: defs: | ||
| let | ||
| # TODO: facilities elsewhere for wiring `options` and/or other "meta" info into this. | ||
| args = { value = r; }; | ||
| r = (mergeDefinitions loc t (map (fn: { inherit (fn) file; value = toFunction fn.value args; }) defs)).mergedValue; | ||
| in r; | ||
| getSubOptions = prefix: t.getSubOptions prefix; | ||
| getSubModules = t.getSubModules; | ||
| substSubModules = m: fix (t.substSubModules m); | ||
| functor = defaultFunctor "fix" // { inherit t; }; | ||
| nestedTypes.t = t; | ||
| }; | ||
|
|
||
| in | ||
| { | ||
| inherit fix; | ||
| } |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,86 @@ | ||||||
| { lib }: | ||||||
|
|
||||||
| let | ||||||
| inherit (lib) | ||||||
| mapAttrs zipAttrsWith removeAttrs attrNames showOption | ||||||
| mergeDefinitions mkOptionType isAttrs | ||||||
| ; | ||||||
|
|
||||||
| record = args@{ fields, wildcard ? null }: | ||||||
|
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. What's missing is something like
|
||||||
| let | ||||||
| fields = | ||||||
| mapAttrs | ||||||
| (name: value: | ||||||
| if value._type or null != "option" | ||||||
| then throw "Record field `${lib.escapeNixIdentifier name}` must be declared with `mkOption`." | ||||||
| else if value?apply then throw "In field ${lib.escapeNixIdentifier name} records do not support options with `apply`" | ||||||
| else if value?readOnly then throw "In field ${lib.escapeNixIdentifier name} records do not support options with `readOnly`" | ||||||
| else value) | ||||||
| args.fields; | ||||||
| wildcard = | ||||||
| if args.wildcard or null == null | ||||||
| then null | ||||||
| else if args.wildcard._type or null != "option" | ||||||
| then throw "Record wildcard must be declared with `mkOption`." | ||||||
| else if args.wildcard?apply | ||||||
| then throw "Records do not support options with `apply`, but wildcard has `apply`." | ||||||
| else args.wildcard; | ||||||
| in | ||||||
| mkOptionType { | ||||||
| name = "record"; | ||||||
| description = if wildcard == null then "record" else "open record of ${wildcard.description}"; | ||||||
| descriptionClass = if wildcard == null then "noun" else "composite"; | ||||||
| check = isAttrs; | ||||||
| merge = loc: defs: | ||||||
| let | ||||||
| data = zipAttrsWith | ||||||
| (name: values: values) | ||||||
| (map | ||||||
| (def: mapAttrs (_fieldName: fieldValue: { inherit (def) file; value = fieldValue; }) def.value) | ||||||
| defs); | ||||||
| requiredFieldValues = | ||||||
| mapAttrs | ||||||
| (fieldName: fieldOption: | ||||||
| builtins.addErrorContext ("while evaluating the field `${fieldName}' of option `${showOption loc}'") ( | ||||||
| (mergeDefinitions | ||||||
| (loc ++ [fieldName]) | ||||||
| fieldOption.type | ||||||
| (data.${fieldName} or [] ++ | ||||||
| (if fieldOption?default | ||||||
| then [{ value = fieldOption.default; file = "the default value of option ${showOption loc}"; }] | ||||||
| else [])) | ||||||
| ).mergedValue | ||||||
| ) | ||||||
| ) | ||||||
| fields; | ||||||
| extraData = removeAttrs data (attrNames fields); | ||||||
| in | ||||||
| if wildcard == null | ||||||
| then | ||||||
| if extraData == {} | ||||||
| then requiredFieldValues | ||||||
| else throw "A definition for option `${showOption loc}' has an unknown field." | ||||||
|
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. Which one? Print at least one attribute name. |
||||||
| else | ||||||
| requiredFieldValues // | ||||||
|
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. Maybe flip the |
||||||
| mapAttrs | ||||||
| (fieldName: fieldDefs: | ||||||
| builtins.addErrorContext ("while evaluating the wildcard field `${fieldName}' of option `${showOption loc}'") ( | ||||||
| (mergeDefinitions | ||||||
| (loc ++ [fieldName]) | ||||||
| wildcard.type | ||||||
|
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.
Suggested change
|
||||||
| fieldDefs | ||||||
| ).mergedValue | ||||||
| ) | ||||||
| ) | ||||||
| extraData; | ||||||
| nestedTypes = fields // { | ||||||
| # potential collision with `_wildcard` field | ||||||
|
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. Unimportant, but I guess we could rename the original fields of that satisfy regex
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. Should it be an error for nested types to have a wildcard option? |
||||||
| _wildcard = wildcard; | ||||||
| }; | ||||||
| }; | ||||||
|
|
||||||
| in | ||||||
| # public | ||||||
| { | ||||||
| inherit record; | ||||||
| } | ||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I feel like
fixis rather useless in a world where we still have (more capable)submodules to handle this aspect.We could first, or only, make
recorduseful on its own, for data modeling use cases, whereassubmodule(or usually justevalModules) can continue to fill a more programming-like role.