unf/flake.nix

130 lines
3.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;
let
rawEval =
{
pkgs,
lib,
projectName ? "unf",
self ? null,
newPath ? "https://my.site/unf/docs",
specialArgs ? { },
modules ? [ ],
userOpts ? { },
}:
let
basePath = self.outPath;
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;
};
in
pkgs.nixosOptionsDoc (
{
inherit (eval) options;
transformOptions =
opt:
opt
// {
declarations =
let
devDir = toString basePath;
inherit (lib) hasPrefix removePrefix;
in
map (
decl:
let
decl' = toString decl;
in
if hasPrefix devDir decl' then
let
subpath = removePrefix "/" (removePrefix devDir decl');
in
{
url = "${newPath}/${subpath}";
name = subpath;
}
else
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
{
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"
'';
};
};
}