summary refs log tree commit diff
path: root/nixos/doc/manual/from_md/development/option-types.section.xml
blob: 4447292927021d5427219ffe16c204067e13f349 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
<section xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink" xml:id="sec-option-types">
  <title>Options Types</title>
  <para>
    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.
  </para>
  <section xml:id="sec-option-types-basic">
    <title>Basic Types</title>
    <para>
      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.
    </para>
    <variablelist>
      <varlistentry>
        <term>
          <literal>types.bool</literal>
        </term>
        <listitem>
          <para>
            A boolean, its values can be <literal>true</literal> or
            <literal>false</literal>.
          </para>
        </listitem>
      </varlistentry>
      <varlistentry>
        <term>
          <literal>types.path</literal>
        </term>
        <listitem>
          <para>
            A filesystem path is anything that starts with a slash when
            coerced to a string. Even if derivations can be considered
            as paths, the more specific <literal>types.package</literal>
            should be preferred.
          </para>
        </listitem>
      </varlistentry>
      <varlistentry>
        <term>
          <literal>types.package</literal>
        </term>
        <listitem>
          <para>
            A top-level store path. This can be an attribute set
            pointing to a store path, like a derivation or a flake
            input.
          </para>
        </listitem>
      </varlistentry>
      <varlistentry>
        <term>
          <literal>types.anything</literal>
        </term>
        <listitem>
          <para>
            A type that accepts any value and recursively merges
            attribute sets together. This type is recommended when the
            option type is unknown.
          </para>
          <anchor xml:id="ex-types-anything" />
          <para>
            <emphasis role="strong">Example:
            <literal>types.anything</literal> Example</emphasis>
          </para>
          <para>
            Two definitions of this type like
          </para>
          <programlisting language="bash">
{
  str = lib.mkDefault &quot;foo&quot;;
  pkg.hello = pkgs.hello;
  fun.fun = x: x + 1;
}
</programlisting>
          <programlisting language="bash">
{
  str = lib.mkIf true &quot;bar&quot;;
  pkg.gcc = pkgs.gcc;
  fun.fun = lib.mkForce (x: x + 2);
}
</programlisting>
          <para>
            will get merged to
          </para>
          <programlisting language="bash">
{
  str = &quot;bar&quot;;
  pkg.gcc = pkgs.gcc;
  pkg.hello = pkgs.hello;
  fun.fun = x: x + 2;
}
</programlisting>
        </listitem>
      </varlistentry>
      <varlistentry>
        <term>
          <literal>types.raw</literal>
        </term>
        <listitem>
          <para>
            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 <literal>mkForce</literal>,
            <literal>mkIf</literal> and 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.
          </para>
        </listitem>
      </varlistentry>
      <varlistentry>
        <term>
          <literal>types.optionType</literal>
        </term>
        <listitem>
          <para>
            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 <literal>_module.freeformType</literal> option.
          </para>
        </listitem>
      </varlistentry>
      <varlistentry>
        <term>
          <literal>types.attrs</literal>
        </term>
        <listitem>
          <para>
            A free-form attribute set.
          </para>
          <warning>
            <para>
              This type will be deprecated in the future because it
              doesn't recurse into attribute sets, silently drops
              earlier attribute definitions, and doesn't discharge
              <literal>lib.mkDefault</literal>,
              <literal>lib.mkIf</literal> and co. For allowing arbitrary
              attribute sets, prefer
              <literal>types.attrsOf types.anything</literal> instead
              which doesn't have these problems.
            </para>
          </warning>
        </listitem>
      </varlistentry>
    </variablelist>
    <para>
      Integer-related types:
    </para>
    <variablelist>
      <varlistentry>
        <term>
          <literal>types.int</literal>
        </term>
        <listitem>
          <para>
            A signed integer.
          </para>
        </listitem>
      </varlistentry>
      <varlistentry>
        <term>
          <literal>types.ints.{s8, s16, s32}</literal>
        </term>
        <listitem>
          <para>
            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.
            <literal>−128</literal> to <literal>127</literal> for 8
            bits).
          </para>
        </listitem>
      </varlistentry>
      <varlistentry>
        <term>
          <literal>types.ints.unsigned</literal>
        </term>
        <listitem>
          <para>
            An unsigned integer (that is &gt;= 0).
          </para>
        </listitem>
      </varlistentry>
      <varlistentry>
        <term>
          <literal>types.ints.{u8, u16, u32}</literal>
        </term>
        <listitem>
          <para>
            Unsigned integers with a fixed length (8, 16 or 32 bits).
            They go from 0 to 2^n−1 respectively (e.g.
            <literal>0</literal> to <literal>255</literal> for 8 bits).
          </para>
        </listitem>
      </varlistentry>
      <varlistentry>
        <term>
          <literal>types.ints.positive</literal>
        </term>
        <listitem>
          <para>
            A positive integer (that is &gt; 0).
          </para>
        </listitem>
      </varlistentry>
      <varlistentry>
        <term>
          <literal>types.port</literal>
        </term>
        <listitem>
          <para>
            A port number. This type is an alias to
            <literal>types.ints.u16</literal>.
          </para>
        </listitem>
      </varlistentry>
    </variablelist>
    <para>
      String-related types:
    </para>
    <variablelist>
      <varlistentry>
        <term>
          <literal>types.str</literal>
        </term>
        <listitem>
          <para>
            A string. Multiple definitions cannot be merged.
          </para>
        </listitem>
      </varlistentry>
      <varlistentry>
        <term>
          <literal>types.lines</literal>
        </term>
        <listitem>
          <para>
            A string. Multiple definitions are concatenated with a new
            line <literal>&quot;\n&quot;</literal>.
          </para>
        </listitem>
      </varlistentry>
      <varlistentry>
        <term>
          <literal>types.commas</literal>
        </term>
        <listitem>
          <para>
            A string. Multiple definitions are concatenated with a comma
            <literal>&quot;,&quot;</literal>.
          </para>
        </listitem>
      </varlistentry>
      <varlistentry>
        <term>
          <literal>types.envVar</literal>
        </term>
        <listitem>
          <para>
            A string. Multiple definitions are concatenated with a
            collon <literal>&quot;:&quot;</literal>.
          </para>
        </listitem>
      </varlistentry>
      <varlistentry>
        <term>
          <literal>types.strMatching</literal>
        </term>
        <listitem>
          <para>
            A string matching a specific regular expression. Multiple
            definitions cannot be merged. The regular expression is
            processed using <literal>builtins.match</literal>.
          </para>
        </listitem>
      </varlistentry>
    </variablelist>
  </section>
  <section xml:id="sec-option-types-value">
    <title>Value Types</title>
    <para>
      Value types are types that take a value parameter.
    </para>
    <variablelist>
      <varlistentry>
        <term>
          <literal>types.enum</literal>
          <emphasis><literal>l</literal></emphasis>
        </term>
        <listitem>
          <para>
            One element of the list
            <emphasis><literal>l</literal></emphasis>, e.g.
            <literal>types.enum [ &quot;left&quot; &quot;right&quot; ]</literal>.
            Multiple definitions cannot be merged.
          </para>
        </listitem>
      </varlistentry>
      <varlistentry>
        <term>
          <literal>types.separatedString</literal>
          <emphasis><literal>sep</literal></emphasis>
        </term>
        <listitem>
          <para>
            A string with a custom separator
            <emphasis><literal>sep</literal></emphasis>, e.g.
            <literal>types.separatedString &quot;|&quot;</literal>.
          </para>
        </listitem>
      </varlistentry>
      <varlistentry>
        <term>
          <literal>types.ints.between</literal>
          <emphasis><literal>lowest highest</literal></emphasis>
        </term>
        <listitem>
          <para>
            An integer between
            <emphasis><literal>lowest</literal></emphasis> and
            <emphasis><literal>highest</literal></emphasis> (both
            inclusive). Useful for creating types like
            <literal>types.port</literal>.
          </para>
        </listitem>
      </varlistentry>
      <varlistentry>
        <term>
          <literal>types.submodule</literal>
          <emphasis><literal>o</literal></emphasis>
        </term>
        <listitem>
          <para>
            A set of sub options
            <emphasis><literal>o</literal></emphasis>.
            <emphasis><literal>o</literal></emphasis> 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
            <literal>types.submoduleWith { modules = toList o; shorthandOnlyDefinesConfig = true; }</literal>.
            Submodules are detailed in
            <link linkend="section-option-types-submodule">Submodule</link>.
          </para>
        </listitem>
      </varlistentry>
      <varlistentry>
        <term>
          <literal>types.submoduleWith</literal> {
          <emphasis><literal>modules</literal></emphasis>,
          <emphasis><literal>specialArgs</literal></emphasis> ? {},
          <emphasis><literal>shorthandOnlyDefinesConfig</literal></emphasis>
          ? false }
        </term>
        <listitem>
          <para>
            Like <literal>types.submodule</literal>, but more flexible
            and with better defaults. It has parameters
          </para>
          <itemizedlist>
            <listitem>
              <para>
                <emphasis><literal>modules</literal></emphasis> A 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.
              </para>
              <note>
                <para>
                  Only options defined with this argument are included
                  in rendered documentation.
                </para>
              </note>
            </listitem>
            <listitem>
              <para>
                <emphasis><literal>specialArgs</literal></emphasis> An
                attribute set of extra arguments to be passed to the
                module functions. The option
                <literal>_module.args</literal> should be used instead
                for most arguments since it allows overriding.
                <emphasis><literal>specialArgs</literal></emphasis>
                should 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 the
                <literal>lib</literal> argument, because
                <literal>lib</literal> itself is used to define
                <literal>_module.args</literal>, which makes using
                <literal>_module.args</literal> to define it impossible.
              </para>
            </listitem>
            <listitem>
              <para>
                <emphasis><literal>shorthandOnlyDefinesConfig</literal></emphasis>
                Whether definitions of this type should default to the
                <literal>config</literal> section of a module (see
                <link linkend="ex-module-syntax">Example: Structure of
                NixOS Modules</link>) if it is an attribute set.
                Enabling this only has a benefit when the submodule
                defines an option named <literal>config</literal> or
                <literal>options</literal>. In such a case it would
                allow the option to be set with
                <literal>the-submodule.config = &quot;value&quot;</literal>
                instead of requiring
                <literal>the-submodule.config.config = &quot;value&quot;</literal>.
                This is because only when modules
                <emphasis>don't</emphasis> set the
                <literal>config</literal> or <literal>options</literal>
                keys, all keys are interpreted as option definitions in
                the <literal>config</literal> section. Enabling this
                option implicitly puts all attributes in the
                <literal>config</literal> section.
              </para>
              <para>
                With this option enabled, defining a
                non-<literal>config</literal> section requires using a
                function:
                <literal>the-submodule = { ... }: { options = { ... }; }</literal>.
              </para>
            </listitem>
          </itemizedlist>
        </listitem>
      </varlistentry>
    </variablelist>
  </section>
  <section xml:id="sec-option-types-composed">
    <title>Composed Types</title>
    <para>
      Composed types are types that take a type as parameter.
      <literal>listOf int</literal> and
      <literal>either int str</literal> are examples of composed types.
    </para>
    <variablelist>
      <varlistentry>
        <term>
          <literal>types.listOf</literal>
          <emphasis><literal>t</literal></emphasis>
        </term>
        <listitem>
          <para>
            A list of <emphasis><literal>t</literal></emphasis> type,
            e.g. <literal>types.listOf int</literal>. Multiple
            definitions are merged with list concatenation.
          </para>
        </listitem>
      </varlistentry>
      <varlistentry>
        <term>
          <literal>types.attrsOf</literal>
          <emphasis><literal>t</literal></emphasis>
        </term>
        <listitem>
          <para>
            An attribute set of where all the values are of
            <emphasis><literal>t</literal></emphasis> type. Multiple
            definitions result in the joined attribute set.
          </para>
          <note>
            <para>
              This type is <emphasis>strict</emphasis> in its values,
              which in turn means attributes cannot depend on other
              attributes. See <literal> types.lazyAttrsOf</literal> for
              a lazy version.
            </para>
          </note>
        </listitem>
      </varlistentry>
      <varlistentry>
        <term>
          <literal>types.lazyAttrsOf</literal>
          <emphasis><literal>t</literal></emphasis>
        </term>
        <listitem>
          <para>
            An attribute set of where all the values are of
            <emphasis><literal>t</literal></emphasis> type. Multiple
            definitions result in the joined attribute set. This is the
            lazy version of <literal>types.attrsOf </literal>, allowing
            attributes to depend on each other.
          </para>
          <warning>
            <para>
              This version does not fully support conditional
              definitions! With an option <literal>foo</literal> of this
              type and a definition
              <literal>foo.attr = lib.mkIf false 10</literal>,
              evaluating <literal>foo ? attr</literal> will return
              <literal>true</literal> even though it should be false.
              Accessing the value will then throw an error. For types
              <emphasis><literal>t</literal></emphasis> that have an
              <literal>emptyValue</literal> defined, that value will be
              returned instead of throwing an error. So if the type of
              <literal>foo.attr</literal> was
              <literal>lazyAttrsOf (nullOr int)</literal>,
              <literal>null</literal> would be returned instead for the
              same <literal>mkIf false</literal> definition.
            </para>
          </warning>
        </listitem>
      </varlistentry>
      <varlistentry>
        <term>
          <literal>types.nullOr</literal>
          <emphasis><literal>t</literal></emphasis>
        </term>
        <listitem>
          <para>
            <literal>null</literal> or type
            <emphasis><literal>t</literal></emphasis>. Multiple
            definitions are merged according to type
            <emphasis><literal>t</literal></emphasis>.
          </para>
        </listitem>
      </varlistentry>
      <varlistentry>
        <term>
          <literal>types.uniq</literal>
          <emphasis><literal>t</literal></emphasis>
        </term>
        <listitem>
          <para>
            Ensures that type <emphasis><literal>t</literal></emphasis>
            cannot be merged. It is used to ensure option definitions
            are declared only once.
          </para>
        </listitem>
      </varlistentry>
      <varlistentry>
        <term>
          <literal>types.unique</literal>
          <literal>{ message = m }</literal>
          <emphasis><literal>t</literal></emphasis>
        </term>
        <listitem>
          <para>
            Ensures that type <emphasis><literal>t</literal></emphasis>
            cannot be merged. Prints the message
            <emphasis><literal>m</literal></emphasis>, after the line
            <literal>The option &lt;option path&gt; is defined multiple times.</literal>
            and before a list of definition locations.
          </para>
        </listitem>
      </varlistentry>
      <varlistentry>
        <term>
          <literal>types.either</literal>
          <emphasis><literal>t1 t2</literal></emphasis>
        </term>
        <listitem>
          <para>
            Type <emphasis><literal>t1</literal></emphasis> or type
            <emphasis><literal>t2</literal></emphasis>, e.g.
            <literal>with types; either int str</literal>. Multiple
            definitions cannot be merged.
          </para>
        </listitem>
      </varlistentry>
      <varlistentry>
        <term>
          <literal>types.oneOf</literal> [
          <emphasis><literal>t1 t2</literal></emphasis> ... ]
        </term>
        <listitem>
          <para>
            Type <emphasis><literal>t1</literal></emphasis> or type
            <emphasis><literal>t2</literal></emphasis> and so forth,
            e.g. <literal>with types; oneOf [ int str bool ]</literal>.
            Multiple definitions cannot be merged.
          </para>
        </listitem>
      </varlistentry>
      <varlistentry>
        <term>
          <literal>types.coercedTo</literal>
          <emphasis><literal>from f to</literal></emphasis>
        </term>
        <listitem>
          <para>
            Type <emphasis><literal>to</literal></emphasis> or type
            <emphasis><literal>from</literal></emphasis> which will be
            coerced to type <emphasis><literal>to</literal></emphasis>
            using function <emphasis><literal>f</literal></emphasis>
            which takes an argument of type
            <emphasis><literal>from</literal></emphasis> and return a
            value of type <emphasis><literal>to</literal></emphasis>.
            Can be used to preserve backwards compatibility of an option
            if its type was changed.
          </para>
        </listitem>
      </varlistentry>
    </variablelist>
  </section>
  <section xml:id="section-option-types-submodule">
    <title>Submodule</title>
    <para>
      <literal>submodule</literal> is a very powerful type that defines
      a set of sub-options that are handled like a separate module.
    </para>
    <para>
      It takes a parameter <emphasis><literal>o</literal></emphasis>,
      that should be a set, or a function returning a set with an
      <literal>options</literal> key defining the sub-options. Submodule
      option definitions are type-checked accordingly to the
      <literal>options</literal> declarations. Of course, you can nest
      submodule option definitons for even higher modularity.
    </para>
    <para>
      The option set can be defined directly
      (<link linkend="ex-submodule-direct">Example: Directly defined
      submodule</link>) or as reference
      (<link linkend="ex-submodule-reference">Example: Submodule defined
      as a reference</link>).
    </para>
    <anchor xml:id="ex-submodule-direct" />
    <para>
      <emphasis role="strong">Example: Directly defined
      submodule</emphasis>
    </para>
    <programlisting language="bash">
options.mod = mkOption {
  description = &quot;submodule example&quot;;
  type = with types; submodule {
    options = {
      foo = mkOption {
        type = int;
      };
      bar = mkOption {
        type = str;
      };
    };
  };
};
</programlisting>
    <anchor xml:id="ex-submodule-reference" />
    <para>
      <emphasis role="strong">Example: Submodule defined as a
      reference</emphasis>
    </para>
    <programlisting language="bash">
let
  modOptions = {
    options = {
      foo = mkOption {
        type = int;
      };
      bar = mkOption {
        type = int;
      };
    };
  };
in
options.mod = mkOption {
  description = &quot;submodule example&quot;;
  type = with types; submodule modOptions;
};
</programlisting>
    <para>
      The <literal>submodule</literal> type is especially interesting
      when used with composed types like <literal>attrsOf</literal> or
      <literal>listOf</literal>. When composed with
      <literal>listOf</literal>
      (<link linkend="ex-submodule-listof-declaration">Example:
      Declaration of a list of submodules</link>),
      <literal>submodule</literal> allows multiple definitions of the
      submodule option set
      (<link linkend="ex-submodule-listof-definition">Example:
      Definition of a list of submodules</link>).
    </para>
    <anchor xml:id="ex-submodule-listof-declaration" />
    <para>
      <emphasis role="strong">Example: Declaration of a list of
      submodules</emphasis>
    </para>
    <programlisting language="bash">
options.mod = mkOption {
  description = &quot;submodule example&quot;;
  type = with types; listOf (submodule {
    options = {
      foo = mkOption {
        type = int;
      };
      bar = mkOption {
        type = str;
      };
    };
  });
};
</programlisting>
    <anchor xml:id="ex-submodule-listof-definition" />
    <para>
      <emphasis role="strong">Example: Definition of a list of
      submodules</emphasis>
    </para>
    <programlisting language="bash">
config.mod = [
  { foo = 1; bar = &quot;one&quot;; }
  { foo = 2; bar = &quot;two&quot;; }
];
</programlisting>
    <para>
      When composed with <literal>attrsOf</literal>
      (<link linkend="ex-submodule-attrsof-declaration">Example:
      Declaration of attribute sets of submodules</link>),
      <literal>submodule</literal> allows multiple named definitions of
      the submodule option set
      (<link linkend="ex-submodule-attrsof-definition">Example:
      Definition of attribute sets of submodules</link>).
    </para>
    <anchor xml:id="ex-submodule-attrsof-declaration" />
    <para>
      <emphasis role="strong">Example: Declaration of attribute sets of
      submodules</emphasis>
    </para>
    <programlisting language="bash">
options.mod = mkOption {
  description = &quot;submodule example&quot;;
  type = with types; attrsOf (submodule {
    options = {
      foo = mkOption {
        type = int;
      };
      bar = mkOption {
        type = str;
      };
    };
  });
};
</programlisting>
    <anchor xml:id="ex-submodule-attrsof-definition" />
    <para>
      <emphasis role="strong">Example: Definition of attribute sets of
      submodules</emphasis>
    </para>
    <programlisting language="bash">
config.mod.one = { foo = 1; bar = &quot;one&quot;; };
config.mod.two = { foo = 2; bar = &quot;two&quot;; };
</programlisting>
  </section>
  <section xml:id="sec-option-types-extending">
    <title>Extending types</title>
    <para>
      Types are mainly characterized by their <literal>check</literal>
      and <literal>merge</literal> functions.
    </para>
    <variablelist>
      <varlistentry>
        <term>
          <literal>check</literal>
        </term>
        <listitem>
          <para>
            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 <literal>addCheck</literal> function
            (<link linkend="ex-extending-type-check-1">Example: Adding a
            type check</link>), or to fully override the check function
            (<link linkend="ex-extending-type-check-2">Example:
            Overriding a type check</link>).
          </para>
          <anchor xml:id="ex-extending-type-check-1" />
          <para>
            <emphasis role="strong">Example: Adding a type
            check</emphasis>
          </para>
          <programlisting language="bash">
byte = mkOption {
  description = &quot;An integer between 0 and 255.&quot;;
  type = types.addCheck types.int (x: x &gt;= 0 &amp;&amp; x &lt;= 255);
};
</programlisting>
          <anchor xml:id="ex-extending-type-check-2" />
          <para>
            <emphasis role="strong">Example: Overriding a type
            check</emphasis>
          </para>
          <programlisting language="bash">
nixThings = mkOption {
  description = &quot;words that start with 'nix'&quot;;
  type = types.str // {
    check = (x: lib.hasPrefix &quot;nix&quot; x)
  };
};
</programlisting>
        </listitem>
      </varlistentry>
      <varlistentry>
        <term>
          <literal>merge</literal>
        </term>
        <listitem>
          <para>
            Function to merge the options values when multiple values
            are set. The function takes two parameters,
            <literal>loc</literal> the option path as a list of strings,
            and <literal>defs</literal> the list of defined values as a
            list. It is possible to override a type merge function for
            custom needs.
          </para>
        </listitem>
      </varlistentry>
    </variablelist>
  </section>
  <section xml:id="sec-option-types-custom">
    <title>Custom Types</title>
    <para>
      Custom types can be created with the
      <literal>mkOptionType</literal> function. As type creation
      includes some more complex topics such as submodule handling, it
      is recommended to get familiar with <literal>types.nix</literal>
      code before creating a new type.
    </para>
    <para>
      The only required parameter is <literal>name</literal>.
    </para>
    <variablelist>
      <varlistentry>
        <term>
          <literal>name</literal>
        </term>
        <listitem>
          <para>
            A string representation of the type function name.
          </para>
        </listitem>
      </varlistentry>
      <varlistentry>
        <term>
          <literal>definition</literal>
        </term>
        <listitem>
          <para>
            Description of the type used in documentation. Give
            information of the type and any of its arguments.
          </para>
        </listitem>
      </varlistentry>
      <varlistentry>
        <term>
          <literal>check</literal>
        </term>
        <listitem>
          <para>
            A function to type check the definition value. Takes the
            definition value as a parameter and returns a boolean
            indicating the type check result, <literal>true</literal>
            for success and <literal>false</literal> for failure.
          </para>
        </listitem>
      </varlistentry>
      <varlistentry>
        <term>
          <literal>merge</literal>
        </term>
        <listitem>
          <para>
            A function to merge multiple definitions values. Takes two
            parameters:
          </para>
          <variablelist>
            <varlistentry>
              <term>
                <emphasis><literal>loc</literal></emphasis>
              </term>
              <listitem>
                <para>
                  The option path as a list of strings, e.g.
                  <literal>[&quot;boot&quot; &quot;loader &quot;grub&quot; &quot;enable&quot;]</literal>.
                </para>
              </listitem>
            </varlistentry>
            <varlistentry>
              <term>
                <emphasis><literal>defs</literal></emphasis>
              </term>
              <listitem>
                <para>
                  The list of sets of defined <literal>value</literal>
                  and <literal>file</literal> where the value was
                  defined, e.g.
                  <literal>[ { file = &quot;/foo.nix&quot;; value = 1; } { file = &quot;/bar.nix&quot;; value = 2 } ]</literal>.
                  The <literal>merge</literal> function should return
                  the merged value or throw an error in case the values
                  are impossible or not meant to be merged.
                </para>
              </listitem>
            </varlistentry>
          </variablelist>
        </listitem>
      </varlistentry>
      <varlistentry>
        <term>
          <literal>getSubOptions</literal>
        </term>
        <listitem>
          <para>
            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.
            <literal>prefix: elemType.getSubOptions (prefix ++ [&quot;prefix&quot;])</literal>
            where
            <emphasis><literal>&quot;prefix&quot;</literal></emphasis>
            is the newly added prefix.
          </para>
        </listitem>
      </varlistentry>
      <varlistentry>
        <term>
          <literal>getSubModules</literal>
        </term>
        <listitem>
          <para>
            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
            <literal>elemType</literal>, the function should just
            recursively look into submodules by returning
            <literal>elemType.getSubModules;</literal>.
          </para>
        </listitem>
      </varlistentry>
      <varlistentry>
        <term>
          <literal>substSubModules</literal>
        </term>
        <listitem>
          <para>
            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 <literal>substSubModules</literal>,
            e.g for a type <literal>composedType</literal> that take an
            <literal>elemtype</literal> type parameter, this function
            should be defined as
            <literal>m: composedType (elemType.substSubModules m)</literal>.
          </para>
        </listitem>
      </varlistentry>
      <varlistentry>
        <term>
          <literal>typeMerge</literal>
        </term>
        <listitem>
          <para>
            A function to merge multiple type declarations. Takes the
            type to merge <literal>functor</literal> as parameter. A
            <literal>null</literal> return value means that type cannot
            be merged.
          </para>
          <variablelist>
            <varlistentry>
              <term>
                <emphasis><literal>f</literal></emphasis>
              </term>
              <listitem>
                <para>
                  The type to merge <literal>functor</literal>.
                </para>
              </listitem>
            </varlistentry>
          </variablelist>
          <para>
            Note: There is a generic <literal>defaultTypeMerge</literal>
            that work with most of value and composed types.
          </para>
        </listitem>
      </varlistentry>
      <varlistentry>
        <term>
          <literal>functor</literal>
        </term>
        <listitem>
          <para>
            An attribute set representing the type. It is used for type
            operations and has the following keys:
          </para>
          <variablelist>
            <varlistentry>
              <term>
                <literal>type</literal>
              </term>
              <listitem>
                <para>
                  The type function.
                </para>
              </listitem>
            </varlistentry>
            <varlistentry>
              <term>
                <literal>wrapped</literal>
              </term>
              <listitem>
                <para>
                  Holds the type parameter for composed types.
                </para>
              </listitem>
            </varlistentry>
            <varlistentry>
              <term>
                <literal>payload</literal>
              </term>
              <listitem>
                <para>
                  Holds the value parameter for value types. The types
                  that have a <literal>payload</literal> are the
                  <literal>enum</literal>,
                  <literal>separatedString</literal> and
                  <literal>submodule</literal> types.
                </para>
              </listitem>
            </varlistentry>
            <varlistentry>
              <term>
                <literal>binOp</literal>
              </term>
              <listitem>
                <para>
                  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.
                </para>
              </listitem>
            </varlistentry>
          </variablelist>
        </listitem>
      </varlistentry>
    </variablelist>
  </section>
</section>