refactor: expose nixosOptionsDoc args, add multiple output formats

This commit is contained in:
atagen 2025-07-30 15:33:59 +10:00
parent 4d711e5002
commit 480737fc84

View file

@ -6,8 +6,10 @@
# cheerfully lifted from nixos + hjem-rum doc gen # cheerfully lifted from nixos + hjem-rum doc gen
outputs = outputs =
inputs: with inputs; { inputs:
lib.pak-chooie = with inputs;
let
rawEval =
{ {
pkgs, pkgs,
lib, lib,
@ -16,6 +18,7 @@
newPath ? "https://my.site/unf/docs", newPath ? "https://my.site/unf/docs",
specialArgs ? { }, specialArgs ? { },
modules ? [ ], modules ? [ ],
userOpts ? { },
}: }:
let let
basePath = self.outPath; basePath = self.outPath;
@ -53,9 +56,12 @@
internal = true; internal = true;
}; };
} }
] ++ modules; ]
++ modules;
}; };
optionsDoc = pkgs.nixosOptionsDoc { in
pkgs.nixosOptionsDoc (
{
inherit (eval) options; inherit (eval) options;
transformOptions = transformOptions =
@ -69,9 +75,12 @@
in in
map ( map (
decl: decl:
if hasPrefix (toString devDir) (toString decl) then
let let
subpath = removePrefix "/" (removePrefix (toString devDir) (toString decl)); decl' = toString decl;
in
if hasPrefix devDir decl' then
let
subpath = removePrefix "/" (removePrefix devDir decl');
in in
{ {
url = "${newPath}/${subpath}"; url = "${newPath}/${subpath}";
@ -81,15 +90,41 @@
decl decl
) opt.declarations; ) opt.declarations;
}; };
}; }
// userOpts
);
in
{
lib =
let
inherit (builtins) removeAttrs toJSON toXML;
raw = { pkgs, ... }@args: pkgs.callPackage rawEval (removeAttrs args [ "pkgs" ]);
withTransform =
fmt: transform:
{ pkgs, ... }@args:
pkgs.writeText "options.${fmt}" (transform (raw args).optionsNix);
in
{
md = args: (raw args).optionsCommonMark;
nix = args: (raw args).optionsNix;
json = withTransform "json" toJSON;
xml = withTransform "xml" toXML;
yaml = { pkgs, ... }@args: (pkgs.formats.yaml { }).generate "options.yaml" (raw args).optionsNix;
toml = { pkgs, ... }@args: (pkgs.formats.toml { }).generate "options.toml" (raw args).optionsNix;
html =
{ pkgs, projectName, ... }@args:
let
json = self.lib.json args;
in in
pkgs.runCommandLocal "${projectName}-docs" pkgs.runCommandLocal "${projectName}-docs"
{ nativeBuildInputs = [ ndg.packages.${pkgs.system}.ndg ]; } { nativeBuildInputs = [ ndg.packages.${pkgs.system}.ndg ]; }
'' ''
ndg --verbose html --jobs $NIX_BUILD_CORES --title "${projectName}" \ ndg --verbose html --jobs $NIX_BUILD_CORES --title "${projectName}" \
--module-options "${optionsDoc.optionsJSON}/share/doc/nixos/options.json" \ --module-options "${json}" \
--generate-search true \ --generate-search true \
--output-dir "$out" --output-dir "$out"
''; '';
}; };
};
} }