Home Explore Blog Models CI



nixpkgs

8th chunk of `nixos/doc/manual/development/option-types.section.md`
d3d55988e860605c924a14d0baa2dc396d19891beb5dd33800000001000007ee
    ::: {#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: Defining Custom Nix Option Types with `mkOptionType`
Summary
This chunk details the `merge` function's role in combining option values for existing types, explaining its `loc` and `defs` parameters. It then shifts to the creation of custom types using `mkOptionType`, emphasizing that `name` is the only required parameter. The section elaborates on other key parameters for custom types: `description` for documentation, `check` for validating definition values, `merge` for handling multiple definitions (with specific `loc` and `defs` parameters), and `getSubOptions` for generating sub-option documentation in composed types.