···
+
<section xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink" xml:id="sec-option-declarations">
+
<title>Option Declarations</title>
+
An option declaration specifies the name, type and description of a
+
NixOS configuration option. It is invalid to define an option that
+
hasn’t been declared in any module. An option declaration generally
+
<programlisting language="bash">
+
type = type specification;
+
default = default value;
+
example = example value;
+
description = "Description for use in the NixOS manual.";
+
The attribute names within the <literal>name</literal> attribute
+
path must be camel cased in general but should, as an exception,
+
<link xlink:href="https://nixos.org/nixpkgs/manual/#sec-package-naming">
+
package attribute name</link> when referencing a Nixpkgs package.
+
For example, the option
+
<literal>services.nix-serve.bindAddress</literal> references the
+
<literal>nix-serve</literal> Nixpkgs package.
+
The function <literal>mkOption</literal> accepts the following
+
<literal>type</literal>
+
The type of the option (see
+
<xref linkend="sec-option-types" />). It may be omitted, but
+
that’s not advisable since it may lead to errors that are hard
+
<literal>default</literal>
+
The default value used if no value is defined by any module. A
+
default is not required; but if a default is not given, then
+
users of the module will have to define the value of the
+
option, otherwise an error will be thrown.
+
<literal>example</literal>
+
An example value that will be shown in the NixOS manual.
+
<literal>description</literal>
+
A textual description of the option, in DocBook format, that
+
will be included in the NixOS manual.
+
<section xml:id="sec-option-declarations-eot">
+
<title>Extensible Option Types</title>
+
Extensible option types is a feature that allow to extend certain
+
types declaration through multiple module files. This feature only
+
work with a restricted set of types, namely
+
<literal>enum</literal> and <literal>submodules</literal> and any
+
composed forms of them.
+
Extensible option types can be used for <literal>enum</literal>
+
options that affects multiple modules, or as an alternative to
+
related <literal>enable</literal> options.
+
As an example, we will take the case of display managers. There is
+
a central display manager module for generic display manager
+
options and a module file per display manager backend (sddm, gdm
+
There are two approach to this module structure:
+
Managing the display managers independently by adding an
+
enable option to every display manager module backend. (NixOS)
+
Managing the display managers in the central module by adding
+
an option to select which display manager backend to use.
+
Both approaches have problems.
+
Making backends independent can quickly become hard to manage. For
+
display managers, there can be only one enabled at a time, but the
+
type system can not enforce this restriction as there is no
+
relation between each backend <literal>enable</literal> option. As
+
a result, this restriction has to be done explicitely by adding
+
assertions in each display manager backend module.
+
On the other hand, managing the display managers backends in the
+
central module will require to change the central module option
+
every time a new backend is added or removed.
+
By using extensible option types, it is possible to create a
+
placeholder option in the central module
+
(<link linkend="ex-option-declaration-eot-service">Example:
+
Extensible type placeholder in the service module</link>), and to
+
extend it in each backend module
+
(<link linkend="ex-option-declaration-eot-backend-gdm">Example:
+
<literal>services.xserver.displayManager.enable</literal> in the
+
<literal>gdm</literal> module</link>,
+
<link linkend="ex-option-declaration-eot-backend-sddm">Example:
+
<literal>services.xserver.displayManager.enable</literal> in the
+
<literal>sddm</literal> module</link>).
+
As a result, <literal>displayManager.enable</literal> option
+
values can be added without changing the main service module file
+
and the type system automatically enforce that there can only be a
+
single display manager enabled.
+
<anchor xml:id="ex-option-declaration-eot-service" />
+
<emphasis role="strong">Example: Extensible type placeholder in
+
the service module</emphasis>
+
<programlisting language="bash">
+
services.xserver.displayManager.enable = mkOption {
+
description = "Display manager to use";
+
type = with types; nullOr (enum [ ]);
+
<anchor xml:id="ex-option-declaration-eot-backend-gdm" />
+
<emphasis role="strong">Example: Extending
+
<literal>services.xserver.displayManager.enable</literal> in the
+
<literal>gdm</literal> module</emphasis>
+
<programlisting language="bash">
+
services.xserver.displayManager.enable = mkOption {
+
type = with types; nullOr (enum [ "gdm" ]);
+
<anchor xml:id="ex-option-declaration-eot-backend-sddm" />
+
<emphasis role="strong">Example: Extending
+
<literal>services.xserver.displayManager.enable</literal> in the
+
<literal>sddm</literal> module</emphasis>
+
<programlisting language="bash">
+
services.xserver.displayManager.enable = mkOption {
+
type = with types; nullOr (enum [ "sddm" ]);
+
The placeholder declaration is a standard
+
<literal>mkOption</literal> declaration, but it is important that
+
extensible option declarations only use the
+
<literal>type</literal> argument.
+
Extensible option types work with any of the composed variants of
+
<literal>enum</literal> such as
+
<literal>with types; nullOr (enum [ "foo" "bar" ])</literal>
+
<literal>with types; listOf (enum [ "foo" "bar" ])</literal>.