Home Explore Blog CI



nixpkgs

7th chunk of `nixos/doc/manual/development/option-types.section.md`
a4026d4d51891587179e9c6bb78223b1d183df4fb04dae990000000100000bf7
    type = with types; listOf (submodule {
      options = {
        foo = mkOption {
          type = int;
        };
        bar = mkOption {
          type = str;
        };
      };
    });
  };
}
```
:::

::: {#ex-submodule-listof-definition .example}
### Definition of a list of submodules
```nix
{
  config.mod = [
    { foo = 1; bar = "one"; }
    { foo = 2; bar = "two"; }
  ];
}
```
:::

When composed with `attrsOf`
([Example: Declaration of attribute sets of submodules](#ex-submodule-attrsof-declaration)), `submodule` allows
multiple named definitions of the submodule option set
([Example: Definition of attribute sets of submodules](#ex-submodule-attrsof-definition)).

::: {#ex-submodule-attrsof-declaration .example}
### Declaration of attribute sets of submodules
```nix
{
  options.mod = mkOption {
    description = "submodule example";
    type = with types; attrsOf (submodule {
      options = {
        foo = mkOption {
          type = int;
        };
        bar = mkOption {
          type = str;
        };
      };
    });
  };
}
```
:::

::: {#ex-submodule-attrsof-definition .example}
### Definition of attribute sets of submodules
```nix
{
  config.mod.one = { foo = 1; bar = "one"; };
  config.mod.two = { foo = 2; bar = "two"; };
}
```
:::

## Extending types {#sec-option-types-extending}

Types are mainly characterized by their `check` and `merge` functions.

`check`

:   The function to type check the value. Takes a value as parameter and
    return a boolean. It is possible to extend a type check with the
    `addCheck` function ([Example: Adding a type check](#ex-extending-type-check-1)),
    or to fully override the check function
    ([Example: Overriding a type check](#ex-extending-type-check-2)).

    ::: {#ex-extending-type-check-1 .example}
    ### Adding a type check

    ```nix
    {
      byte = mkOption {
        description = "An integer between 0 and 255.";
        type = types.addCheck types.int (x: x >= 0 && x <= 255);
      };
    }
    ```
    :::

    ::: {#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

Title: Submodule Examples and Extending/Customizing Option Types
Summary
The text provides examples of using `submodule` with `attrsOf` for multiple named submodule definitions. It then describes how to extend existing option types by modifying or overriding their `check` function, providing examples of adding and overriding type checks. Finally, it introduces the creation of custom types using `mkOptionType`, recommending familiarity with `types.nix` for complex features like submodule handling, and highlights the required `name` parameter along with the optional `description`.