summaryrefslogtreecommitdiffstats
path: root/lib/tests/modules/composed-types-valueMeta.nix
blob: fc2279b69301731e8ca78127be87e23dbaca55c4 (plain)
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
{ lib, ... }:
let
  inherit (lib) types mkOption;

  attrsOfModule = mkOption {
    type = types.attrsOf (
      types.submodule {
        options.bar = mkOption {
          type = types.int;
        };
      }
    );
  };

  listOfModule = mkOption {
    type = types.listOf (
      types.submodule {
        options.bar = mkOption {
          type = types.int;
        };
      }
    );
  };

in
{
  imports = [
    #  Module A
    {
      options.attrsOfModule = attrsOfModule;
      options.mergedAttrsOfModule = attrsOfModule;
      options.listOfModule = listOfModule;
      options.mergedListOfModule = listOfModule;
    }
    # Module B
    {
      options.mergedAttrsOfModule = attrsOfModule;
      options.mergedListOfModule = listOfModule;
    }
    # Values
    # It is important that the value is defined in a separate module
    # Without valueMeta the actual value and sub-options wouldn't be accessible via:
    # options.attrsOfModule.type.getSubOptions
    {
      attrsOfModule = {
        foo.bar = 42;
      };
      mergedAttrsOfModule = {
        foo.bar = 42;
      };
    }
    (
      { options, ... }:
      {
        config.listOfModule = [
          {
            bar = 42;
          }
        ];
        config.mergedListOfModule = [
          {
            bar = 42;
          }
        ];
        # Result options to expose the list module to bash as plain attribute path
        options.listResult = mkOption {
          default = (builtins.head options.listOfModule.valueMeta.list).configuration.options.bar.value;
        };
        options.mergedListResult = mkOption {
          default = (builtins.head options.mergedListOfModule.valueMeta.list).configuration.options.bar.value;
        };
      }
    )
  ];
}