Home Explore Blog Models CI



nixpkgs

7th chunk of `nixos/doc/manual/development/option-types.section.md`
1b6b82cbb5fa107a523d0d13a1ad0dda4dfb32efbbbc32e00000000100000bf2
      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 Composition with `attrsOf`, Type Extension, and Custom Type Creation
Summary
This chunk concludes the discussion on the `submodule` type, specifically demonstrating its composition with `attrsOf` to allow for multiple named definitions of submodule option sets through practical code examples. It then introduces the concept of extending existing types, highlighting the `check` and `merge` functions as core characteristics. The `check` function, responsible for type validation, can be extended using `addCheck` or entirely overridden for custom validation logic. The `merge` function handles combining option values when multiple definitions exist, and can also be overridden. Finally, the chunk introduces `mkOptionType` for creating custom types, noting its complexity and recommending familiarity with `types.nix` code, and outlines the `name` and `description` parameters for custom types.