at 23.05-pre 8.6 kB view raw
1/* Generate JSON, XML and DocBook documentation for given NixOS options. 2 3 Minimal example: 4 5 { pkgs, }: 6 7 let 8 eval = import (pkgs.path + "/nixos/lib/eval-config.nix") { 9 baseModules = [ 10 ../module.nix 11 ]; 12 modules = []; 13 }; 14 in pkgs.nixosOptionsDoc { 15 options = eval.options; 16 } 17 18*/ 19{ pkgs 20, lib 21, options 22, transformOptions ? lib.id # function for additional tranformations of the options 23, documentType ? "appendix" # TODO deprecate "appendix" in favor of "none" 24 # and/or rename function to moduleOptionDoc for clean slate 25 26 # If you include more than one option list into a document, you need to 27 # provide different ids. 28, variablelistId ? "configuration-variable-list" 29 # Strig to prefix to the option XML/HTML id attributes. 30, optionIdPrefix ? "opt-" 31, revision ? "" # Specify revision for the options 32# a set of options the docs we are generating will be merged into, as if by recursiveUpdate. 33# used to split the options doc build into a static part (nixos/modules) and a dynamic part 34# (non-nixos modules imported via configuration.nix, other module sources). 35, baseOptionsJSON ? null 36# instead of printing warnings for eg options with missing descriptions (which may be lost 37# by nix build unless -L is given), emit errors instead and fail the build 38, warningsAreErrors ? true 39# allow docbook option docs if `true`. only markdown documentation is allowed when set to 40# `false`, and a different renderer may be used with different bugs and performance 41# characteristics but (hopefully) indistinguishable output. 42, allowDocBook ? true 43# whether lib.mdDoc is required for descriptions to be read as markdown. 44, markdownByDefault ? false 45}: 46 47let 48 # Make a value safe for JSON. Functions are replaced by the string "<function>", 49 # derivations are replaced with an attrset 50 # { _type = "derivation"; name = <name of that derivation>; }. 51 # We need to handle derivations specially because consumers want to know about them, 52 # but we can't easily use the type,name subset of keys (since type is often used as 53 # a module option and might cause confusion). Use _type,name instead to the same 54 # effect, since _type is already used by the module system. 55 substSpecial = x: 56 if lib.isDerivation x then { _type = "derivation"; name = x.name; } 57 else if builtins.isAttrs x then lib.mapAttrs (name: substSpecial) x 58 else if builtins.isList x then map substSpecial x 59 else if lib.isFunction x then "<function>" 60 else x; 61 62 rawOpts = lib.optionAttrSetToDocList options; 63 transformedOpts = map transformOptions rawOpts; 64 filteredOpts = lib.filter (opt: opt.visible && !opt.internal) transformedOpts; 65 optionsList = lib.flip map filteredOpts 66 (opt: opt 67 // lib.optionalAttrs (opt ? example) { example = substSpecial opt.example; } 68 // lib.optionalAttrs (opt ? default) { default = substSpecial opt.default; } 69 // lib.optionalAttrs (opt ? type) { type = substSpecial opt.type; } 70 // lib.optionalAttrs (opt ? relatedPackages && opt.relatedPackages != []) { relatedPackages = genRelatedPackages opt.relatedPackages opt.name; } 71 ); 72 73 # Generate DocBook documentation for a list of packages. This is 74 # what `relatedPackages` option of `mkOption` from 75 # ../../../lib/options.nix influences. 76 # 77 # Each element of `relatedPackages` can be either 78 # - a string: that will be interpreted as an attribute name from `pkgs` and turned into a link 79 # to search.nixos.org, 80 # - a list: that will be interpreted as an attribute path from `pkgs` and turned into a link 81 # to search.nixos.org, 82 # - an attrset: that can specify `name`, `path`, `comment` 83 # (either of `name`, `path` is required, the rest are optional). 84 # 85 # NOTE: No checks against `pkgs` are made to ensure that the referenced package actually exists. 86 # Such checks are not compatible with option docs caching. 87 genRelatedPackages = packages: optName: 88 let 89 unpack = p: if lib.isString p then { name = p; } 90 else if lib.isList p then { path = p; } 91 else p; 92 describe = args: 93 let 94 title = args.title or null; 95 name = args.name or (lib.concatStringsSep "." args.path); 96 in '' 97 <listitem> 98 <para> 99 <link xlink:href="https://search.nixos.org/packages?show=${name}&amp;sort=relevance&amp;query=${name}"> 100 <literal>${lib.optionalString (title != null) "${title} aka "}pkgs.${name}</literal> 101 </link> 102 </para> 103 ${lib.optionalString (args ? comment) "<para>${args.comment}</para>"} 104 </listitem> 105 ''; 106 in "<itemizedlist>${lib.concatStringsSep "\n" (map (p: describe (unpack p)) packages)}</itemizedlist>"; 107 108 optionsNix = builtins.listToAttrs (map (o: { name = o.name; value = removeAttrs o ["name" "visible" "internal"]; }) optionsList); 109 110in rec { 111 inherit optionsNix; 112 113 optionsAsciiDoc = pkgs.runCommand "options.adoc" {} '' 114 ${pkgs.python3Minimal}/bin/python ${./generateAsciiDoc.py} \ 115 < ${optionsJSON}/share/doc/nixos/options.json \ 116 > $out 117 ''; 118 119 optionsCommonMark = pkgs.runCommand "options.md" {} '' 120 ${pkgs.python3Minimal}/bin/python ${./generateCommonMark.py} \ 121 < ${optionsJSON}/share/doc/nixos/options.json \ 122 > $out 123 ''; 124 125 optionsJSON = pkgs.runCommand "options.json" 126 { meta.description = "List of NixOS options in JSON format"; 127 nativeBuildInputs = [ 128 pkgs.brotli 129 (let 130 # python3Minimal can't be overridden with packages on Darwin, due to a missing framework. 131 # Instead of modifying stdenv, we take the easy way out, since most people on Darwin will 132 # just be hacking on the Nixpkgs manual (which also uses make-options-doc). 133 python = if pkgs.stdenv.isDarwin then pkgs.python3 else pkgs.python3Minimal; 134 self = (python.override { 135 inherit self; 136 includeSiteCustomize = true; 137 }); 138 in self.withPackages (p: [ p.mistune ])) 139 ]; 140 options = builtins.toFile "options.json" 141 (builtins.unsafeDiscardStringContext (builtins.toJSON optionsNix)); 142 # merge with an empty set if baseOptionsJSON is null to run markdown 143 # processing on the input options 144 baseJSON = 145 if baseOptionsJSON == null 146 then builtins.toFile "base.json" "{}" 147 else baseOptionsJSON; 148 } 149 '' 150 # Export list of options in different format. 151 dst=$out/share/doc/nixos 152 mkdir -p $dst 153 154 python ${./mergeJSON.py} \ 155 ${lib.optionalString warningsAreErrors "--warnings-are-errors"} \ 156 ${lib.optionalString (! allowDocBook) "--error-on-docbook"} \ 157 ${lib.optionalString markdownByDefault "--markdown-by-default"} \ 158 $baseJSON $options \ 159 > $dst/options.json 160 161 brotli -9 < $dst/options.json > $dst/options.json.br 162 163 mkdir -p $out/nix-support 164 echo "file json $dst/options.json" >> $out/nix-support/hydra-build-products 165 echo "file json-br $dst/options.json.br" >> $out/nix-support/hydra-build-products 166 ''; 167 168 # Convert options.json into an XML file. 169 # The actual generation of the xml file is done in nix purely for the convenience 170 # of not having to generate the xml some other way 171 optionsXML = pkgs.runCommand "options.xml" {} '' 172 export NIX_STORE_DIR=$TMPDIR/store 173 export NIX_STATE_DIR=$TMPDIR/state 174 ${pkgs.nix}/bin/nix-instantiate \ 175 --eval --xml --strict ${./optionsJSONtoXML.nix} \ 176 --argstr file ${optionsJSON}/share/doc/nixos/options.json \ 177 > "$out" 178 ''; 179 180 optionsDocBook = pkgs.runCommand "options-docbook.xml" {} '' 181 optionsXML=${optionsXML} 182 if grep /nixpkgs/nixos/modules $optionsXML; then 183 echo "The manual appears to depend on the location of Nixpkgs, which is bad" 184 echo "since this prevents sharing via the NixOS channel. This is typically" 185 echo "caused by an option default that refers to a relative path (see above" 186 echo "for hints about the offending path)." 187 exit 1 188 fi 189 190 ${pkgs.python3Minimal}/bin/python ${./sortXML.py} $optionsXML sorted.xml 191 ${pkgs.libxslt.bin}/bin/xsltproc \ 192 --stringparam documentType '${documentType}' \ 193 --stringparam revision '${revision}' \ 194 --stringparam variablelistId '${variablelistId}' \ 195 --stringparam optionIdPrefix '${optionIdPrefix}' \ 196 -o intermediate.xml ${./options-to-docbook.xsl} sorted.xml 197 ${pkgs.libxslt.bin}/bin/xsltproc \ 198 -o "$out" ${./postprocess-option-descriptions.xsl} intermediate.xml 199 ''; 200}