From 480737fc844ab42d9cfab9d7097976221707af1d Mon Sep 17 00:00:00 2001 From: atagen Date: Wed, 30 Jul 2025 15:33:59 +1000 Subject: [PATCH] refactor: expose nixosOptionsDoc args, add multiple output formats --- flake.nix | 65 ++++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 50 insertions(+), 15 deletions(-) diff --git a/flake.nix b/flake.nix index d662ca9..537c7af 100644 --- a/flake.nix +++ b/flake.nix @@ -6,8 +6,10 @@ # cheerfully lifted from nixos + hjem-rum doc gen outputs = - inputs: with inputs; { - lib.pak-chooie = + inputs: + with inputs; + let + rawEval = { pkgs, lib, @@ -16,6 +18,7 @@ newPath ? "https://my.site/unf/docs", specialArgs ? { }, modules ? [ ], + userOpts ? { }, }: let basePath = self.outPath; @@ -53,9 +56,12 @@ internal = true; }; } - ] ++ modules; + ] + ++ modules; }; - optionsDoc = pkgs.nixosOptionsDoc { + in + pkgs.nixosOptionsDoc ( + { inherit (eval) options; transformOptions = @@ -69,9 +75,12 @@ in map ( decl: - if hasPrefix (toString devDir) (toString decl) then + let + decl' = toString decl; + in + if hasPrefix devDir decl' then let - subpath = removePrefix "/" (removePrefix (toString devDir) (toString decl)); + subpath = removePrefix "/" (removePrefix devDir decl'); in { url = "${newPath}/${subpath}"; @@ -81,15 +90,41 @@ decl ) 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 - pkgs.runCommandLocal "${projectName}-docs" - { nativeBuildInputs = [ ndg.packages.${pkgs.system}.ndg ]; } - '' - ndg --verbose html --jobs $NIX_BUILD_CORES --title "${projectName}" \ - --module-options "${optionsDoc.optionsJSON}/share/doc/nixos/options.json" \ - --generate-search true \ - --output-dir "$out" - ''; + { + 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 + pkgs.runCommandLocal "${projectName}-docs" + { nativeBuildInputs = [ ndg.packages.${pkgs.system}.ndg ]; } + '' + ndg --verbose html --jobs $NIX_BUILD_CORES --title "${projectName}" \ + --module-options "${json}" \ + --generate-search true \ + --output-dir "$out" + ''; + }; }; }