summaryrefslogtreecommitdiff
path: root/doc/manual/default.nix
blob: d8e12f298b6d21b37cb5f5ac4618bd808aedd924 (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
{ pkgs
, options
, config
, version
, revision
, nixpkgsRevision
, extraSources ? []
, prefix ? ../..
}:

with pkgs;

let
  lib = pkgs.lib;

  gitHubDeclaration = user: repo: ref: subpath:
    # Default to `master` if we don't know what revision the system
    # configuration is using (custom nixpkgs, etc.).
    let urlRef = if ref != null then ref else "master";
    in {
      url = "https://github.com/${user}/${repo}/blob/${urlRef}/${subpath}";
      name = "<${repo}/${subpath}>";
    };

  optionsDoc = buildPackages.nixosOptionsDoc {
    inherit options;
    transformOptions = opt: opt // {
      # Clean up declaration sites to not refer to the nix-darwin source tree.
      # TODO: handle `extraSources`? (it's not set anywhere)
      declarations = map
        (decl:
          if lib.hasPrefix (toString prefix) (toString decl) then
            gitHubDeclaration "LnL7" "nix-darwin" revision
              (lib.removePrefix "/"
                (lib.removePrefix (toString prefix) (toString decl)))
          # TODO: handle this in a better way (may require upstream
          # changes to nixpkgs)
          else if decl == "lib/modules.nix" then
            gitHubDeclaration "NixOS" "nixpkgs" nixpkgsRevision decl
          else decl)
        opt.declarations;
    };
  };

in rec {
  # TODO: Use `optionsDoc.optionsJSON` directly once upstream
  # `nixosOptionsDoc` is more customizable.
  optionsJSON = runCommand "options.json"
    { meta.description = "List of nix-darwin options in JSON format"; }
    ''
      mkdir -p $out/{share/doc,nix-support}
      cp -a ${optionsDoc.optionsJSON}/share/doc/nixos $out/share/doc/darwin
      substitute \
        ${optionsDoc.optionsJSON}/nix-support/hydra-build-products \
        $out/nix-support/hydra-build-products \
        --replace \
          '${optionsDoc.optionsJSON}/share/doc/nixos' \
          "$out/share/doc/darwin"
    '';

  # Generate the nix-darwin manual.
  manualHTML = runCommand "darwin-manual-html"
    { nativeBuildInputs = [ buildPackages.nixos-render-docs ];
      styles = lib.sourceFilesBySuffices (pkgs.path + "/doc") [ ".css" ];
      meta.description = "The Darwin manual in HTML format";
      allowedReferences = ["out"];
    }
    ''
      # Generate the HTML manual.
      dst=$out/share/doc/darwin
      mkdir -p $dst

      cp $styles/style.css $dst
      cp -r ${pkgs.documentation-highlighter} $dst/highlightjs

      substitute ${./manual.md} manual.md \
        --replace '@DARWIN_VERSION@' "${version}" \
        --replace \
          '@DARWIN_OPTIONS_JSON@' \
          ${optionsJSON}/share/doc/darwin/options.json

      # Pass --redirects option if nixos-render-docs supports it
      if nixos-render-docs manual html --help | grep --silent -E '^\s+--redirects\s'; then
        redirects_opt="--redirects ${./redirects.json}"
      fi

      # TODO: --manpage-urls?
      nixos-render-docs -j $NIX_BUILD_CORES manual html \
        --manpage-urls ${pkgs.writeText "manpage-urls.json" "{}"} \
        --revision ${lib.escapeShellArg revision} \
        --generator "nixos-render-docs ${lib.version}" \
        $redirects_opt \
        --stylesheet style.css \
        --stylesheet highlightjs/mono-blue.css \
        --script ./highlightjs/highlight.pack.js \
        --script ./highlightjs/loader.js \
        --toc-depth 1 \
        --chunk-toc-depth 1 \
        ./manual.md \
        $dst/index.html

      mkdir -p $out/nix-support
      echo "nix-build out $out" >> $out/nix-support/hydra-build-products
      echo "doc manual $dst" >> $out/nix-support/hydra-build-products
    '';

  # Index page of the nix-darwin manual.
  manualHTMLIndex = "${manualHTML}/share/doc/darwin/index.html";

  manualEpub = builtins.throw "The nix-darwin EPUB manual has been removed.";

  # Generate the nix-darwin manpages.
  manpages = runCommand "darwin-manpages"
    { nativeBuildInputs = [ buildPackages.nixos-render-docs ];
      allowedReferences = ["out"];
    }
    ''
      # Generate manpages.
      mkdir -p $out/share/man/man5
      nixos-render-docs -j $NIX_BUILD_CORES options manpage \
        --revision ${lib.escapeShellArg revision} \
        ${optionsJSON}/share/doc/darwin/options.json \
        $out/share/man/man5/configuration.nix.5

      # TODO: get these parameterized in upstream nixos-render-docs
      sed -i -e '
        /^\.TH / s|NixOS|nix-darwin|g

        /^\.SH "NAME"$/ {
          N
          s|NixOS|nix-darwin|g
        }

        /^\.SH "DESCRIPTION"$/ {
          N; N
          s|/etc/nixos/configuration|configuration|g
          s|NixOS|nix-darwin|g
          s|nixos|nix-darwin|g
        }

        /\.SH "AUTHORS"$/ {
          N; N
          s|Eelco Dolstra and the Nixpkgs/NixOS contributors|Daiderd Jordan and the nix-darwin contributors|g
        }
      ' $out/share/man/man5/configuration.nix.5
    '';
}