-
-
Notifications
You must be signed in to change notification settings - Fork 37
/
lib.nix
234 lines (191 loc) · 6.79 KB
/
lib.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
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
lib: rec {
/*
Function: flattenTree
Synopsis: Flattens a nested attribute set (tree) into a single-level attribute set.
Parameters:
- tree (attrset): A nested attribute set
Returns:
- An attribute set where keys are constructed in reverse DNS notation, based on the nesting.
Example:
Input: { a = { b = { c = <path>; }; }; }
Output: { "a.b.c" = <path>; }
Description:
The function traverses the nested attribute set and produces a flattened attribute set.
It uses dot-based reverse DNS notation to concatenate the nested keys.
*/
flattenTree = {
tree,
separator ? ".",
}: let
op = sum: path: val: let
pathStr = builtins.concatStringsSep separator path;
in
if builtins.isPath val
then
(sum
// {
"${pathStr}" = val;
})
else if builtins.isAttrs val
then
# recurse into that attribute set
(recurse sum path val)
else
# ignore that value
sum;
recurse = sum: path: val:
builtins.foldl'
(sum: key: op sum (path ++ [key]) val.${key})
sum
(builtins.attrNames val);
in
recurse {} [] tree;
/*
Function: rakeLeaves
Synopsis: Recursively collects `.nix` files from a directory into an attribute set.
Parameters:
- dirPath (string): The directory path to collect `.nix` files from.
Returns:
- An attribute set mapping filenames (without the `.nix` suffix) to their paths.
*/
rakeLeaves = dirPath: let
collect = file: type: {
name = lib.removeSuffix ".nix" file;
value = let
path = dirPath + "/${file}";
in
if (type == "regular")
then path
else rakeLeaves path;
};
files = builtins.readDir dirPath;
in
lib.filterAttrs (_n: v: v != {}) (lib.mapAttrs' collect files);
mkdocs = with lib; rec {
/*
Function: getBaseName
Synopsis: Extracts the base name from a full module path.
Parameters:
- name (string): The full module path.
Returns:
- A string representing the base name of the module.
Example:
Input: "path.to.module"
Output: "module"
Description:
The function splits the input string by dots and returns the last element,
representing the base name of the module.
*/
getBaseName = name: head (splitString "." name);
/*
Function: generateModuleDoc
Synopsis: Generates documentation for a single module.
Parameters:
- value (attrset): The Nix module for which to generate documentation.
Returns:
- An attribute set representing the documentation of the module.
Example:
Input: <module-attrset>
Output: <doc-attrset>
Description:
This function evaluates the module to extract its options, then transforms
these options into a documentation-friendly format. It filters out non-visible
and internal options.
*/
generateModuleDoc = value: let
options = evalModules {modules = [value];};
docList = optionAttrSetToDocList options;
visibleOpts = filter (opt: opt.visible && !opt.internal) docList;
# Transform each option into a simpler attrset.
transformOption = o: nameValuePair o.name (removeAttrs o ["name" "visible" "internal"]);
in
listToAttrs (map transformOption visibleOpts);
/*
Function: renderTableParameters
Synopsis: Renders module parameters into a markdown table format.
Parameters:
- attrs (attrset): An attribute set containing the parameters of a module.
Returns:
- A string representing a markdown table of parameters.
Example:
Input: { type = "string"; description.text = "A simple parameter"; default.text = "default"; }
Output: "| string | A simple parameter | default |"
Description:
Formats the given attribute set into a markdown table row, showing the type,
description, and default value of a module parameter.
*/
renderTableParameters = attrs: let
type = attrs.type or "";
description = attrs.description.text or "";
default = attrs.default.text or "";
in "| ${type} | ${description} | ${default} |";
/*
Function: generateMarkdownEntry
Synopsis: Generates a markdown entry for a module documentation.
Parameters:
- name (string): The name of the module.
- value (attrset): The documentation attribute set of the module.
Returns:
- A string representing the markdown documentation for the module.
Example:
Input: "moduleName", <module-doc-attrset>
Output: <markdown-content>
Description:
Creates a markdown formatted documentation entry for a given module, including
a code snippet and a parameter table.
*/
generateMarkdownEntry = name: value: ''
## `${name}`
**Snippet**
```nix
${name}
```
**Parameter**
| Type | Description | Default |
| ---- | ----------- | ------- |
${renderTableParameters value}
'';
/*
Function: createModuleDocumentation
Synopsis: Creates markdown documentation for a collection of modules.
Parameters:
- docs (attrset): An attribute set where each key is a module name and its value is the module's documentation.
Returns:
- A string representing the concatenated markdown documentation of all modules.
Example:
Input: <modules-docs-attrset>
Output: <markdown-documentation>
Description:
Concatenates the markdown entries of each module in 'docs' to produce a single markdown
document representing the entire documentation.
*/
createModuleDocumentation = docs:
concatStringsSep "\n"
(mapAttrsToList (name: value: let
markdownContent = concatStringsSep "\n\n" (mapAttrsToList generateMarkdownEntry value);
in ''
path=$out/${strings.sanitizeDerivationName name}.md
printf '%s' ${escapeShellArg markdownContent} > "$path"
'')
docs);
/*
Function: createNixosMarkdownDocs
Synopsis: Generates markdown documentation for NixOS modules in a given path.
Parameters:
- modulesPath (string): The file path to the NixOS modules.
Returns:
- A string representing the markdown documentation for all modules in the specified path.
Example:
Input: "/path/to/modules"
Output: <markdown-documentation>
Description:
This function rakes leaves to get all 'options.nix' files, flattens the tree, and then
generates markdown documentation for each module found.
*/
createNixosMarkdownDocs = {modulesPath}: let
eachOptions = filterAttrs (_: hasSuffix "options.nix") (flattenTree {tree = rakeLeaves modulesPath;});
docs = mapAttrs' (name: value: nameValuePair (getBaseName name) (generateModuleDoc value)) eachOptions;
in
createModuleDocumentation docs;
};
}