Files
proxy-suite-flake/nix/options-doc.nix
T

287 lines
7.3 KiB
Nix

{
nixpkgs,
pkgsFor,
proxySuiteModule,
}:
system:
let
pkgs = pkgsFor system;
lib = pkgs.lib;
eval = import "${nixpkgs}/nixos/lib/eval-config.nix" {
inherit system;
modules = [
proxySuiteModule
{
system.stateVersion = lib.trivial.release;
}
];
};
filterGeneratedOptions = lib.filterAttrs (name: _: name != "_module");
singBoxPackagePlaceholder = "__proxy_suite_doc_pkg_sing_box__";
xrayPackagePlaceholder = "__proxy_suite_doc_pkg_xray__";
sanitizeConfigForDocs =
config:
builtins.removeAttrs config [ "singBox" ]
// {
proxy =
(config.proxy or { })
// {
singBox =
((config.proxy or { }).singBox or { })
// lib.optionalAttrs (((config.proxy or { }).singBox or { }) ? package) {
package = singBoxPackagePlaceholder;
};
xray =
((config.proxy or { }).xray or { })
// lib.optionalAttrs (((config.proxy or { }).xray or { }) ? package) {
package = xrayPackagePlaceholder;
};
};
};
renderConfigText =
config:
lib.replaceStrings
[
"\"${singBoxPackagePlaceholder}\""
"\"${xrayPackagePlaceholder}\""
]
[
"pkgs.sing-box"
"pkgs.xray"
]
(
lib.generators.toPretty { } (sanitizeConfigForDocs config)
);
mkGeneratedExampleValue =
option:
if (option ? _type) && option._type == "option" then
if option ? example then
option.example
else if (option ? type) && ((option.type.name or null) == "submodule") then
lib.mapAttrs (_: mkGeneratedExampleValue) (filterGeneratedOptions (option.type.getSubOptions [ ]))
else if option ? default then
option.default
else
throw "Cannot generate example value for option ${lib.showOption option.loc}"
else
lib.mapAttrs (_: mkGeneratedExampleValue) (filterGeneratedOptions option);
defaultConfigText = renderConfigText eval.config.services.proxy-suite;
generatedExampleConfig =
lib.mapAttrs (_: mkGeneratedExampleValue) (filterGeneratedOptions eval.options.services.proxy-suite);
exampleConfigText = renderConfigText (builtins.removeAttrs generatedExampleConfig [ "singBox" ]);
repoRoot = toString ../.;
repoUrl = "https://github.com/FUFSoB/proxy-suite-flake/blob/main";
transformDeclaration =
decl:
let
declStr = toString decl;
subpath = lib.removePrefix "/" (lib.removePrefix repoRoot declStr);
in
if lib.hasPrefix repoRoot declStr then
{
url = "${repoUrl}/${subpath}";
name = subpath;
}
else
decl;
optionDocs = pkgs.nixosOptionsDoc {
options = {
services = {
"proxy-suite" = eval.options.services.proxy-suite;
};
};
documentType = "none";
variablelistId = "proxy-suite-options";
optionIdPrefix = "proxy-suite-opt-";
transformOptions = opt: opt // { declarations = map transformDeclaration opt.declarations; };
};
in
pkgs.runCommand "proxy-suite-options.md" { } ''
preamble="$TMPDIR/preamble.md"
sections="$TMPDIR/sections.md"
sections_with_anchors="$TMPDIR/sections-with-anchors.md"
section_index="$TMPDIR/sections-index.tsv"
toc="$TMPDIR/toc.md"
cat >"$preamble" <<'EOF'
# proxy-suite options
This file is generated from the `services.proxy-suite` option descriptions in [`modules/proxy-suite/options/default.nix`](/modules/proxy-suite/options/default.nix).
Update module option docs there instead of editing this file by hand.
## Table of contents
EOF
cat >"$sections" <<'EOF'
## Complete default config
```nix
services.proxy-suite = ${defaultConfigText};
```
## Complete example config
This is generated by filling in all the options with example values (or defaults if no example is provided). This is not meant to be a recommended config, just a comprehensive example of how to set all the options.
```nix
services.proxy-suite = ${exampleConfigText};
```
EOF
cat ${optionDocs.optionsCommonMark} >>"$sections"
: >"$section_index"
: >"$sections_with_anchors"
awk -v section_index="$section_index" -v out="$sections_with_anchors" '
function slugify(text, slug) {
slug = text
gsub(/\\/, "", slug)
slug = tolower(slug)
gsub(/[^a-z0-9]+/, "-", slug)
sub(/^-+/, "", slug)
sub(/-+$/, "", slug)
return slug
}
/^## / {
title = substr($0, 4)
base = slugify(title)
count = seen[base]
seen[base]++
slug = count == 0 ? base : base "-" count
print title "\t" slug >> section_index
print "<a id=\"" slug "\"></a>" >> out
}
{
print >> out
}
' "$sections"
: >"$toc"
awk -F '\t' -v toc="$toc" '
function addChild(parent, child, key) {
key = parent SUBSEP child
if (!(key in seenChild)) {
seenChild[key] = 1
childCount[parent]++
childOrder[parent, childCount[parent]] = child
}
}
function indent(depth, i, prefix) {
prefix = ""
for (i = 0; i < depth; i++) {
prefix = prefix " "
}
return prefix
}
function displayLabel(label) {
if (label == "*") {
return "item"
}
return label
}
function printNode(path, label, depth, i, child, childPath, text) {
text = displayLabel(label)
if (path in slugByPath) {
text = "[" text "](#" slugByPath[path] ")"
}
print indent(depth) "- " text >> toc
for (i = 1; i <= childCount[path]; i++) {
child = childOrder[path, i]
childPath = path "." child
printNode(childPath, child, depth + 1)
}
}
{
title = $1
slug = $2
normalized = title
gsub(/\\/, "", normalized)
if (normalized ~ /^Complete /) {
sampleCount++
sampleTitle[sampleCount] = title
sampleSlug[sampleCount] = slug
next
}
if (index(normalized, "services.proxy-suite") == 1) {
optionRootSeen = 1
slugByPath[normalized] = slug
count = split(normalized, parts, /\./)
if (count < 3) {
next
}
parent = "services.proxy-suite"
for (i = 3; i <= count; i++) {
child = parts[i]
addChild(parent, child)
parent = parent "." child
}
next
}
otherCount++
otherTitle[otherCount] = title
otherSlug[otherCount] = slug
}
END {
if (sampleCount > 0) {
print "- Config samples" >> toc
for (i = 1; i <= sampleCount; i++) {
print " - [" sampleTitle[i] "](#" sampleSlug[i] ")" >> toc
}
}
if (optionRootSeen) {
print "- services.proxy-suite" >> toc
for (i = 1; i <= childCount["services.proxy-suite"]; i++) {
child = childOrder["services.proxy-suite", i]
printNode("services.proxy-suite." child, child, 1)
}
}
for (i = 1; i <= otherCount; i++) {
print "- [" otherTitle[i] "](#" otherSlug[i] ")" >> toc
}
}
' "$section_index"
raw="$TMPDIR/options.md.raw"
cat "$preamble" >"$raw"
cat "$toc" >>"$raw"
printf '\n' >>"$raw"
cat "$sections_with_anchors" >>"$raw"
awk '
NF {
for (i = 0; i < blanks; i++) {
print ""
}
blanks = 0
print
next
}
{
blanks++
}
' "$raw" >"$out"
''