feat: add function to generate docs for nixlet values

add docs for nixlets in this repo to the mkdocs site
This commit is contained in:
technofab 2025-07-19 16:25:46 +02:00
parent 2cb8c69aaa
commit 48e8cf48b0
No known key found for this signature in database
3 changed files with 214 additions and 74 deletions

84
lib/valuesDocs.nix Normal file
View file

@ -0,0 +1,84 @@
{
lib,
nixlet,
transformOptions ? opt: opt,
filter ? _: true,
headingDepth ? 3,
...
}: let
inherit
(lib)
removeSuffix
concatStringsSep
mapAttrsToList
concatStrings
replicate
;
_transformOptions = opt:
transformOptions (opt
// {
visible = let
filtered = !builtins.elem (builtins.head opt.loc) ["_module"];
in
filtered && opt.visible && (filter opt);
name = lib.removePrefix "config." opt.name;
});
rawOpts = lib.optionAttrSetToDocList nixlet.values.options;
transformedOpts = map _transformOptions rawOpts;
filteredOpts = lib.filter (opt: opt.visible && !opt.internal) transformedOpts;
optionsNix = builtins.listToAttrs (
map (o: {
name = o.name;
value = removeAttrs o [
"visible"
"internal"
];
})
filteredOpts
);
optToMd = opt: let
headingDecl = concatStrings (replicate headingDepth "#");
in
''
${headingDecl} `${opt.name}`
${
if opt.description != null
then opt.description
else "(no description)"
}
**Type**:
```console
${opt.type}
```
''
+ (lib.optionalString (opt ? default && opt.default != null) ''
**Default value**:
```nix
${removeSuffix "\n" opt.default.text}
```
'')
+ (lib.optionalString (opt ? example) ''
**Example value**:
```nix
${removeSuffix "\n" opt.example.text}
```
'')
+ "\n";
opts = mapAttrsToList (name: opt:
optToMd opt)
optionsNix;
markdown = concatStringsSep "\n" opts;
in
builtins.toFile "values-doc.md" markdown