2024-04-21 15:54:59 +00:00
|
|
|
/**
|
2024-07-27 06:49:29 +00:00
|
|
|
Generates documentation for [nix modules](https://nix.dev/tutorials/module-system/index.html).
|
2020-04-24 23:36:52 +00:00
|
|
|
|
2024-04-21 15:54:59 +00:00
|
|
|
It uses the declared `options` to generate documentation in various formats.
|
2020-04-24 23:36:52 +00:00
|
|
|
|
2024-04-21 15:54:59 +00:00
|
|
|
# Outputs
|
2020-04-24 23:36:52 +00:00
|
|
|
|
2024-04-21 15:54:59 +00:00
|
|
|
This function returns an attribute set with the following entries.
|
|
|
|
|
|
|
|
## optionsCommonMark
|
|
|
|
|
|
|
|
Documentation in CommonMark text format.
|
|
|
|
|
|
|
|
## optionsJSON
|
|
|
|
|
|
|
|
All options in a JSON format suitable for further automated processing.
|
|
|
|
|
|
|
|
`example.json`
|
|
|
|
```json
|
|
|
|
{
|
|
|
|
...
|
|
|
|
"fileSystems.<name>.options": {
|
|
|
|
"declarations": ["nixos/modules/tasks/filesystems.nix"],
|
|
|
|
"default": {
|
|
|
|
"_type": "literalExpression",
|
|
|
|
"text": "[\n \"defaults\"\n]"
|
|
|
|
},
|
|
|
|
"description": "Options used to mount the file system.",
|
|
|
|
"example": {
|
|
|
|
"_type": "literalExpression",
|
|
|
|
"text": "[\n \"data=journal\"\n]"
|
|
|
|
},
|
|
|
|
"loc": ["fileSystems", "<name>", "options"],
|
|
|
|
"readOnly": false,
|
|
|
|
"type": "non-empty (list of string (with check: non-empty))"
|
|
|
|
"relatedPackages": "- [`pkgs.tmux`](\n https://search.nixos.org/packages?show=tmux&sort=relevance&query=tmux\n )\n",
|
|
|
|
},
|
|
|
|
...
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
## optionsDocBook
|
|
|
|
|
|
|
|
deprecated since 23.11 and will be removed in 24.05.
|
|
|
|
|
|
|
|
## optionsAsciiDoc
|
|
|
|
|
|
|
|
Documentation rendered as AsciiDoc. This is useful for e.g. man pages.
|
|
|
|
|
|
|
|
> Note: NixOS itself uses this ouput to to build the configuration.nix man page"
|
|
|
|
|
|
|
|
## optionsNix
|
|
|
|
|
|
|
|
All options as a Nix attribute set value, with the same schema as `optionsJSON`.
|
|
|
|
|
|
|
|
# Example
|
|
|
|
|
|
|
|
## Example: NixOS configuration
|
|
|
|
|
|
|
|
```nix
|
|
|
|
let
|
|
|
|
# Evaluate a NixOS configuration
|
|
|
|
eval = import (pkgs.path + "/nixos/lib/eval-config.nix") {
|
|
|
|
# Overriden explicitly here, this would include all modules from NixOS otherwise.
|
|
|
|
# See: docs of eval-config.nix for more details
|
|
|
|
baseModules = [];
|
|
|
|
modules = [
|
|
|
|
./module.nix
|
|
|
|
];
|
|
|
|
};
|
|
|
|
in
|
|
|
|
pkgs.nixosOptionsDoc {
|
|
|
|
inherit (eval) options;
|
2020-04-24 23:36:52 +00:00
|
|
|
}
|
2024-04-21 15:54:59 +00:00
|
|
|
```
|
|
|
|
|
|
|
|
## Example: non-NixOS modules
|
|
|
|
|
|
|
|
`nixosOptionsDoc` can also be used to build documentation for non-NixOS modules.
|
2020-04-24 23:36:52 +00:00
|
|
|
|
2024-04-21 15:54:59 +00:00
|
|
|
```nix
|
|
|
|
let
|
|
|
|
eval = lib.evalModules {
|
|
|
|
modules = [
|
|
|
|
./module.nix
|
|
|
|
];
|
|
|
|
};
|
|
|
|
in
|
|
|
|
pkgs.nixosOptionsDoc {
|
|
|
|
inherit (eval) options;
|
|
|
|
}
|
|
|
|
```
|
2020-04-24 23:36:52 +00:00
|
|
|
*/
|
|
|
|
{ pkgs
|
|
|
|
, lib
|
|
|
|
, options
|
2022-12-28 21:21:41 +00:00
|
|
|
, transformOptions ? lib.id # function for additional transformations of the options
|
2022-06-16 17:23:12 +00:00
|
|
|
, documentType ? "appendix" # TODO deprecate "appendix" in favor of "none"
|
|
|
|
# and/or rename function to moduleOptionDoc for clean slate
|
2022-07-14 12:49:19 +00:00
|
|
|
|
|
|
|
# If you include more than one option list into a document, you need to
|
|
|
|
# provide different ids.
|
|
|
|
, variablelistId ? "configuration-variable-list"
|
2022-12-17 10:02:37 +00:00
|
|
|
# String to prefix to the option XML/HTML id attributes.
|
2022-10-06 18:32:54 +00:00
|
|
|
, optionIdPrefix ? "opt-"
|
2020-04-24 23:36:52 +00:00
|
|
|
, revision ? "" # Specify revision for the options
|
2022-01-07 04:07:37 +00:00
|
|
|
# a set of options the docs we are generating will be merged into, as if by recursiveUpdate.
|
|
|
|
# used to split the options doc build into a static part (nixos/modules) and a dynamic part
|
|
|
|
# (non-nixos modules imported via configuration.nix, other module sources).
|
|
|
|
, baseOptionsJSON ? null
|
|
|
|
# instead of printing warnings for eg options with missing descriptions (which may be lost
|
|
|
|
# by nix build unless -L is given), emit errors instead and fail the build
|
|
|
|
, warningsAreErrors ? true
|
2022-09-11 15:47:08 +00:00
|
|
|
# allow docbook option docs if `true`. only markdown documentation is allowed when set to
|
|
|
|
# `false`, and a different renderer may be used with different bugs and performance
|
|
|
|
# characteristics but (hopefully) indistinguishable output.
|
2023-07-15 17:15:38 +00:00
|
|
|
# deprecated since 23.11.
|
|
|
|
# TODO remove in a while.
|
|
|
|
, allowDocBook ? false
|
2024-04-21 15:54:59 +00:00
|
|
|
# TODO remove in a while (see https://github.com/NixOS/nixpkgs/issues/300735)
|
2023-07-15 17:15:38 +00:00
|
|
|
, markdownByDefault ? true
|
2020-04-24 23:36:52 +00:00
|
|
|
}:
|
|
|
|
|
2023-07-15 17:15:38 +00:00
|
|
|
assert markdownByDefault && ! allowDocBook;
|
|
|
|
|
2020-04-24 23:36:52 +00:00
|
|
|
let
|
2022-06-16 17:23:12 +00:00
|
|
|
rawOpts = lib.optionAttrSetToDocList options;
|
|
|
|
transformedOpts = map transformOptions rawOpts;
|
|
|
|
filteredOpts = lib.filter (opt: opt.visible && !opt.internal) transformedOpts;
|
|
|
|
optionsList = lib.flip map filteredOpts
|
|
|
|
(opt: opt
|
2020-08-20 17:08:02 +00:00
|
|
|
// lib.optionalAttrs (opt ? relatedPackages && opt.relatedPackages != []) { relatedPackages = genRelatedPackages opt.relatedPackages opt.name; }
|
2020-04-24 23:36:52 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
# Generate DocBook documentation for a list of packages. This is
|
|
|
|
# what `relatedPackages` option of `mkOption` from
|
|
|
|
# ../../../lib/options.nix influences.
|
|
|
|
#
|
|
|
|
# Each element of `relatedPackages` can be either
|
2022-01-07 04:07:37 +00:00
|
|
|
# - a string: that will be interpreted as an attribute name from `pkgs` and turned into a link
|
|
|
|
# to search.nixos.org,
|
|
|
|
# - a list: that will be interpreted as an attribute path from `pkgs` and turned into a link
|
|
|
|
# to search.nixos.org,
|
|
|
|
# - an attrset: that can specify `name`, `path`, `comment`
|
2020-04-24 23:36:52 +00:00
|
|
|
# (either of `name`, `path` is required, the rest are optional).
|
2022-01-07 04:07:37 +00:00
|
|
|
#
|
|
|
|
# NOTE: No checks against `pkgs` are made to ensure that the referenced package actually exists.
|
|
|
|
# Such checks are not compatible with option docs caching.
|
2020-08-20 17:08:02 +00:00
|
|
|
genRelatedPackages = packages: optName:
|
2020-04-24 23:36:52 +00:00
|
|
|
let
|
|
|
|
unpack = p: if lib.isString p then { name = p; }
|
|
|
|
else if lib.isList p then { path = p; }
|
|
|
|
else p;
|
|
|
|
describe = args:
|
|
|
|
let
|
|
|
|
title = args.title or null;
|
|
|
|
name = args.name or (lib.concatStringsSep "." args.path);
|
2022-01-07 04:07:37 +00:00
|
|
|
in ''
|
2023-02-09 11:40:11 +00:00
|
|
|
- [${lib.optionalString (title != null) "${title} aka "}`pkgs.${name}`](
|
2023-02-02 18:25:31 +00:00
|
|
|
https://search.nixos.org/packages?show=${name}&sort=relevance&query=${name}
|
|
|
|
)${
|
|
|
|
lib.optionalString (args ? comment) "\n\n ${args.comment}"
|
|
|
|
}
|
2022-01-07 04:07:37 +00:00
|
|
|
'';
|
2023-02-02 18:25:31 +00:00
|
|
|
in lib.concatMapStrings (p: describe (unpack p)) packages;
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
optionsNix = builtins.listToAttrs (map (o: { name = o.name; value = removeAttrs o ["name" "visible" "internal"]; }) optionsList);
|
|
|
|
|
2021-12-19 01:06:50 +00:00
|
|
|
in rec {
|
2020-04-24 23:36:52 +00:00
|
|
|
inherit optionsNix;
|
|
|
|
|
2023-02-22 10:55:15 +00:00
|
|
|
optionsAsciiDoc = pkgs.runCommand "options.adoc" {
|
|
|
|
nativeBuildInputs = [ pkgs.nixos-render-docs ];
|
|
|
|
} ''
|
|
|
|
nixos-render-docs -j $NIX_BUILD_CORES options asciidoc \
|
|
|
|
--manpage-urls ${pkgs.path + "/doc/manpage-urls.json"} \
|
|
|
|
--revision ${lib.escapeShellArg revision} \
|
2022-12-17 10:02:37 +00:00
|
|
|
${optionsJSON}/share/doc/nixos/options.json \
|
2023-02-22 10:55:15 +00:00
|
|
|
$out
|
2021-12-19 01:06:50 +00:00
|
|
|
'';
|
2020-04-24 23:36:52 +00:00
|
|
|
|
2023-02-22 10:55:15 +00:00
|
|
|
optionsCommonMark = pkgs.runCommand "options.md" {
|
|
|
|
nativeBuildInputs = [ pkgs.nixos-render-docs ];
|
|
|
|
} ''
|
|
|
|
nixos-render-docs -j $NIX_BUILD_CORES options commonmark \
|
|
|
|
--manpage-urls ${pkgs.path + "/doc/manpage-urls.json"} \
|
|
|
|
--revision ${lib.escapeShellArg revision} \
|
2022-12-17 10:02:37 +00:00
|
|
|
${optionsJSON}/share/doc/nixos/options.json \
|
2023-02-22 10:55:15 +00:00
|
|
|
$out
|
2021-12-19 01:06:50 +00:00
|
|
|
'';
|
2021-04-13 19:44:15 +00:00
|
|
|
|
2020-04-24 23:36:52 +00:00
|
|
|
optionsJSON = pkgs.runCommand "options.json"
|
|
|
|
{ meta.description = "List of NixOS options in JSON format";
|
2022-10-30 15:09:59 +00:00
|
|
|
nativeBuildInputs = [
|
2022-06-26 10:26:21 +00:00
|
|
|
pkgs.brotli
|
2024-01-13 08:15:51 +00:00
|
|
|
pkgs.python3
|
2022-06-26 10:26:21 +00:00
|
|
|
];
|
2022-01-07 04:07:37 +00:00
|
|
|
options = builtins.toFile "options.json"
|
|
|
|
(builtins.unsafeDiscardStringContext (builtins.toJSON optionsNix));
|
2022-09-11 15:47:08 +00:00
|
|
|
# merge with an empty set if baseOptionsJSON is null to run markdown
|
|
|
|
# processing on the input options
|
|
|
|
baseJSON =
|
|
|
|
if baseOptionsJSON == null
|
|
|
|
then builtins.toFile "base.json" "{}"
|
|
|
|
else baseOptionsJSON;
|
2020-04-24 23:36:52 +00:00
|
|
|
}
|
|
|
|
''
|
|
|
|
# Export list of options in different format.
|
|
|
|
dst=$out/share/doc/nixos
|
|
|
|
mkdir -p $dst
|
|
|
|
|
2023-02-02 18:25:31 +00:00
|
|
|
TOUCH_IF_DB=$dst/.used-docbook \
|
2022-09-11 15:47:08 +00:00
|
|
|
python ${./mergeJSON.py} \
|
|
|
|
${lib.optionalString warningsAreErrors "--warnings-are-errors"} \
|
|
|
|
$baseJSON $options \
|
|
|
|
> $dst/options.json
|
2020-04-24 23:36:52 +00:00
|
|
|
|
2023-07-15 17:15:38 +00:00
|
|
|
if grep /nixpkgs/nixos/modules $dst/options.json; then
|
2020-04-24 23:36:52 +00:00
|
|
|
echo "The manual appears to depend on the location of Nixpkgs, which is bad"
|
|
|
|
echo "since this prevents sharing via the NixOS channel. This is typically"
|
|
|
|
echo "caused by an option default that refers to a relative path (see above"
|
|
|
|
echo "for hints about the offending path)."
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2023-07-15 17:15:38 +00:00
|
|
|
brotli -9 < $dst/options.json > $dst/options.json.br
|
|
|
|
|
|
|
|
mkdir -p $out/nix-support
|
|
|
|
echo "file json $dst/options.json" >> $out/nix-support/hydra-build-products
|
|
|
|
echo "file json-br $dst/options.json.br" >> $out/nix-support/hydra-build-products
|
|
|
|
'';
|
|
|
|
|
2024-04-21 15:54:59 +00:00
|
|
|
optionsDocBook = throw "optionsDocBook has been removed in 24.05";
|
2020-04-24 23:36:52 +00:00
|
|
|
}
|