Skip to content

Commit c3e6935

Browse files
Merge pull request #6 from feel-co/robust-args
builder: more robust argument handling
2 parents 48d1138 + 3d8b821 commit c3e6935

1 file changed

Lines changed: 65 additions & 49 deletions

File tree

pkgs/builder.nix

Lines changed: 65 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
{
22
lib,
3-
# build dependencies
3+
# Build Dependencies
44
runCommandLocal,
55
pandoc,
66
nixosOptionsDoc,
77
ndg-stylesheet,
8-
# options
8+
# Options
9+
pandocCmd ? "pandoc",
910
genJsonDocs ? true,
1011
checkModules ? false,
1112
rawModules ? [
@@ -34,60 +35,75 @@
3435
templatePath ? ./assets/default-template.html,
3536
styleSheetPath ? ./assets/default-styles.scss,
3637
codeThemePath ? ./assets/default-syntax.theme,
38+
filters ? [],
3739
optionsDocArgs ? {},
3840
sandboxing ? true,
3941
embedResources ? false,
4042
generateLinkAnchors ? true,
41-
} @ args:
42-
assert args ? specialArgs -> args ? rawModules;
43-
assert args ? evaluatedModules -> !(args ? rawModules); let
44-
inherit (lib.strings) optionalString;
43+
outPath ? "share/doc",
44+
} @ args: let
45+
inherit (builtins) isString;
46+
inherit (lib.asserts) assertMsg;
47+
in
48+
# TODO explain this one
49+
assert args ? specialArgs -> args ? rawModules;
50+
assert assertMsg (args ? evaluatedModules -> !(args ? rawModules)) "evaluatedModules and rawModules are mutually exclusive";
51+
assert assertMsg (isString outPath) "outPath must be a string";
52+
# TODO assert that outPath doesn't escape $out
53+
let
54+
inherit (lib.strings) optionalString concatStringsSep;
55+
inherit (lib.lists) optionals;
4556

46-
configMD =
47-
(nixosOptionsDoc (
48-
(removeAttrs optionsDocArgs ["options"])
49-
// {inherit (evaluatedModules) options;}
50-
))
51-
.optionsCommonMark;
57+
configMD =
58+
(nixosOptionsDoc (
59+
(removeAttrs optionsDocArgs ["options"])
60+
// {inherit (evaluatedModules) options;}
61+
))
62+
.optionsCommonMark;
5263

53-
configJSON =
54-
(nixosOptionsDoc (
55-
(removeAttrs optionsDocArgs ["options"])
56-
// {inherit (evaluatedModules) options;}
57-
))
58-
.optionsJSON;
59-
in
60-
runCommandLocal "generate-option-docs.html" {nativeBuildInputs = [pandoc];} (
61-
''
62-
mkdir -p $out/share/doc
64+
configJSON =
65+
(nixosOptionsDoc (
66+
(removeAttrs optionsDocArgs ["options"])
67+
// {inherit (evaluatedModules) options;}
68+
))
69+
.optionsJSON;
70+
71+
pandocArgs = {
72+
luaFilters = filters ++ optionals generateLinkAnchors [./assets/filters/anchor.lua];
73+
finalOutPath = "$out/${outPath}";
74+
};
75+
in
76+
runCommandLocal "generate-option-docs.html" {nativeBuildInputs = [pandoc];} (
77+
''
78+
mkdir -p ${pandocArgs.finalOutPath}
6379
64-
${optionalString genJsonDocs ''
65-
cp -vf ${configJSON}/share/doc/nixos/options.json $out/share/doc/options.json
66-
''}
80+
${optionalString genJsonDocs ''
81+
cp -vf ${configJSON}/share/doc/nixos/options.json $out/share/doc/options.json
82+
''}
6783
68-
# Convert to Pandoc markdown instead of using commonmark directly
69-
# as the former automatically generates heading IDs and TOC links.
70-
pandoc \
71-
--from commonmark \
72-
--to markdown \
73-
${configMD} |
84+
# Convert to Pandoc markdown instead of using commonmark directly
85+
# as the former automatically generates heading IDs and TOC links.
86+
${pandocCmd} \
87+
--from commonmark \
88+
--to markdown \
89+
${configMD} |
7490
7591
76-
# Convert Pandoc markdown to HTML using our own template and css files
77-
# where available. --sandbox is passed for extra security by default
78-
# with an optional override to disable it.
79-
pandoc \
80-
--from markdown \
81-
--to html \
82-
--metadata title="${title}" \
83-
--toc \
84-
--standalone \
85-
''
86-
+ optionalString generateLinkAnchors ''--lua-filter=${./assets/filters/anchor.lua} \''
87-
+ optionalString embedResources ''--self-contained \''
88-
+ optionalString sandboxing ''--sandbox \''
89-
+ optionalString (templatePath != null) ''--template ${templatePath} \''
90-
+ optionalString (styleSheetPath != null) ''--css ${ndg-stylesheet.override {inherit styleSheetPath;}} \''
91-
+ optionalString (codeThemePath != null) ''--highlight-style ${codeThemePath} \''
92-
+ "-o $out/share/doc/index.html"
93-
)
92+
# Convert Pandoc markdown to HTML using our own template and css files
93+
# where available. --sandbox is passed for extra security by default
94+
# with an optional override to disable it.
95+
${pandocCmd} \
96+
--from markdown \
97+
--to html \
98+
--metadata title="${title}" \
99+
--toc \
100+
--standalone \
101+
''
102+
+ optionalString (pandocArgs.luaFilters != []) ''${concatStringsSep " " (map (f: "--lua-filter=" + f) pandocArgs.luaFilters)} \''
103+
+ optionalString embedResources ''--embed-resources --standalone \''
104+
+ optionalString sandboxing ''--sandbox \''
105+
+ optionalString (templatePath != null) ''--template ${templatePath} \''
106+
+ optionalString (styleSheetPath != null) ''--css ${ndg-stylesheet.override {inherit styleSheetPath;}} \''
107+
+ optionalString (codeThemePath != null) ''--highlight-style ${codeThemePath} \''
108+
+ "-o ${pandocArgs.finalOutPath}/index.html"
109+
)

0 commit comments

Comments
 (0)