summaryrefslogtreecommitdiffstats
path: root/doc/doc-support/package.nix
blob: 0c9c5c6b88077ed91b9ff7c8a6a526e46d9081bd (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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# This file describes the Nixpkgs manual, which happens to use module docs infra originally
# developed for NixOS. To build this derivation, run `nix-build -A nixpkgs-manual`.
#
{
  lib,
  stdenvNoCC,
  callPackage,
  devmode,
  mkShellNoCC,
  documentation-highlighter,
  nixos-render-docs,
  nixos-render-docs-redirects,
  writeShellScriptBin,
  nixpkgs ? { },
  markdown-code-runner,
  roboto,
  treefmt,
  nixosOptionsDoc,
}:
stdenvNoCC.mkDerivation (
  finalAttrs:
  let
    inherit (finalAttrs.finalPackage.optionsDoc) optionsJSON;
    inherit (finalAttrs.finalPackage) epub lib-docs pythonInterpreterTable;

    # Make anything from lib (the module system internals) invisible
    hide-lib =
      opt:
      opt
      // {
        visible = if lib.all (decl: decl == "lib/modules.nix") opt.declarations then false else opt.visible;
      };

    toURL =
      decl:
      let
        declStr = toString decl;
        root = toString ../..;
        subpath = lib.removePrefix "/" (lib.removePrefix root declStr);
      in
      if lib.hasPrefix root declStr then
        {
          url = "https://github.com/NixOS/nixpkgs/blob/master/${subpath}";
          name = "nixpkgs/${subpath}";
        }
      else
        decl;

    mapURLs = opt: opt // { declarations = map toURL opt.declarations; };

    docs.generic.meta-maintainers = nixosOptionsDoc {
      inherit (lib.evalModules { modules = [ ../../modules/generic/meta-maintainers.nix ]; }) options;
      transformOptions = opt: hide-lib (mapURLs opt);
    };
  in
  {
    version = lib.trivial.release;
    pname = "nixpkgs-manual";

    nativeBuildInputs = [ nixos-render-docs ];

    src = lib.cleanSourceWith {
      src = ../.;
      filter =
        path: type:
        type == "directory"
        || lib.hasSuffix ".md" path
        || lib.hasSuffix ".md.in" path
        || lib.elem path (
          map toString [
            ../style.css
            ../anchor-use.js
            ../anchor.min.js
            ../manpage-urls.json
            ../redirects.json
            ../nav.json
          ]
        );
    };

    postPatch = ''
      ln -s ${optionsJSON}/share/doc/nixos/options.json ./config-options.json
      ln -s ${treefmt.functionsDoc.markdown} ./packages/treefmt-functions.section.md
      ln -s ${treefmt.optionsDoc.optionsJSON}/share/doc/nixos/options.json ./treefmt-options.json
      ln -s ${docs.generic.meta-maintainers.optionsJSON}/share/doc/nixos/options.json ./options-modules-generic-meta-maintainers.json
    '';

    buildPhase = ''
      runHook preBuild

      substituteInPlace ./languages-frameworks/python.section.md \
        --subst-var-by python-interpreter-table "$(<"${pythonInterpreterTable}")"

      cat ./functions/library.md.in ${lib-docs}/index.md > ./functions/library.md

      substitute ./manual.md.in ./manual.md \
        --replace-fail '@MANUAL_VERSION@' '${lib.version}'

      mkdir -p out/media

      mkdir -p out/highlightjs
      cp -t out/highlightjs \
        ${documentation-highlighter}/highlight.pack.js \
        ${documentation-highlighter}/LICENSE \
        ${documentation-highlighter}/mono-blue.css \
        ${documentation-highlighter}/loader.js

      cp -t out ./style.css ./anchor.min.js ./anchor-use.js

      nixos-render-docs manual html \
        --manpage-urls ./manpage-urls.json \
        --redirects ./redirects.json \
        --revision ${nixpkgs.rev or "master"} \
        --stylesheet style.css \
        --stylesheet highlightjs/mono-blue.css \
        --script ./highlightjs/highlight.pack.js \
        --script ./highlightjs/loader.js \
        --script ./anchor.min.js \
        --script ./anchor-use.js \
        --sidebar-depth 3 \
        --experimental-config ./nav.json \
        --header ${./header.html}\
        --no-navheader \
        manual.md \
        out/index.html

      runHook postBuild
    '';

    installPhase = ''
      runHook preInstall

      dest="$out/share/doc/nixpkgs"
      mkdir -p "$(dirname "$dest")"
      mv out "$dest"
      cp "$dest/index.html" "$dest/manual.html"

      cp ${roboto.src}/web/Roboto\[ital\,wdth\,wght\].ttf "$dest/Roboto.ttf"

      cp ${epub} "$dest/nixpkgs-manual.epub"

      mkdir -p $out/nix-support/
      echo "doc manual $dest index.html" >> $out/nix-support/hydra-build-products
      echo "doc manual $dest nixpkgs-manual.epub" >> $out/nix-support/hydra-build-products

      runHook postInstall
    '';

    passthru = {
      lib-docs = callPackage ./lib-function-docs.nix { inherit nixpkgs; };

      epub = callPackage ./epub.nix { };

      optionsDoc = callPackage ./options-doc.nix { };

      pythonInterpreterTable = callPackage ./python-interpreter-table.nix { };

      shell =
        let
          devmode' = devmode.override {
            buildArgs = toString ../.;
            open = "/share/doc/nixpkgs/index.html";
          };
          nixos-render-docs-redirects' = writeShellScriptBin "redirects" ''${lib.getExe nixos-render-docs-redirects} --file '${toString ../redirects.json}' "$@"'';
        in
        mkShellNoCC {
          packages = [
            devmode'
            nixos-render-docs-redirects'
            markdown-code-runner
          ];
        };

      tests = {
        # Don't run this in CI because it's not reproducible
        manpage-urls = callPackage ../tests/manpage-urls.nix { };
      };
    };

    meta.license = lib.licenses.mit;
  }
)