summaryrefslogtreecommitdiffstats
path: root/docs/package.nix
blob: c101145a43bad3b3def1edd224ae66e0b7ee5dea (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
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
{
  hjemModule,
  nixpkgs,
  pkgs,
  lib,
}: let
  inherit (builtins) isAttrs;
  inherit (lib.attrsets) isDerivation mapAttrs optionalAttrs;
  inherit (lib.modules) mkForce evalModules;
  inherit (lib.options) mkOption;
  inherit (lib.strings) hasPrefix removePrefix;
  inherit (lib.trivial) pipe;
  inherit (lib.types) anything;

  configJSON =
    (pkgs.nixosOptionsDoc {
      variablelistId = "hjem-options";
      warningsAreErrors = true;

      inherit
        (
          (evalModules {
            modules = [
              hjemModule
              {
                # exclude NixOS options from the documentation
                options = {
                  _module.args = mkOption {
                    internal = true;
                  };
                  users = mkOption {
                    type = anything;
                    internal = true;
                  };
                };
                config = {
                  _module = let
                    # From nixpkgs:
                    #
                    # Recursively replace each derivation in the given attribute set
                    # with the same derivation but with the `outPath` attribute set to
                    # the string `"\${pkgs.attribute.path}"`. This allows the
                    # documentation to refer to derivations through their values without
                    # establishing an actual dependency on the derivation output.
                    #
                    # This is not perfect, but it seems to cover a vast majority of use cases.
                    #
                    # Caveat: even if the package is reached by a different means, the
                    # path above will be shown and not e.g.
                    # `${config.services.foo.package}`.
                    scrubDerivations = namePrefix: pkgSet:
                      mapAttrs (
                        name: value: let
                          wholeName = "${namePrefix}.${name}";
                        in
                          if isAttrs value
                          then
                            scrubDerivations wholeName value
                            // optionalAttrs (isDerivation value) {
                              inherit (value) drvPath;
                              outPath = "\${${wholeName}}";
                            }
                          else value
                      )
                      pkgSet;
                  in {
                    check = false;
                    args = {
                      pkgs = mkForce (scrubDerivations "pkgs" pkgs);
                      utils = import "${nixpkgs}/nixos/lib/utils.nix" {
                        inherit lib;
                        config = {};
                        pkgs = null;
                      };
                    };
                  };

                  # due to how options are documented, `hjem.users.<username>` will try to access `users.users."‹username›"`
                  users.users."‹username›" = {home = "/home/‹username›";};
                };
              }
            ];
          })
        )
        options
        ;

      transformOptions = opt:
        opt
        // {
          declarations =
            map (
              decl:
                if hasPrefix (toString ../.) (toString decl)
                then
                  pipe decl [
                    toString
                    (removePrefix (toString ../.))
                    (removePrefix "/")
                    (x: {
                      url = "https://github.com/feel-co/hjem/blob/main/${x}";
                      name = "<hjem/${x}>";
                    })
                  ]
                else if decl == "lib/modules.nix"
                then {
                  url = "https://github.com/NixOS/nixpkgs/blob/master/${decl}";
                  name = "<nixpkgs/lib/modules.nix>";
                }
                else decl
            )
            opt.declarations;
        };
    })
    .optionsJSON;

  ndgConfig = (pkgs.formats.toml {}).generate "ndg-hjem.toml" {
    title = "Hjem";
    module_options = "${configJSON}/share/doc/nixos/options.json";
    manpage_urls_path = "${nixpkgs}/doc/manpage-urls.json";
    highlight_code = true;
    search.enable = true;
    sidebar.options.depth = 3;
  };

  hjemDocs =
    pkgs.runCommand "hjem-docs" {
      outputs = ["out" "man"];
      nativeBuildInputs = [pkgs.ndg pkgs.gzip];
    } ''
      mkdir -p $out/share/doc
      mkdir -p $man/share/man/man5

      # Copy the markdown sources to be processed by ndg
      cp -rvf ${./inputs} ./inputs

      ndg --verbose \
        --config-file ${ndgConfig} \
        html \
        --jobs $NIX_BUILD_CORES \
        --input-dir ./inputs \
        --output-dir "$out/share/doc"

      ndg --verbose man \
        --module-options ${configJSON}/share/doc/nixos/options.json \
        --title "hjem" \
        --section 5 \
        --output-file hjem.5

      gzip -c hjem.5 > $man/share/man/man5/hjem.5.gz
    '';
in {
  html = hjemDocs;
  man = hjemDocs.man;
  options.json =
    pkgs.runCommand "options.json" {
      meta.description = "List of Hjem options in JSON format.";
    } ''
      mkdir -p $out/{share/doc,nix-support}

      cp -a ${configJSON}/share/doc/nixos $out/share/doc/hjem

      substitute \
        ${configJSON}/nix-support/hydra-build-products \
        $out/nix-support/hydra-build-products \
          --replace '${configJSON}/share/doc/nixos' "$out/share/doc/hjem"
    '';
}