config-perso/machines/kat-son/doc/default.nix

126 lines
3.2 KiB
Nix
Raw Normal View History

2024-12-11 17:03:41 +01:00
{
lib,
config,
pkgs,
...
}:
let
inherit (lib)
mkOption
mkEnableOption
mkIf
types
importJSON
filterAttrs
mapAttrs'
mapAttrsToList
removePrefix
pathIsDirectory
hasSuffix
;
inherit (lib.strings)
sanitizeDerivationName
;
yaml = pkgs.formats.yaml { };
json = pkgs.formats.json { };
cfg = config.services.extranix;
module-eval =
module:
let
eval = lib.evalModules { modules = module.paths; };
opts-doc = pkgs.nixosOptionsDoc { inherit (eval) options; };
val = importJSON "${opts-doc.optionsJSON}/share/doc/nixos/options.json";
filtered-opts = filterAttrs (name: _: name != "_module.args") val;
result = json.generate "options-extranix.json" {
last_update = "-/-";
options = mapAttrsToList (title: val: {
inherit title;
inherit (val)
type
readOnly
description
loc
;
example = val.example.text or "";
default = val.default.text or "";
declarations = map (
decl:
let
baseString1 = toString module.base;
baseString = baseString1 + (if hasSuffix "/" baseString1 then "" else "/");
innerPath1 = if pathIsDirectory decl then decl + "/default.nix" else decl;
innerPath = removePrefix baseString innerPath1;
in
{
name = "<${innerPath}>";
url = "${module.url}${if hasSuffix "/" module.url then "" else "/"}${innerPath}";
}
) val.declarations;
}) filtered-opts;
};
in
result;
options-files = mapAttrs' (name: value: {
name = sanitizeDerivationName name;
value = module-eval value;
}) cfg.modules;
webroot = pkgs.callPackage ./webroot.nix {
inherit options-files;
inherit (cfg) static-data;
settings = yaml.generate "config.yaml" cfg.settings;
hugo-theme-extranix-options-search = pkgs.callPackage ./hugo-theme-extranix-options-search.nix { };
};
in
{
options.services.extranix = {
enable = mkEnableOption "extranix documentation";
modules = mkOption {
type =
let
module-mod.options = {
paths = mkOption {
type = types.listOf types.path;
};
base = mkOption {
type = types.path;
};
url = mkOption {
type = types.str;
};
};
in
types.attrsOf (types.submodule module-mod);
};
settings = mkOption {
type = yaml.type;
};
static-data = mkOption {
type = types.path;
};
host = mkOption {
type = types.str;
};
};
config = mkIf cfg.enable {
services = {
extranix = {
settings = {
markup.goldmark.renderer.unsafe = true;
theme = "extranix-options-search";
params.releases = mapAttrsToList (name: _: {
inherit name;
value = sanitizeDerivationName name;
}) cfg.modules;
};
};
nginx = {
enable = true;
virtualHosts.${cfg.host}.locations."/".alias = "${webroot}/";
};
};
};
}