docs: add clan modules readme support

This commit is contained in:
Johannes Kirschbauer
2024-04-17 12:52:04 +02:00
parent 0dde758296
commit 87559613ed
6 changed files with 58 additions and 11 deletions

View File

@@ -18,7 +18,7 @@
sshd = ./sshd.nix; sshd = ./sshd.nix;
sunshine = ./sunshine.nix; sunshine = ./sunshine.nix;
syncthing = ./syncthing.nix; syncthing = ./syncthing.nix;
root-password = ./root-password.nix; root-password = ./root-password;
thelounge = ./thelounge.nix; thelounge = ./thelounge.nix;
vm-user = ./vm-user.nix; vm-user = ./vm-user.nix;
waypipe = ./waypipe.nix; waypipe = ./waypipe.nix;

View File

@@ -0,0 +1,13 @@
## Usage
!!! tip "This module sets the password for the root user (automatically)."
After the system was installed/deployed the following command can be used to display the root-password:
```bash
clan secrets get {machine_name}-password
```
---
See also: [Facts / Secrets](../../getting-started/secrets.md)

View File

@@ -12,12 +12,13 @@
# { clanCore = «derivation JSON»; clanModules = { ${name} = «derivation JSON» }; } # { clanCore = «derivation JSON»; clanModules = { ${name} = «derivation JSON» }; }
jsonDocs = import ./get-module-docs.nix { jsonDocs = import ./get-module-docs.nix {
inherit (inputs) nixpkgs; inherit (inputs) nixpkgs;
inherit pkgs; inherit pkgs self;
inherit (self.nixosModules) clanCore; inherit (self.nixosModules) clanCore;
inherit (self) clanModules; inherit (self) clanModules;
}; };
clanModulesFileInfo = pkgs.writeText "info.json" (builtins.toJSON jsonDocs.clanModules); clanModulesFileInfo = pkgs.writeText "info.json" (builtins.toJSON jsonDocs.clanModules);
clanModulesReadmes = pkgs.writeText "info.json" (builtins.toJSON jsonDocs.clanModulesReadmes);
# Simply evaluated options (JSON) # Simply evaluated options (JSON)
renderOptions = renderOptions =
@@ -43,6 +44,7 @@
export CLAN_CORE=${jsonDocs.clanCore}/share/doc/nixos/options.json export CLAN_CORE=${jsonDocs.clanCore}/share/doc/nixos/options.json
# A file that contains the links to all clanModule docs # A file that contains the links to all clanModule docs
export CLAN_MODULES=${clanModulesFileInfo} export CLAN_MODULES=${clanModulesFileInfo}
export CLAN_MODULES_READMES=${clanModulesReadmes}
mkdir $out mkdir $out
@@ -63,5 +65,8 @@
deploy-docs = pkgs.callPackage ./deploy-docs.nix { inherit (config.packages) docs; }; deploy-docs = pkgs.callPackage ./deploy-docs.nix { inherit (config.packages) docs; };
inherit module-docs; inherit module-docs;
}; };
legacyPackages = {
foo = jsonDocs;
};
}; };
} }

View File

@@ -3,6 +3,7 @@
pkgs, pkgs,
clanCore, clanCore,
clanModules, clanModules,
self,
}: }:
let let
allNixosModules = (import "${nixpkgs}/nixos/modules/module-list.nix") ++ [ allNixosModules = (import "${nixpkgs}/nixos/modules/module-list.nix") ++ [
@@ -36,10 +37,28 @@ let
name: module: (evalDocs ((getOptions [ module ]).clan.${name} or { })).optionsJSON name: module: (evalDocs ((getOptions [ module ]).clan.${name} or { })).optionsJSON
) clanModules; ) clanModules;
clanModulesReadmes = builtins.mapAttrs (
module_name: _module:
let
readme = "${self}/clanModules/${module_name}/README.md";
readmeContents =
if
builtins.trace "Trying to get Module README.md for ${module_name} from ${readme}"
# TODO: Edge cases
(builtins.pathExists readme)
then
(builtins.readFile readme)
else
null;
in
readmeContents
) clanModules;
# clanCore docs # clanCore docs
clanCoreDocs = (evalDocs (getOptions [ ]).clanCore).optionsJSON; clanCoreDocs = (evalDocs (getOptions [ ]).clanCore).optionsJSON;
in in
{ {
inherit clanModulesReadmes;
clanCore = clanCoreDocs; clanCore = clanCoreDocs;
clanModules = clanModulesDocs; clanModules = clanModulesDocs;
} }

View File

@@ -31,6 +31,8 @@ from typing import Any
# Get environment variables # Get environment variables
CLAN_CORE = os.getenv("CLAN_CORE") CLAN_CORE = os.getenv("CLAN_CORE")
CLAN_MODULES = os.environ.get("CLAN_MODULES") CLAN_MODULES = os.environ.get("CLAN_MODULES")
CLAN_MODULES_READMES = os.environ.get("CLAN_MODULES_READMES")
OUT = os.environ.get("out") OUT = os.environ.get("out")
@@ -74,9 +76,9 @@ def render_option(name: str, option: dict[str, Any]) -> str:
if example: if example:
res += f""" res += f"""
??? example ???+ example
```nix ```nix
{example} {example}
``` ```
""" """
@@ -121,9 +123,7 @@ def produce_clan_core_docs() -> None:
raise ValueError(f"Environment variables are not set correctly: $out={OUT}") raise ValueError(f"Environment variables are not set correctly: $out={OUT}")
# A mapping of output file to content # A mapping of output file to content
core_outputs: dict[str, str] = { core_outputs: dict[str, str] = {}
"clan-core/index.md": "",
}
with open(CLAN_CORE) as f: with open(CLAN_CORE) as f:
options: dict[str, dict[str, Any]] = json.load(f) options: dict[str, dict[str, Any]] = json.load(f)
module_name = "clan-core" module_name = "clan-core"
@@ -133,10 +133,10 @@ def produce_clan_core_docs() -> None:
# Create seperate files for nested options # Create seperate files for nested options
if len(option_name.split(".")) <= 2: if len(option_name.split(".")) <= 2:
# i.e. clan-core.clanDir # i.e. clan-core.clanDir
output = module_header(module_name) output = core_outputs.get(outfile, module_header(module_name))
output += render_option(option_name, info) output += render_option(option_name, info)
core_outputs[outfile] += output # Update the content
core_outputs[outfile] = output
else: else:
# Clan sub-options # Clan sub-options
[_, sub] = option_name.split(".")[0:2] [_, sub] = option_name.split(".")[0:2]
@@ -147,7 +147,6 @@ def produce_clan_core_docs() -> None:
# Update the content # Update the content
core_outputs[outfile] = output core_outputs[outfile] = output
print(core_outputs)
for outfile, output in core_outputs.items(): for outfile, output in core_outputs.items():
(Path(OUT) / outfile).parent.mkdir(parents=True, exist_ok=True) (Path(OUT) / outfile).parent.mkdir(parents=True, exist_ok=True)
with open(Path(OUT) / outfile, "w") as of: with open(Path(OUT) / outfile, "w") as of:
@@ -159,6 +158,10 @@ def produce_clan_modules_docs() -> None:
raise ValueError( raise ValueError(
f"Environment variables are not set correctly: $CLAN_MODULES={CLAN_MODULES}" f"Environment variables are not set correctly: $CLAN_MODULES={CLAN_MODULES}"
) )
if not CLAN_MODULES_READMES:
raise ValueError(
f"Environment variables are not set correctly: $CLAN_MODULES_READMES={CLAN_MODULES_READMES}"
)
if not OUT: if not OUT:
raise ValueError(f"Environment variables are not set correctly: $out={OUT}") raise ValueError(f"Environment variables are not set correctly: $out={OUT}")
@@ -166,12 +169,19 @@ def produce_clan_modules_docs() -> None:
with open(CLAN_MODULES) as f: with open(CLAN_MODULES) as f:
links: dict[str, str] = json.load(f) links: dict[str, str] = json.load(f)
with open(CLAN_MODULES_READMES) as readme:
readme_map: dict[str, str] = json.load(readme)
# {'borgbackup': '/nix/store/hi17dwgy7963ddd4ijh81fv0c9sbh8sw-options.json', ... } # {'borgbackup': '/nix/store/hi17dwgy7963ddd4ijh81fv0c9sbh8sw-options.json', ... }
for module_name, options_file in links.items(): for module_name, options_file in links.items():
with open(Path(options_file) / "share/doc/nixos/options.json") as f: with open(Path(options_file) / "share/doc/nixos/options.json") as f:
options: dict[str, dict[str, Any]] = json.load(f) options: dict[str, dict[str, Any]] = json.load(f)
print(f"Rendering options for {module_name}...") print(f"Rendering options for {module_name}...")
output = module_header(module_name) output = module_header(module_name)
if readme_map.get(module_name, None):
output += f"{readme_map[module_name]}\n"
for option_name, info in options.items(): for option_name, info in options.items():
output += render_option(option_name, info) output += render_option(option_name, info)