Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

builder: more robust argument handling #6

Merged
merged 4 commits into from
Mar 4, 2025
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
builder: enforce that outPath is a string
eclairevoyant committed Mar 4, 2025

Verified

This commit was signed with the committer’s verified signature.
eclairevoyant éclairevoyant
commit 3d8b8213a8c2311730140666616c848c5c053f53
111 changes: 59 additions & 52 deletions pkgs/builder.nix
Original file line number Diff line number Diff line change
@@ -41,62 +41,69 @@
embedResources ? false,
generateLinkAnchors ? true,
outPath ? "share/doc",
} @ args:
assert args ? specialArgs -> args ? rawModules;
assert args ? evaluatedModules -> !(args ? rawModules); let
inherit (lib.strings) optionalString concatStringsSep;
inherit (lib.lists) optionals;
} @ args: let
inherit (builtins) isString;
inherit (lib.asserts) assertMsg;
in
# TODO explain this one
assert args ? specialArgs -> args ? rawModules;
assert assertMsg (args ? evaluatedModules -> !(args ? rawModules)) "evaluatedModules and rawModules are mutually exclusive";
assert assertMsg (isString outPath) "outPath must be a string";
# TODO assert that outPath doesn't escape $out
let
inherit (lib.strings) optionalString concatStringsSep;
inherit (lib.lists) optionals;

configMD =
(nixosOptionsDoc (
(removeAttrs optionsDocArgs ["options"])
// {inherit (evaluatedModules) options;}
))
.optionsCommonMark;
configMD =
(nixosOptionsDoc (
(removeAttrs optionsDocArgs ["options"])
// {inherit (evaluatedModules) options;}
))
.optionsCommonMark;

configJSON =
(nixosOptionsDoc (
(removeAttrs optionsDocArgs ["options"])
// {inherit (evaluatedModules) options;}
))
.optionsJSON;
configJSON =
(nixosOptionsDoc (
(removeAttrs optionsDocArgs ["options"])
// {inherit (evaluatedModules) options;}
))
.optionsJSON;

pandocArgs = {
luaFilters = filters ++ optionals generateLinkAnchors [./assets/filters/anchor.lua];
finalOutPath = "$out/${toString outPath}"; # toString handles 'null' case
};
in
runCommandLocal "generate-option-docs.html" {nativeBuildInputs = [pandoc];} (
''
mkdir -p ${pandocArgs.finalOutPath}
pandocArgs = {
luaFilters = filters ++ optionals generateLinkAnchors [./assets/filters/anchor.lua];
finalOutPath = "$out/${outPath}";
};
in
runCommandLocal "generate-option-docs.html" {nativeBuildInputs = [pandoc];} (
''
mkdir -p ${pandocArgs.finalOutPath}
${optionalString genJsonDocs ''
cp -vf ${configJSON}/share/doc/nixos/options.json $out/share/doc/options.json
''}
${optionalString genJsonDocs ''
cp -vf ${configJSON}/share/doc/nixos/options.json $out/share/doc/options.json
''}
# Convert to Pandoc markdown instead of using commonmark directly
# as the former automatically generates heading IDs and TOC links.
${pandocCmd} \
--from commonmark \
--to markdown \
${configMD} |
# Convert to Pandoc markdown instead of using commonmark directly
# as the former automatically generates heading IDs and TOC links.
${pandocCmd} \
--from commonmark \
--to markdown \
${configMD} |
# Convert Pandoc markdown to HTML using our own template and css files
# where available. --sandbox is passed for extra security by default
# with an optional override to disable it.
${pandocCmd} \
--from markdown \
--to html \
--metadata title="${title}" \
--toc \
--standalone \
''
+ optionalString (pandocArgs.luaFilters != []) ''${concatStringsSep " " (map (f: "--lua-filter=" + f) pandocArgs.luaFilters)} \''
+ optionalString embedResources ''--embed-resources --standalone \''
+ optionalString sandboxing ''--sandbox \''
+ optionalString (templatePath != null) ''--template ${templatePath} \''
+ optionalString (styleSheetPath != null) ''--css ${ndg-stylesheet.override {inherit styleSheetPath;}} \''
+ optionalString (codeThemePath != null) ''--highlight-style ${codeThemePath} \''
+ "-o ${pandocArgs.finalOutPath}/index.html"
)
# Convert Pandoc markdown to HTML using our own template and css files
# where available. --sandbox is passed for extra security by default
# with an optional override to disable it.
${pandocCmd} \
--from markdown \
--to html \
--metadata title="${title}" \
--toc \
--standalone \
''
+ optionalString (pandocArgs.luaFilters != []) ''${concatStringsSep " " (map (f: "--lua-filter=" + f) pandocArgs.luaFilters)} \''
+ optionalString embedResources ''--embed-resources --standalone \''
+ optionalString sandboxing ''--sandbox \''
+ optionalString (templatePath != null) ''--template ${templatePath} \''
+ optionalString (styleSheetPath != null) ''--css ${ndg-stylesheet.override {inherit styleSheetPath;}} \''
+ optionalString (codeThemePath != null) ''--highlight-style ${codeThemePath} \''
+ "-o ${pandocArgs.finalOutPath}/index.html"
)