at 17.09-beta 6.0 kB view raw
1<section xmlns="http://docbook.org/ns/docbook" 2 xmlns:xlink="http://www.w3.org/1999/xlink" 3 xmlns:xi="http://www.w3.org/2001/XInclude" 4 version="5.0" 5 xml:id="sec-option-declarations"> 6 7<title>Option Declarations</title> 8 9<para>An option declaration specifies the name, type and description 10of a NixOS configuration option. It is invalid to define an option 11that hasn’t been declared in any module. An option declaration 12generally looks like this: 13 14<programlisting> 15options = { 16 <replaceable>name</replaceable> = mkOption { 17 type = <replaceable>type specification</replaceable>; 18 default = <replaceable>default value</replaceable>; 19 example = <replaceable>example value</replaceable>; 20 description = "<replaceable>Description for use in the NixOS manual.</replaceable>"; 21 }; 22}; 23</programlisting> 24 25</para> 26 27<para>The function <varname>mkOption</varname> accepts the following arguments. 28 29<variablelist> 30 31 <varlistentry> 32 <term><varname>type</varname></term> 33 <listitem> 34 <para>The type of the option (see <xref linkend='sec-option-types' />). 35 It may be omitted, but that’s not advisable since it may lead to errors 36 that are hard to diagnose.</para> 37 </listitem> 38 </varlistentry> 39 40 <varlistentry> 41 <term><varname>default</varname></term> 42 <listitem> 43 <para>The default value used if no value is defined by any 44 module. A default is not required; but if a default is not given, 45 then users of the module will have to define the value of the 46 option, otherwise an error will be thrown.</para> 47 </listitem> 48 </varlistentry> 49 50 <varlistentry> 51 <term><varname>example</varname></term> 52 <listitem> 53 <para>An example value that will be shown in the NixOS manual.</para> 54 </listitem> 55 </varlistentry> 56 57 <varlistentry> 58 <term><varname>description</varname></term> 59 <listitem> 60 <para>A textual description of the option, in DocBook format, 61 that will be included in the NixOS manual.</para> 62 </listitem> 63 </varlistentry> 64 65</variablelist> 66 67</para> 68 69<section xml:id="sec-option-declarations-eot"><title>Extensible Option 70 Types</title> 71 72 <para>Extensible option types is a feature that allow to extend certain types 73 declaration through multiple module files. 74 This feature only work with a restricted set of types, namely 75 <literal>enum</literal> and <literal>submodules</literal> and any composed 76 forms of them.</para> 77 78 <para>Extensible option types can be used for <literal>enum</literal> options 79 that affects multiple modules, or as an alternative to related 80 <literal>enable</literal> options.</para> 81 82 <para>As an example, we will take the case of display managers. There is a 83 central display manager module for generic display manager options and a 84 module file per display manager backend (slim, sddm, gdm ...). 85 </para> 86 87 <para>There are two approach to this module structure: 88 89 <itemizedlist> 90 <listitem><para>Managing the display managers independently by adding an 91 enable option to every display manager module backend. (NixOS)</para> 92 </listitem> 93 <listitem><para>Managing the display managers in the central module by 94 adding an option to select which display manager backend to use.</para> 95 </listitem> 96 </itemizedlist> 97 </para> 98 99 <para>Both approaches have problems.</para> 100 101 <para>Making backends independent can quickly become hard to manage. For 102 display managers, there can be only one enabled at a time, but the type 103 system can not enforce this restriction as there is no relation between 104 each backend <literal>enable</literal> option. As a result, this restriction 105 has to be done explicitely by adding assertions in each display manager 106 backend module.</para> 107 108 <para>On the other hand, managing the display managers backends in the 109 central module will require to change the central module option every time 110 a new backend is added or removed.</para> 111 112 <para>By using extensible option types, it is possible to create a placeholder 113 option in the central module (<xref linkend='ex-option-declaration-eot-service' 114 />), and to extend it in each backend module (<xref 115 linkend='ex-option-declaration-eot-backend-slim' />, <xref 116 linkend='ex-option-declaration-eot-backend-sddm' />).</para> 117 118 <para>As a result, <literal>displayManager.enable</literal> option values can 119 be added without changing the main service module file and the type system 120 automatically enforce that there can only be a single display manager 121 enabled.</para> 122 123<example xml:id='ex-option-declaration-eot-service'><title>Extensible type 124 placeholder in the service module</title> 125<screen> 126services.xserver.displayManager.enable = mkOption { 127 description = "Display manager to use"; 128 type = with types; nullOr (enum [ ]); 129};</screen></example> 130 131<example xml:id='ex-option-declaration-eot-backend-slim'><title>Extending 132 <literal>services.xserver.displayManager.enable</literal> in the 133 <literal>slim</literal> module</title> 134<screen> 135services.xserver.displayManager.enable = mkOption { 136 type = with types; nullOr (enum [ "slim" ]); 137};</screen></example> 138 139<example xml:id='ex-option-declaration-eot-backend-sddm'><title>Extending 140 <literal>services.foo.backend</literal> in the <literal>sddm</literal> 141 module</title> 142<screen> 143services.xserver.displayManager.enable = mkOption { 144 type = with types; nullOr (enum [ "sddm" ]); 145};</screen></example> 146 147<para>The placeholder declaration is a standard <literal>mkOption</literal> 148 declaration, but it is important that extensible option declarations only use 149 the <literal>type</literal> argument.</para> 150 151<para>Extensible option types work with any of the composed variants of 152 <literal>enum</literal> such as 153 <literal>with types; nullOr (enum [ "foo" "bar" ])</literal> 154 or <literal>with types; listOf (enum [ "foo" "bar" ])</literal>.</para> 155 156</section> 157</section>