unf/flake.nix
2025-05-25 15:53:27 +10:00

95 lines
2.9 KiB
Nix

{
inputs = {
nixpkgs.url = "github:nixos/nixpkgs/nixpkgs-unstable";
ndg.url = "github:feel-co/ndg";
};
# cheerfully lifted from nixos + hjem-rum doc gen
outputs =
inputs: with inputs; {
lib.pak-chooie =
{
pkgs,
lib,
projectName ? "unf",
basePath ? /home/me/myproject,
newPath ? "https://my.site/unf/docs",
specialArgs ? { },
modules ? [ ],
}:
let
eval = lib.evalModules {
inherit specialArgs;
modules = [
(
let
scrubDerivations =
namePrefix: pkgSet:
mapAttrs (
name: value:
let
wholeName = "${namePrefix}.${name}";
in
if isAttrs value then
scrubDerivations wholeName value
// optionalAttrs (isDerivation value) {
inherit (value) drvPath;
outPath = "\${${wholeName}}";
}
else
value
) pkgSet;
in
{
_module = {
check = false;
args.pkgs = mkForce (scrubDerivations "pkgs" pkgs);
};
}
)
{
options._module.args = lib.mkOption {
internal = true;
};
}
] ++ modules;
};
optionsDoc = pkgs.nixosOptionsDoc {
inherit (eval) options;
transformOptions =
opt:
opt
// {
declarations =
let
devDir = toString basePath;
inherit (lib) hasPrefix removePrefix;
in
map (
decl:
if hasPrefix (toString devDir) (toString decl) then
let
subpath = removePrefix "/" (removePrefix (toString devDir) (toString decl));
in
{
url = "${newpath}${subpath}";
name = subpath;
}
else
decl
) opt.declarations;
};
};
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"
'';
};
}