Skip to content

Commit

Permalink
Merge pull request #16 from NuschtOS/mkSearch-options-json
Browse files Browse the repository at this point in the history
Allow using options.json directly
  • Loading branch information
SuperSandro2000 authored Jun 29, 2024
2 parents ad491c5 + 05eed26 commit 9912ae1
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 9 deletions.
11 changes: 10 additions & 1 deletion nix/fixup-options.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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:
Expand Down
35 changes: 27 additions & 8 deletions nix/wrapper.nix
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{ lib, nixosOptionsDoc, nuscht-search, python3, runCommand, xorg }:
{ lib, nixosOptionsDoc, jq, nuscht-search, python3, runCommand, xorg }:

rec {
mkOptionsJSON = modules:
Expand All @@ -11,34 +11,53 @@ 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 ])) ]; }
(''
mkdir $out
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
'';
}

0 comments on commit 9912ae1

Please sign in to comment.