summaryrefslogtreecommitdiffstats
path: root/doc-options-md.nix
blob: 5cbc7c2208aba8ec924fb82d63c7d4f434512d7d (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
{ outputAttrPath, optionsAttrPath, optionsInternal ? true, }:

{ lib, options, pkgs, ... }:

with lib;

let
  visibleOptionDocs = filter (opt: opt.visible && !opt.internal) (optionAttrSetToDocList options);

  isLiteral = value:
    value ? _type &&
    (value._type == "literalExpression" || value._type == "literalExample");

  toValue = value:
    if isLiteral value then value.text
    else generators.toPretty { } value;

  toMarkdown = option:
    ''
      ## `${option.name}`

      ${option.description}

      ${optionalString (option ? default) ''
        **Default value**:

        ```nix
        ${toValue option.default}
        ```
      ''}

      **Type**: ${option.type}${optionalString option.readOnly " (read only)"}

      ${optionalString (option ? example) ''
        **Example**:

        ```nix
        ${toValue option.example}
        ```
      ''}

      Declared in:

      ${concatStringsSep "\n" (map (decl: "- ${decl}") option.declarations)}

    '';

  # TODO: rewrite "Declared in" so that it points to GitHub repository

  options-md = concatStringsSep "\n" (map toMarkdown visibleOptionDocs);
in
{
  config = setAttrByPath outputAttrPath {
    doc-options-md = pkgs.writeText "options.md" options-md;
  };
}