query
On this page

submodule

lib.types.submodule

Docs pulled from | This Revision | 14 minutes ago


Nixpkgs manual

types.submodule o

A set of sub options o. o can be an attribute set, a function returning an attribute set, or a path to a file containing such a value. Submodules are used in composed types to create modular options. This is equivalent to types.submoduleWith { modules = toList o; shorthandOnlyDefinesConfig = true; }.

submodule is a very powerful type that defines a set of sub-options that are handled like a separate module.

It takes a parameter o, that should be a set, or a function returning a set with an options key defining the sub-options. Submodule option definitions are type-checked accordingly to the options declarations. Of course, you can nest submodule option definitions for even higher modularity.

The option set can be defined directly (Example: Directly defined submodule) or as reference (Example: Submodule defined as a reference).

Note that even if your submodule’s options all have a default value, you will still need to provide a default value (e.g. an empty attribute set) if you want to allow users to leave it undefined.

Directly defined submodule

{
  options.mod = mkOption {
    description = "submodule example";
    type =
      with types;
      submodule {
        options = {
          foo = mkOption { type = int; };
          bar = mkOption { type = str; };
        };
      };
  };
}

Submodule defined as a reference

let
  modOptions = {
    options = {
      foo = mkOption { type = int; };
      bar = mkOption { type = int; };
    };
  };
in
{
  options.mod = mkOption {
    description = "submodule example";
    type = with types; submodule modOptions;
  };
}

The submodule type is especially interesting when used with composed types like attrsOf or listOf. When composed with listOf (Example: Declaration of a list of submodules), submodule allows multiple definitions of the submodule option set (Example: Definition of a list of submodules).

Declaration of a list of submodules

{
  options.mod = mkOption {
    description = "submodule example";
    type =
      with types;
      listOf (submodule {
        options = {
          foo = mkOption { type = int; };
          bar = mkOption { type = str; };
        };
      });
  };
}

Definition of a list of submodules

{
  config.mod = [
    {
      foo = 1;
      bar = "one";
    }
    {
      foo = 2;
      bar = "two";
    }
  ];
}

When composed with attrsOf (Example: Declaration of attribute sets of submodules), submodule allows multiple named definitions of the submodule option set (Example: Definition of attribute sets of submodules).

Declaration of attribute sets of submodules

{
  options.mod = mkOption {
    description = "submodule example";
    type =
      with types;
      attrsOf (submodule {
        options = {
          foo = mkOption { type = int; };
          bar = mkOption { type = str; };
        };
      });
  };
}

Definition of attribute sets of submodules

{
  config.mod.one = {
    foo = 1;
    bar = "one";
  };
  config.mod.two = {
    foo = 2;
    bar = "two";
  };
}

Noogle detected

Implementation

The following is the current implementation of this function.

submodule =
    modules:
    submoduleWith {
      shorthandOnlyDefinesConfig = true;
      modules = toList modules;
    };