docs: extract scrubDerivations function from nmd

This commit is contained in:
NotAShelf 2024-04-09 11:16:23 +03:00
parent 576429ba5c
commit b928f0bc4e
No known key found for this signature in database
GPG key ID: 02D1DD3FA08B6B29

View file

@ -4,18 +4,37 @@
lib ? import ../lib/stdlib-extended.nix pkgs.lib inputs, lib ? import ../lib/stdlib-extended.nix pkgs.lib inputs,
... ...
}: let }: let
nmd = import inputs.nmd { inherit (lib.modules) mkForce evalModules;
inherit lib; inherit (lib.strings) hasPrefix removePrefix;
# The DocBook output of `nixos-render-docs` doesn't have the change inherit (lib.attrsets) isAttrs mapAttrs optionalAttrs recursiveUpdate isDerivation;
# `nmd` uses to work around the broken stylesheets in
# `docbook-xsl-ns`, so we restore the patched version here. # From home-manager:
pkgs = #
pkgs # Recursively replace each derivation in the given attribute set
// { # with the same derivation but with the `outPath` attribute set to
docbook-xsl-ns = # the string `"\${pkgs.attribute.path}"`. This allows the
pkgs.docbook-xsl-ns.override {withManOptDedupPatch = true;}; # documentation to refer to derivations through their values without
}; # establishing an actual dependency on the derivation output.
}; #
# This is not perfect, but it seems to cover a vast majority of use
# cases.
#
# Caveat: even if the package is reached by a different means, the
# path above will be shown and not e.g.
# `${config.services.foo.package}`.
scrubDerivations = prefixPath: attrs: let
scrubDerivation = name: value: let
pkgAttrName = prefixPath + "." + name;
in
if isAttrs value
then
scrubDerivations pkgAttrName value
// optionalAttrs (isDerivation value) {
outPath = "\${${pkgAttrName}}";
}
else value;
in
mapAttrs scrubDerivation attrs;
# Make sure the used package is scrubbed to avoid actually # Make sure the used package is scrubbed to avoid actually
# instantiating derivations. # instantiating derivations.
@ -23,30 +42,30 @@
imports = [ imports = [
{ {
_module.args = { _module.args = {
pkgs = lib.mkForce (nmd.scrubDerivations "pkgs" pkgs); pkgs = mkForce (scrubDerivations "pkgs" pkgs);
pkgs_i686 = lib.mkForce {}; pkgs_i686 = mkForce {};
}; };
} }
]; ];
}; };
dontCheckDefinitions = {_module.check = false;}; # Specify the path to the module entrypoint
githubDeclaration = user: repo: subpath: let
urlRef = "main";
in {
url = "https://github.com/${user}/${repo}/blob/${urlRef}/${subpath}";
name = "<${repo}/${subpath}>";
};
nvimPath = toString ./..; nvimPath = toString ./..;
buildOptionsDocs = args @ { buildOptionsDocs = args @ {
modules, modules,
includeModuleSystemOptions ? true, includeModuleSystemOptions ? true,
... ...
}: let }: let
inherit ((lib.evalModules {inherit modules;})) options; inherit ((evalModules {inherit modules;})) options;
# Declaration of the Github site URL.
githubDeclaration = user: repo: subpath: let
urlRef = "github.com";
branch = "main";
in {
url = "https://${urlRef}/${user}/${repo}/blob/${branch}/${subpath}";
name = "<${repo}/${subpath}>";
};
in in
pkgs.buildPackages.nixosOptionsDoc ({ pkgs.buildPackages.nixosOptionsDoc ({
options = options =
@ -54,15 +73,14 @@
then options then options
else builtins.removeAttrs options ["_module"]; else builtins.removeAttrs options ["_module"];
transformOptions = opt: transformOptions = opt:
opt recursiveUpdate opt {
// { # Clean up declaration sites to not refer to the neovim-flakee
# Clean up declaration sites to not refer to the Home Manager
# source tree. # source tree.
declarations = map (decl: declarations = map (decl:
if lib.hasPrefix nvimPath (toString decl) if hasPrefix nvimPath (toString decl)
then then
githubDeclaration "notashelf" "neovim-flake" githubDeclaration "notashelf" "neovim-flake"
(lib.removePrefix "/" (lib.removePrefix nvimPath (toString decl))) (removePrefix "/" (removePrefix nvimPath (toString decl)))
else if decl == "lib/modules.nix" else if decl == "lib/modules.nix"
then then
# TODO: handle this in a better way (may require upstream # TODO: handle this in a better way (may require upstream
@ -86,6 +104,7 @@
release-config = builtins.fromJSON (builtins.readFile ../release.json); release-config = builtins.fromJSON (builtins.readFile ../release.json);
revision = "release-${release-config.release}"; revision = "release-${release-config.release}";
# Generate the `man home-configuration.nix` package # Generate the `man home-configuration.nix` package
nvf-configuration-manual = nvf-configuration-manual =
pkgs.runCommand "neovim-flake-reference-manpage" { pkgs.runCommand "neovim-flake-reference-manpage" {
@ -95,12 +114,15 @@
# Generate manpages. # Generate manpages.
mkdir -p $out/share/man/man5 mkdir -p $out/share/man/man5
mkdir -p $out/share/man/man1 mkdir -p $out/share/man/man1
nixos-render-docs -j $NIX_BUILD_CORES options manpage \ nixos-render-docs -j $NIX_BUILD_CORES options manpage \
--revision ${revision} \ --revision ${revision} \
${nvimModuleDocs.optionsJSON}/share/doc/nixos/options.json \ ${nvimModuleDocs.optionsJSON}/share/doc/nixos/options.json \
$out/share/man/man5/neovim-flake.5 $out/share/man/man5/neovim-flake.5
cp ${./neovim-flake.1} $out/share/man/man1/neovim-flake.1 cp ${./neovim-flake.1} $out/share/man/man1/neovim-flake.1
''; '';
# Generate the HTML manual pages # Generate the HTML manual pages
neovim-flake-manual = pkgs.callPackage ./manual.nix { neovim-flake-manual = pkgs.callPackage ./manual.nix {
inherit (inputs) nmd; inherit (inputs) nmd;
@ -110,6 +132,7 @@
neovim-flake = nvimModuleDocs.optionsJSON; neovim-flake = nvimModuleDocs.optionsJSON;
}; };
}; };
html = neovim-flake-manual; html = neovim-flake-manual;
htmlOpenTool = pkgs.callPackage ./html-open-tool.nix {} {inherit html;}; htmlOpenTool = pkgs.callPackage ./html-open-tool.nix {} {inherit html;};
in { in {
@ -120,7 +143,7 @@ in {
# `nixosOptionsDoc` is more customizable. # `nixosOptionsDoc` is more customizable.
json = json =
pkgs.runCommand "options.json" { pkgs.runCommand "options.json" {
meta.description = "List of Home Manager options in JSON format"; meta.description = "List of neovim-flake options in JSON format";
} '' } ''
mkdir -p $out/{share/doc,nix-support} mkdir -p $out/{share/doc,nix-support}
cp -a ${nvimModuleDocs.optionsJSON}/share/doc/nixos $out/share/doc/neovim-flake cp -a ${nvimModuleDocs.optionsJSON}/share/doc/nixos $out/share/doc/neovim-flake