Merge pull request 'refactor: move docs transformOptions to clanLib to reduce rebuilds' (#4259) from self into main

Reviewed-on: https://git.clan.lol/clan/clan-core/pulls/4259
This commit is contained in:
hsjobeki
2025-07-09 15:31:25 +00:00
7 changed files with 54 additions and 10 deletions

View File

@@ -29,7 +29,10 @@
# Frontmatter for clanModules
clanModulesFrontmatter =
let
docs = pkgs.nixosOptionsDoc { options = self.clanLib.modules.frontmatterOptions; };
docs = pkgs.nixosOptionsDoc {
options = self.clanLib.modules.frontmatterOptions;
transformOptions = self.clanLib.docs.stripStorePathsFromDeclarations;
};
in
docs.optionsJSON;

View File

@@ -7,6 +7,10 @@
pkgs,
clan-core,
}:
let
inherit (clan-core.clanLib.docs) stripStorePathsFromDeclarations;
transformOptions = stripStorePathsFromDeclarations;
in
{
# clanModules docs
clanModulesViaNix = lib.mapAttrs (
@@ -20,6 +24,7 @@
}).options
).clan.${name} or { };
warningsAreErrors = true;
inherit transformOptions;
}).optionsJSON
else
{ }
@@ -32,6 +37,7 @@
(nixosOptionsDoc {
inherit options;
warningsAreErrors = true;
inherit transformOptions;
}).optionsJSON
) rolesOptions
) modulesRolesOptions;
@@ -52,7 +58,15 @@
(nixosOptionsDoc {
transformOptions =
opt: if lib.strings.hasPrefix "_" opt.name then opt // { visible = false; } else opt;
opt:
let
# Apply store path stripping first
transformed = transformOptions opt;
in
if lib.strings.hasPrefix "_" transformed.name then
transformed // { visible = false; }
else
transformed;
options = (lib.evalModules { modules = [ role.interface ]; }).options;
warningsAreErrors = true;
}).optionsJSON
@@ -72,5 +86,6 @@
}).options
).clan.core or { };
warningsAreErrors = true;
inherit transformOptions;
}).optionsJSON;
}

View File

@@ -62,14 +62,11 @@ def sanitize(text: str) -> str:
return text.replace(">", "\\>")
def replace_store_path(text: str) -> tuple[str, str]:
def replace_git_url(text: str) -> tuple[str, str]:
res = text
if text.startswith("/nix/store/"):
res = "https://git.clan.lol/clan/clan-core/src/branch/main/" + str(
Path(*Path(text).parts[4:])
)
# name = Path(res).name
name = str(Path(*Path(text).parts[4:]))
name = Path(res).name
if text.startswith("https://git.clan.lol/clan/clan-core/src/branch/main/"):
name = str(Path(*Path(text).parts[7:]))
return (res, name)
@@ -159,7 +156,7 @@ def render_option(
decls = option.get("declarations", [])
if decls:
source_path, name = replace_store_path(decls[0])
source_path, name = replace_git_url(decls[0])
name = name.split(",")[0]
source_path = source_path.split(",")[0]

View File

@@ -45,6 +45,7 @@ lib.fix (
introspection = import ./introspection { inherit lib; };
jsonschema = import ./jsonschema { inherit lib; };
facts = import ./facts.nix { inherit lib; };
docs = import ./docs.nix { inherit lib; };
# flakes
flakes = clanLib.callLib ./flakes.nix { };

25
lib/docs.nix Normal file
View File

@@ -0,0 +1,25 @@
{ lib, ... }:
rec {
prefix = "https://git.clan.lol/clan/clan-core/src/branch/main/";
# Strip store paths from option declarations to make docs more stable
# This prevents documentation from rebuilding when store paths change
# but the actual content remains the same
stripStorePathsFromDeclarations =
opt:
opt
// {
declarations = map (
decl:
if lib.isString decl && lib.hasPrefix "/nix/store/" decl then
let
parts = lib.splitString "/" decl;
in
if builtins.length parts > 4 then
(prefix + "/" + lib.concatStringsSep "/" (lib.drop 4 parts))
else
decl
else
decl
) opt.declarations;
};
}

View File

@@ -9,9 +9,11 @@ let
clan-core.modules.clan.default
];
};
evalDocs = pkgs.nixosOptionsDoc {
options = eval.options;
warningsAreErrors = false;
transformOptions = clan-core.clanLib.docs.stripStorePathsFromDeclarations;
};
in
{

View File

@@ -49,6 +49,7 @@ in
prefix = [ ];
}).options;
warningsAreErrors = true;
transformOptions = self.clanLib.docs.stripStorePathsFromDeclarations;
}).optionsJSON;
# Run: nix-unit --extra-experimental-features flakes --flake .#legacyPackages.x86_64-linux.evalTests