-
Notifications
You must be signed in to change notification settings - Fork 12
/
type.nix
136 lines (131 loc) · 3.91 KB
/
type.nix
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
{
extracted,
lib,
}: let
inherit (lib) types mapAttrs mapAttrsToList nameValuePair;
mapListToAttrs = f: lst:
builtins.listToAttrs (map f lst);
# Settings module
settingType = setting:
types.submodule ({config, ...}: {
options = {
enable = lib.mkOption {
description = "Enable the \"${setting.name}\" setting";
type = types.bool;
default = setting.enabled;
};
value = lib.mkOption {
description = "The value of the \"${setting.name}\" setting";
type = types.anything;
default = setting.value;
};
flat = lib.mkOption {
description = "Empty attrset in enable=false, the setting and its value otherwise";
type = types.attrsOf types.anything;
readOnly = true;
};
};
config = {
flat =
if config.enable
then {"${setting.name}" = config.value;}
else {};
};
});
settingOption = setting:
nameValuePair setting.name
(lib.mkOption {
description = "Control the \"${setting.name}\" setting";
type = settingType setting;
default = {};
});
# Subsection modules
subsectionType = name: sub:
types.submodule ({config, ...}: {
options =
{
enable = lib.mkEnableOption "settings for ${name}";
flatSettings = lib.mkOption {
description = "Flat attrset of all settings in subsection ${name} enabled";
type = types.attrsOf types.anything;
readOnly = true;
};
}
// mapListToAttrs settingOption sub.settings;
config = {
enable = lib.mkDefault true;
flatSettings =
if config.enable
then
builtins.foldl' (x: y: x // y) {}
(map (setting: config."${setting.name}".flat) sub.settings)
else {};
};
});
subsectionOption = name: sub:
lib.mkOption {
description = "${name}: ${sub.meta.title}\n${sub.meta.description}";
type = subsectionType name sub;
default = {};
};
# Section module
sectionType = name: section: let
subsections = builtins.removeAttrs section ["meta"];
in
types.submodule ({config, ...}: {
options =
{
enable = lib.mkOption {
description = "setting for ${name}";
type = types.bool;
};
flatSettings = lib.mkOption {
description = "Flat attrset of all settings in section ${name} enabled";
type = types.attrsOf types.anything;
readOnly = true;
};
}
// mapAttrs subsectionOption subsections;
config = {
flatSettings =
if config.enable
then
builtins.foldl' (x: y: x // y) {}
(mapAttrsToList (name: _: config."${name}".flatSettings) subsections)
else {};
};
});
sectionOption = name: section:
lib.mkOption {
description = "${name}: ${section.meta.title}\n${section.meta.description}";
type = sectionType name section;
default = {};
};
enableSection = name: _: {config, ...}: {
"${name}".enable = lib.mkDefault config.enableAllSections;
};
# Top-level module
type = types.submodule ({config, ...}: {
options =
{
enable = lib.mkEnableOption "Arkenfox settings";
enableAllSections = lib.mkEnableOption "Enable all sections by default";
flatSettings = lib.mkOption {
description = "Flat attrset of all settings enabled";
type = types.attrsOf types.anything;
readOnly = true;
};
}
// mapAttrs sectionOption extracted;
config = {
flatSettings =
if config.enable
then
builtins.foldl' (x: y: x // y) {}
(mapAttrsToList (name: _: config."${name}".flatSettings) extracted)
else {};
};
imports = mapAttrsToList enableSection extracted;
});
in
type