Skip to content

Commit

Permalink
fix docs
Browse files Browse the repository at this point in the history
  • Loading branch information
MattSturgeon committed Jan 8, 2025
1 parent 3a0417a commit 3fc2c94
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 22 deletions.
30 changes: 13 additions & 17 deletions nixos/doc/manual/development/freeform-modules.section.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,17 @@ submodules.

<!-- TODO: Link to more details on the record and submodule types -->

Wildcard records can also be used to achieve the same thing, and may be
Freeform records can also be used to achieve the same thing, and may be
preferred in many scenarios due to their improved performance when
compared to submodules and also the ability to declare "optional" fields.

Wildcards records are used by simply passing `wildcard = types.anything`
Freeform records are used by simply passing `freeformType = types.anything`
to a record type definition.

::: {#ex-wildcard-record .example}
### Wildcard record
::: {#ex-freeform-record .example}
### Freeform record

Most freeform submodules can also be represented as wildcard records.
Most freeform submodules can also be represented as freeform records.

The following example is equivalent to the first submodule example:

Expand All @@ -36,20 +36,16 @@ The following example is equivalent to the first submodule example:
options.settings = lib.mkOption {
type = lib.types.record {
wildcard = lib.types.str;
freeformType = lib.types.str;
# We want this attribute to be checked for the correct type
fields.port = lib.mkOption {
type = lib.types.port;
# Declaring the option also allows defining a default value
default = 8080;
};
# We could use an "optional" field, instead of supplying a default
optionalFields.port = lib.mkOption {
type = lib.types.port;
};
fields = {
port = lib.mkField {
type = lib.types.port;
default = 8080;
# We could also set `optional = true` instead of setting a default:
# optional = true;
};
};
};
}
Expand Down
10 changes: 5 additions & 5 deletions nixos/doc/manual/development/option-types.section.md
Original file line number Diff line number Diff line change
Expand Up @@ -462,18 +462,18 @@ Composed types are types that take a type as parameter. `listOf
<!-- TODO: Add some examples!! -->

Records are a simpler alternative to [submodules](#section-option-types-submodule).
Rather than merging fully-fledged modules, a record consists only of "field" sub-options and an optional "wildcard" type, similar to a submodule's `freeformType`.
Rather than merging fully-fledged modules, a record consists only of "fields" and an optional `freeformType`.

::: {.note}
Because records are not implemented using the full module system, they can usually be evaluated significantly faster than submodules.
:::

### Record fields
### Record fields {#section-option-types-record-fields}

A record's sub-options are declared as `fields`, using `lib.mkField`. A field is very similar to an option.
A record's fields are declared using `lib.mkField`. A field is very similar to an option.
You can also use record or submodule type fields to implement _nested_ options.

### Record optional fields
### Record optional fields {#section-option-types-record-optional-fields}

Normally, options and fields will always be present in the final value. When undefined they are present as a value that **throws**.

Expand All @@ -484,7 +484,7 @@ This means you can do things like `config.recordOption ? optionalField`.
Optional fields should not define a `default`.
:::

### Freeform Records
### Freeform Records {#section-option-types-record-freeform}

Records can optionally be declared with a freeformType, which can be used to allow definitions that do not match the explicitly declared fields.
Any definition that does not match a field will be checked against the freeformType, for example you could set `freeformType = types.anything`.
Expand Down
15 changes: 15 additions & 0 deletions nixos/doc/manual/redirects.json
Original file line number Diff line number Diff line change
Expand Up @@ -1577,6 +1577,18 @@
"sec-option-types-composed": [
"index.html#sec-option-types-composed"
],
"section-option-types-record": [
"index.html#section-option-types-record"
],
"section-option-types-record-fields": [
"index.html#section-option-types-record-fields"
],
"section-option-types-record-optional-fields": [
"index.html#section-option-types-record-optional-fields"
],
"section-option-types-record-freeform": [
"index.html#section-option-types-record-freeform"
],
"section-option-types-submodule": [
"index.html#section-option-types-submodule"
],
Expand Down Expand Up @@ -1646,6 +1658,9 @@
"sec-freeform-modules": [
"index.html#sec-freeform-modules"
],
"ex-freeform-record": [
"index.html#ex-freeform-record"
],
"ex-freeform-module": [
"index.html#ex-freeform-module"
],
Expand Down

0 comments on commit 3fc2c94

Please sign in to comment.