Option types are a way to put constraints on the values a module option can take. Types are also responsible of how values are merged in case of multiple value definitions.
Basic types
Basic types are the simplest available types in the module system. Basic types include multiple string types that mainly differ in how definition merging is handled.
types.bool-
A boolean, its values can be
trueorfalse. All definitions must have the same value, after priorities. An error is thrown in case of a conflict. types.boolByOr-
A boolean, its values can be
trueorfalse. The result istrueif any of multiple definitions istrue. In other words, definitions are merged with the logical OR operator. types.path-
A filesystem path that starts with a slash. Even if derivations can be considered as paths, the more specific
types.packageshould be preferred. types.pathInStore-
A path that is contained in the Nix store. This can be a top-level store path like
pkgs.helloor a descendant like"${pkgs.hello}/bin/hello". types.externalPath-
A path that is not contained in the Nix store. Typical use cases are: secrets, password or any other external file.
types.pathWith{inStore?null,absolute?null}-
A filesystem path. Either a string or something that can be coerced to a string.
Parameters
inStore(Booleanornull, defaultnull)- Whether the path must be in the store (
true), must not be in the store (false), or it doesn’t matter (null) absolute(Booleanornull, defaultnull)- Whether the path must be absolute (
true), must not be absolute (false), or it doesn’t matter (null)
Behavior
pathWith { inStore = true; }is equivalent topathInStorepathWith { absolute = true; }is equivalent topathpathWith { inStore = false; absolute = true; }requires an absolute path that is not in the store. Useful for password files that shouldn’t be leaked into the store.
types.package-
A top-level store path. This can be an attribute set pointing to a store path, like a derivation or a flake input.
types.enuml-
One element of the list
l, e.g.types.enum [ "left" "right" ]. Multiple definitions cannot be merged.If you want to pair these values with more information, possibly of distinct types, consider using a sum type.
types.anything-
A type that accepts any value and recursively merges attribute sets together. This type is recommended when the option type is unknown.
types.raw-
A type which doesn’t do any checking, merging or nested evaluation. It accepts a single arbitrary value that is not recursed into, making it useful for values coming from outside the module system, such as package sets or arbitrary data. Options of this type are still evaluated according to priorities and conditionals, so
mkForce,mkIfand co. still work on the option value itself, but not for any value nested within it. This type should only be used when checking, merging and nested evaluation are not desirable. types.optionType-
The type of an option’s type. Its merging operation ensures that nested options have the correct file location annotated, and that if possible, multiple option definitions are correctly merged together. The main use case is as the type of the
_module.freeformTypeoption. types.optionDeclaration-
The type of a module system option declaration, as created by
lib.mkOption. This allows an option to hold another option declaration as its value, which can then be spliced into a module’soptionsattrset. Note that this only accepts option declarations, not evaluated options (i.e. options that have been processed byevalModulesand have avaluefield). types.attrs-
A free-form attribute set.
types.pkgs-
A type for the top level Nixpkgs package set.
Numeric types
types.int-
A signed integer.
types.ints.{s8, s16, s32}-
Signed integers with a fixed length (8, 16 or 32 bits). They go from −2^n/2 to 2^n/2−1 respectively (e.g.
−128to127for 8 bits). types.ints.unsigned-
An unsigned integer (that is >= 0).
types.ints.{u8, u16, u32}-
Unsigned integers with a fixed length (8, 16 or 32 bits). They go from 0 to 2^n−1 respectively (e.g.
0to255for 8 bits). types.ints.betweenlowest highest-
An integer between
lowestandhighest(both inclusive). types.ints.positive-
A positive integer (that is > 0).
types.port-
A port number. This type is an alias to
types.ints.u16. types.float-
A floating point number.
types.number-
Either a signed integer or a floating point number. No implicit conversion is done between the two types, and multiple equal definitions will only be merged if they have the same type.
types.numbers.betweenlowest highest-
An integer or floating point number between
lowestandhighest(both inclusive). types.numbers.nonnegative-
A nonnegative integer or floating point number (that is >= 0).
types.numbers.positive-
A positive integer or floating point number (that is > 0).
String types
types.str-
A string. Multiple definitions cannot be merged.
types.separatedStringsep-
A string. Multiple definitions are concatenated with
sep, e.g.types.separatedString "|". types.lines-
A string. Multiple definitions are concatenated with a new line
"\n". types.commas-
A string. Multiple definitions are concatenated with a comma
",". types.envVar-
A string. Multiple definitions are concatenated with a colon
":". types.strMatching-
A string matching a specific regular expression. Multiple definitions cannot be merged. The regular expression is processed using
builtins.match.
Specialised types
types.luaInline-
A string wrapped using
lib.mkLuaInline. Allows embedding lua expressions inline within generated lua. Multiple definitions cannot be merged.
Submodule types
Submodules are detailed in Submodule.
types.submoduleo-
A set of sub options
o.ocan 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 totypes.submoduleWith { modules = toList o; shorthandOnlyDefinesConfig = true; }. types.submoduleWith{modules,specialArgs? {},shorthandOnlyDefinesConfig? false }-
Like
types.submodule, but more flexible and with better defaults. It has parameters-
modulesA list of modules to use by default for this submodule type. This gets combined with all option definitions to build the final list of modules that will be included. -
specialArgsAn attribute set of extra arguments to be passed to the module functions. The option_module.argsshould be used instead for most arguments since it allows overriding.specialArgsshould only be used for arguments that can’t go through the module fixed-point, because of infinite recursion or other problems. An example is overriding thelibargument, becauselibitself is used to define_module.args, which makes using_module.argsto define it impossible. -
shorthandOnlyDefinesConfigWhether definitions of this type should default to theconfigsection of a module (see Example: Structure of NixOS Modules) if it is an attribute set. Enabling this only has a benefit when the submodule defines an option namedconfigoroptions. In such a case it would allow the option to be set withthe-submodule.config = "value"instead of requiringthe-submodule.config.config = "value". This is because only when modules don’t set theconfigoroptionskeys, all keys are interpreted as option definitions in theconfigsection. Enabling this option implicitly puts all attributes in theconfigsection.With this option enabled, defining a non-
configsection requires using a function:the-submodule = { ... }: { options = { ... }; }.
-
types.deferredModule-
Whereas
submodulerepresents an option tree,deferredModulerepresents a module value, such as a module file or a configuration.It can be set multiple times.
Module authors can use its value in
imports, insubmoduleWith‘smodulesor inevalModules’modulesparameter, among other places.Note that
importsmust be evaluated before the module fixpoint. Because of this, deferred modules can only be imported into “other” fixpoints, such as submodules.One use case for this type is the type of a “default” module that allow the user to affect all submodules in an
attrsOf submoduleat once. This is more convenient and discoverable than expecting the module user to type-merge with theattrsOf submoduleoption.
Union types
A union of types is a type such that a value is valid when it is valid for at least one of those types.
If some values are instances of more than one of the types, it is not possible to distinguish which type they are meant to be instances of. If that’s needed, consider using a sum type.
types.eithert1 t2-
Type
t1or typet2, e.g.with types; either int str. Multiple definitions cannot be merged.
types.oneOf[t1 t2… ]-
Type
t1or typet2and so forth, e.g.with types; oneOf [ int str bool ]. Multiple definitions cannot be merged. types.nullOrt-
nullor typet. Multiple definitions are merged according to typet.This is mostly equivalent to
either (enum [ null ]) t, butnullOrprovides anullfallback for attribute values withmkIf falsedefinitions inlazyAttrsOf (nullOr t), whereaseitherwould throw an error when the attribute is accessed.
Sum types
A sum type can be thought of, conceptually, as a types.enum where each valid item is paired with at least a type, through some value syntax.
Nix does not have a built-in syntax for this pairing of a label and a type or value, so sum types may be represented in multiple ways.
If the you’re interested in can be distinguished without a label, you may simplify your value syntax with a union type instead.
types.attrTag{ attr1 = option1; attr2 = option2; ... }-
An attribute set containing one attribute, whose name must be picked from the attribute set (
attr1, etc) and whose value consists of definitions that are valid for the corresponding option (option1, etc).This type appears in the documentation as attribute-tagged union.
Example:
{ lib, ... }:let inherit (lib) type mkOption;in {options.toyRouter.rules = mkOption {description = ''Rules for a fictional packet routing service.'';type = types.attrsOf (types.attrTag {bounce = mkOption {description = "Send back a packet explaining why it wasn't forwarded.";type = types.submodule {options.errorMessage = mkOption { … };};};forward = mkOption {description = "Forward the packet.";type = types.submodule {options.destination = mkOption { … };};};drop = types.mkOption {description = "Drop the packet without sending anything back.";type = types.submodule {};};});};config.toyRouter.rules = {http = {bounce = {errorMessage = "Unencrypted HTTP is banned. You must always use https://.";};};ssh = { drop = {}; };};}
Composed types
Composed types are types that take a type as parameter. listOf int and either int str are examples of composed types.
types.listOft-
A list of
ttype, e.g.types.listOf int. Multiple definitions are merged with list concatenation. types.attrsOft-
An attribute set of where all the values are of
ttype. Multiple definitions result in the joined attribute set. types.lazyAttrsOft-
An attribute set of where all the values are of
ttype. Multiple definitions result in the joined attribute set. This is the lazy version oftypes.attrsOf, allowing attributes to depend on each other. types.attrsWith{elemType,lazy? false,placeholder? “name” }-
An attribute set of where all the values are of
elemTypetype.Parameters
elemType(Required)- Specifies the type of the values contained in the attribute set.
lazy- Determines whether the attribute set is lazily evaluated. See:
types.lazyAttrsOf placeholder(String, default:name)- Placeholder string in documentation for the attribute names.
The default value
nameresults in the placeholder<name>
Behavior
-
attrsWith { elemType = t; }is equivalent toattrsOf t -
attrsWith { lazy = true; elemType = t; }is equivalent tolazyAttrsOf t -
attrsWith { placeholder = "id"; elemType = t; }Displays the option as
foo.<id>in the manual.
types.attrListOft-
An ordered list of single-attribute attribute sets, where each value is of
ttype. The output is always[ { name1 = value1; } { name2 = value2; } ... ].Definitions can be provided in two formats, which may be mixed via
lib.mkMerge,imports, etc:-
List format:
[ { a = 1; } { b = 2; } ]— each element must be a single-attribute attribute set. Elements may be wrapped inlib.mkOrder(orlib.mkBefore/lib.mkAfter) to control ordering; unwrapped elements use the default order priority. -
Attribute set format:
{ a = lib.mkOrder 100 1; b = 2; }— each name-value pair becomes a single-attribute attribute set in the output. Values may be wrapped inlib.mkOrder(orlib.mkBefore/lib.mkAfter) to control ordering. Values withoutlib.mkOrderuse the default priority.
Multiple definitions of the same option are concatenated and then sorted by priority. Entries at the same priority level preserve their definition order.
-
types.attrListWith{elemType,asAttrs? false,mergeAttrValues? _name: values: values }-
An ordered list of single-attribute attribute sets, where each value is of
elemTypetype.Parameters
elemType(Required)- Specifies the type of each value in the attribute list.
asAttrs- When
true, the option value is an attribute set instead of a list. Duplicate keys are merged usingmergeAttrValues. The ordered list is always available viavalueMeta.attrListValue. mergeAttrValues- A function
name: values: mergedValuethat controls how duplicate keys are combined whenasAttrs = true. This is passed as the callback tolib.zipAttrsWith. Thevalueslist is in order of priority. By default, all values are collected into a list.
Behavior
attrListWith { elemType = t; }is equivalent toattrListOf t
types.uniqt-
Ensures that type
tcannot be merged. It is used to ensure option definitions are provided only once. types.unique{ message = m }t-
Ensures that type
tcannot be merged. Prints the messagem, after the lineThe option <option path> is defined multiple times.and before a list of definition locations. types.coercedTofrom f to-
Type
toor typefromwhich will be coerced to typetousing functionfwhich takes an argument of typefromand return a value of typeto. Can be used to preserve backwards compatibility of an option if its type was changed. types.json-
A type representing JSON-compatible values. This includes
null, booleans, integers, floats, strings, paths, attribute sets, and lists. Attribute sets and lists can be arbitrarily nested and contain any JSON-compatible values. types.toml-
A type representing TOML-compatible values. This includes booleans, integers, floats, strings, paths, attribute sets, and lists. Attribute sets and lists can be arbitrarily nested and contain any TOML-compatible values.
Submodule
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.
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).
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).
Extending types
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
addCheckfunction (Example: Adding a type check), or to fully override the check function (Example: Overriding a type check). merge-
Function to merge the options values when multiple values are set. The function takes two parameters,
locthe option path as a list of strings, anddefsthe list of defined values as a list. It is possible to override a type merge function for custom needs.
Custom types
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,
truefor success andfalsefor 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
valueandfilewhere the value was defined, e.g.[ { file = "/foo.nix"; value = 1; } { file = "/bar.nix"; value = 2 } ]. Themergefunction 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 option prefix as a list and return the set of sub-options. Usually defined in a recursive manner by adding a term to the prefix, e.g.
prefix: elemType.getSubOptions (prefix ++ ["prefix"])where"prefix"is the newly added prefix. getSubModules-
For composed types that can take a submodule as type parameter, this function should return the type parameters submodules. If the type parameter is called
elemType, the function should just recursively look into submodules by returningelemType.getSubModules;. substSubModules-
For composed types that can take a submodule as type parameter, this function can be used to substitute the parameter of a submodule type. It takes a module as parameter and return the type with the submodule options substituted. It is usually defined as a type function call with a recursive call to
substSubModules, e.g for a typecomposedTypethat take anelemtypetype parameter, this function should be defined asm: composedType (elemType.substSubModules m). typeMerge-
A function to merge multiple type declarations. Takes the type to merge
functoras parameter. Anullreturn value means that type cannot be merged.f-
The type to merge
functor.
Note: There is a generic
defaultTypeMergethat work with most of value and composed types. functor-
An attribute set representing the type. It is used for type operations and has the following keys:
type-
The type function.
wrapped-
Holds the type parameter for composed types.
payload-
Holds the value parameter for value types. The types that have a
payloadare theenum,separatedStringandsubmoduletypes. binOp-
A binary operation that can merge the payloads of two same types. Defined as a function that take two payloads as parameters and return the payloads merged.