Home Explore Blog CI



nixpkgs

8th chunk of `nixos/doc/manual/development/option-types.section.md`
279333fa9bc4f387d725f4267e0e2d0f267d3d6e93d01de600000001000007ee
    ::: {#ex-extending-type-check-2 .example}
    ### Overriding a type check

    ```nix
    {
      nixThings = mkOption {
        description = "words that start with 'nix'";
        type = types.str // {
          check = (x: lib.hasPrefix "nix" x);
        };
      };
    }
    ```
    :::

`merge`

:   Function to merge the options values when multiple values are set.
    The function takes two parameters, `loc` the option path as a list
    of strings, and `defs` the list of defined values as a list. It is
    possible to override a type merge function for custom needs.

## Custom types {#sec-option-types-custom}

Custom types can be created with the `mkOptionType` function. As type
creation includes some more complex topics such as submodule handling,
it is recommended to get familiar with `types.nix` code before creating
a new type.

The only required parameter is `name`.

`name`

:   A string representation of the type function name.

`description`

:   Description of the type used in documentation. Give information of
    the type and any of its arguments.

`check`

:   A function to type check the definition value. Takes the definition
    value as a parameter and returns a boolean indicating the type check
    result, `true` for success and `false` for failure.

`merge`

:   A function to merge multiple definitions values. Takes two
    parameters:

    *`loc`*

    :   The option path as a list of strings, e.g. `["boot" "loader
                 "grub" "enable"]`.

    *`defs`*

    :   The list of sets of defined `value` and `file` where the value
        was defined, e.g. `[ {
                 file = "/foo.nix"; value = 1; } { file = "/bar.nix"; value = 2 }
                 ]`. The `merge` function should return the merged value
        or throw an error in case the values are impossible or not meant
        to be merged.

`getSubOptions`

:   For composed types that can take a submodule as type parameter, this
    function generate sub-options documentation. It takes the current

Title: Custom Option Types: Definition and Parameters
Summary
This section elaborates on custom option types, detailing the `mkOptionType` function for their creation. It defines the required `name` parameter and optional parameters like `description`, `check` (a function to validate the value), `merge` (a function to merge multiple definitions), and `getSubOptions` (for generating sub-options documentation in composed types with submodule parameters). The `merge` parameter receives the option path (`loc`) and a list of defined values and their source files (`defs`).