diff --git a/nix/fixup-options.py b/nix/fixup-options.py index a7183e2..9158e41 100644 --- a/nix/fixup-options.py +++ b/nix/fixup-options.py @@ -19,6 +19,15 @@ def code(code): print("ERROR: cannot handle a " + code["_type"], file=sys.stderr) sys.exit(1) + +def update_declaration(url, declaration): + if "url" in declaration: + return declaration["url"] + if declaration.startswith("/nix/store/"): + # strip prefix: /nix/store/0a0mxyfmad6kaknkkr0ysraifws856i7-source + return f"{url}{declaration[51:]}" + return declaration + out = [] for i in range(1,len(sys.argv),2): @@ -30,7 +39,7 @@ def code(code): entry = data[key] entry["name"] = key del entry["loc"] - entry["declarations"] = list(map(lambda x: f"{url}{x[51:]}",entry["declarations"])) + entry["declarations"] = list(map(lambda x: update_declaration(url, x), entry["declarations"])) entry["description"] = markdown.markdown(entry["description"]) if 'default' in entry: diff --git a/nix/wrapper.nix b/nix/wrapper.nix index f570b00..0df3ede 100644 --- a/nix/wrapper.nix +++ b/nix/wrapper.nix @@ -1,4 +1,4 @@ -{ lib, nixosOptionsDoc, nuscht-search, python3, runCommand, xorg }: +{ lib, nixosOptionsDoc, jq, nuscht-search, python3, runCommand, xorg }: rec { mkOptionsJSON = modules: @@ -11,7 +11,16 @@ rec { warningsAreErrors = false; }).optionsJSON + /share/doc/nixos/options.json; - mkSearchJSON = listOfModuleAndUrlPrefix: + mkSearchJSON = searchArgs: + let + optionsJSON = opt: opt.optionsJSON or (mkOptionsJSON opt.modules); + optionsJSONPrefixed = opt: if opt?optionsJSON then (runCommand "options.json-prefixed" { + nativeBuildInputs = [ jq ]; + } '' + mkdir $out + jq -r 'with_entries(.key as $key | .key |= "${opt.optionsPrefix}.\($key)")' ${optionsJSON opt} > $out/options.json + '') + /options.json else optionsJSON opt; + in runCommand "options.json" { nativeBuildInputs = [ (python3.withPackages (ps: with ps; [ markdown pygments ])) ]; } ('' @@ -19,26 +28,36 @@ rec { python \ ${./fixup-options.py} \ '' + lib.concatStringsSep " " (lib.flatten (map (opt: [ - (mkOptionsJSON opt.modules) "'${opt.urlPrefix}'" - ]) listOfModuleAndUrlPrefix)) + '' + (optionsJSONPrefixed opt) "'${opt.urlPrefix}'" + ]) searchArgs)) + '' > $out/options.json ''); - mkSearch = { modules, urlPrefix }: + mkSearch = { modules ? null, optionsJSON ? null, urlPrefix }: + let + args = { + inherit urlPrefix; + } // lib.optionalAttrs (modules != null) modules + // lib.optionalAttrs (optionsJSON != null) optionsJSON; + in runCommand "nuscht-search" { nativeBuildInputs = [ xorg.lndir ]; } '' mkdir $out lndir ${nuscht-search} $out - ln -s ${mkSearchJSON [ { inherit modules urlPrefix; } ]} $out/options.json + ln -s ${mkSearchJSON [ args ]} $out/options.json ''; - mkMultiSearch = listOfModuleAndUrlPrefix: + # mkMultiSearch [ + # { modules = [ self.inputs.nixos-modules.nixosModule ]; urlPrefix = "https://github.com/NuschtOS/nixos-modules/blob/main/"; } + # { optionsJSON = ./path/to/options.json; optionsPrefix = "programs.example"; urlPrefix = "https://git.example.com/blob/main/"; } + # ] + mkMultiSearch = searchArgs: runCommand "nuscht-search" { nativeBuildInputs = [ xorg.lndir ]; } '' mkdir $out lndir ${nuscht-search} $out - ln -s ${mkSearchJSON listOfModuleAndUrlPrefix}/options.json $out/options.json + ln -s ${mkSearchJSON searchArgs}/options.json $out/options.json ''; }