1# This module includes the NixOS man-pages in the system environment,
2# and optionally starts a browser that shows the NixOS manual on one
3# of the virtual consoles. The latter is useful for the installation
4# CD.
5
6{ config, lib, pkgs, baseModules, ... }:
7
8with lib;
9
10let
11
12 cfg = config.services.nixosManual;
13
14 /* For the purpose of generating docs, evaluate options with each derivation
15 in `pkgs` (recursively) replaced by a fake with path "\${pkgs.attribute.path}".
16 It isn't perfect, but it seems to cover a vast majority of use cases.
17 Caveat: even if the package is reached by a different means,
18 the path above will be shown and not e.g. `${config.services.foo.package}`. */
19 manual = import ../../../doc/manual rec {
20 inherit pkgs config;
21 version = config.system.nixos.release;
22 revision = "release-${version}";
23 options =
24 let
25 scrubbedEval = evalModules {
26 modules = [ { nixpkgs.localSystem = config.nixpkgs.localSystem; } ] ++ baseModules;
27 args = (config._module.args) // { modules = [ ]; };
28 specialArgs = { pkgs = scrubDerivations "pkgs" pkgs; };
29 };
30 scrubDerivations = namePrefix: pkgSet: mapAttrs
31 (name: value:
32 let wholeName = "${namePrefix}.${name}"; in
33 if isAttrs value then
34 scrubDerivations wholeName value
35 // (optionalAttrs (isDerivation value) { outPath = "\${${wholeName}}"; })
36 else value
37 )
38 pkgSet;
39 in scrubbedEval.options;
40 };
41
42 entry = "${manual.manual}/share/doc/nixos/index.html";
43
44 helpScript = pkgs.writeScriptBin "nixos-help"
45 ''
46 #! ${pkgs.runtimeShell} -e
47 # Finds first executable browser in a colon-separated list.
48 # (see how xdg-open defines BROWSER)
49 browser="$(
50 IFS=: ; for b in $BROWSER; do
51 [ -n "$(type -P "$b" || true)" ] && echo "$b" && break
52 done
53 )"
54 if [ -z "$browser" ]; then
55 browser="$(type -P xdg-open || true)"
56 if [ -z "$browser" ]; then
57 browser="$(type -P w3m || true)"
58 if [ -z "$browser" ]; then
59 echo "$0: unable to start a web browser; please set \$BROWSER"
60 exit 1
61 fi
62 fi
63 fi
64 exec "$browser" ${entry}
65 '';
66
67 desktopItem = pkgs.makeDesktopItem {
68 name = "nixos-manual";
69 desktopName = "NixOS Manual";
70 genericName = "View NixOS documentation in a web browser";
71 icon = "nix-snowflake";
72 exec = "${helpScript}/bin/nixos-help";
73 categories = "System";
74 };
75in
76
77{
78
79 options = {
80
81 services.nixosManual.enable = mkOption {
82 type = types.bool;
83 default = true;
84 description = ''
85 Whether to build the NixOS manual pages.
86 '';
87 };
88
89 services.nixosManual.showManual = mkOption {
90 type = types.bool;
91 default = false;
92 description = ''
93 Whether to show the NixOS manual on one of the virtual
94 consoles.
95 '';
96 };
97
98 services.nixosManual.ttyNumber = mkOption {
99 type = types.int;
100 default = 8;
101 description = ''
102 Virtual console on which to show the manual.
103 '';
104 };
105
106 services.nixosManual.browser = mkOption {
107 type = types.path;
108 default = "${pkgs.w3m-nographics}/bin/w3m";
109 description = ''
110 Browser used to show the manual.
111 '';
112 };
113
114 };
115
116
117 config = mkIf cfg.enable {
118
119 system.build.manual = manual;
120
121 environment.systemPackages = []
122 ++ optionals config.services.xserver.enable [ desktopItem pkgs.nixos-icons ]
123 ++ optional config.documentation.man.enable manual.manpages
124 ++ optionals config.documentation.doc.enable [ manual.manual helpScript ];
125
126 boot.extraTTYs = mkIf cfg.showManual ["tty${toString cfg.ttyNumber}"];
127
128 systemd.services = optionalAttrs cfg.showManual
129 { "nixos-manual" =
130 { description = "NixOS Manual";
131 wantedBy = [ "multi-user.target" ];
132 serviceConfig =
133 { ExecStart = "${cfg.browser} ${entry}";
134 StandardInput = "tty";
135 StandardOutput = "tty";
136 TTYPath = "/dev/tty${toString cfg.ttyNumber}";
137 TTYReset = true;
138 TTYVTDisallocate = true;
139 Restart = "always";
140 };
141 };
142 };
143
144 services.mingetty.helpLine = "\nRun `nixos-help` "
145 + lib.optionalString cfg.showManual "or press <Alt-F${toString cfg.ttyNumber}> "
146 + "for the NixOS manual.";
147
148 };
149
150}