From 53433866bbb71c9c34cbdb45915d2eb0123a08bd Mon Sep 17 00:00:00 2001 From: Peder Bergebakken Sundt Date: Thu, 29 Jun 2023 05:55:36 +0200 Subject: [PATCH] Initial commit --- flake.lock | 74 ++++++++++++++++++++++++++++++++++++++++++++++++ flake.nix | 35 +++++++++++++++++++++++ j2_environ.py | 13 +++++++++ lib.nix | 33 +++++++++++++++++++++ template.html.j2 | 52 ++++++++++++++++++++++++++++++++++ 5 files changed, 207 insertions(+) create mode 100644 flake.lock create mode 100644 flake.nix create mode 100644 j2_environ.py create mode 100644 lib.nix create mode 100644 template.html.j2 diff --git a/flake.lock b/flake.lock new file mode 100644 index 0000000..6043e32 --- /dev/null +++ b/flake.lock @@ -0,0 +1,74 @@ +{ + "nodes": { + "example-nix-minecraft": { + "inputs": { + "mcversions": "mcversions", + "nixpkgs": "nixpkgs" + }, + "locked": { + "lastModified": 1655551827, + "narHash": "sha256-sUdTici3FjWTMvfKtW+h9R6LFvdWrlWub/SI5pAdkAI=", + "owner": "12Boti", + "repo": "nix-minecraft", + "rev": "a2c657b54d86bc1c912ce99597253cfdb4042010", + "type": "github" + }, + "original": { + "owner": "12Boti", + "repo": "nix-minecraft", + "type": "github" + } + }, + "mcversions": { + "flake": false, + "locked": { + "lastModified": 1648176147, + "narHash": "sha256-9xqPSKlSDZZx3z683ZoMhR6jyRd1eS6L+mIdjnqu6Cw=", + "owner": "yushijinhun", + "repo": "minecraft-version-json-history", + "rev": "a8487697562bf897d0688f4edb9f9fc4b2b4bbfb", + "type": "github" + }, + "original": { + "owner": "yushijinhun", + "repo": "minecraft-version-json-history", + "type": "github" + } + }, + "nixpkgs": { + "locked": { + "lastModified": 1648097358, + "narHash": "sha256-GMoTKP/po2Nbkh1tvPvP8Ww6NyFW8FFst1Z3nfzffZc=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "4d60081494259c0785f7e228518fee74e0792c1b", + "type": "github" + }, + "original": { + "id": "nixpkgs", + "type": "indirect" + } + }, + "nixpkgs_2": { + "locked": { + "lastModified": 1687829761, + "narHash": "sha256-QRe1Y8SS3M4GeC58F/6ajz6V0ZLUVWX3ZAMgov2N3/g=", + "path": "/nix/store/mz6lqpbsp4h36kgahvwfrgdjzz042hpn-source", + "rev": "9790f3242da2152d5aa1976e3e4b8b414f4dd206", + "type": "path" + }, + "original": { + "id": "nixpkgs", + "type": "indirect" + } + }, + "root": { + "inputs": { + "example-nix-minecraft": "example-nix-minecraft", + "nixpkgs": "nixpkgs_2" + } + } + }, + "root": "root", + "version": 7 +} diff --git a/flake.nix b/flake.nix new file mode 100644 index 0000000..f2b861a --- /dev/null +++ b/flake.nix @@ -0,0 +1,35 @@ +{ + description = "Generate documentation for NixOS modules."; + + inputs.example-nix-minecraft.url = "github:12Boti/nix-minecraft"; + + outputs = { + self, + nixpkgs, + ... } @ inputs: + let + forAllSystems = f: nixpkgs.lib.genAttrs nixpkgs.lib.systems.flakeExposed (system: f { + inherit system; + pkgs = nixpkgs.legacyPackages.${system}; + lib = nixpkgs.legacyPackages.${system}.lib; + doc = self.lib.${system}; + }); + in { + inherit inputs; + + lib = forAllSystems ({ pkgs, ...}: pkgs.callPackage ./lib.nix { }); + + packages = forAllSystems ({ pkgs, lib, doc, ...}: let + mkJSON = name: modules: lib.pipe modules [ + doc.modules2options + builtins.toJSON + (pkgs.writeText "${name}.json") + ]; + in { + example-nix-minecraft-json = mkJSON "nix-minecraft" inputs.example-nix-minecraft.lib.baseModules; + example-nix-minecraft-html = doc.mkHTML "nix-minecraft" inputs.example-nix-minecraft inputs.example-nix-minecraft.lib.baseModules; + #example-nix-minecraft = ; + }); + + }; +} diff --git a/j2_environ.py b/j2_environ.py new file mode 100644 index 0000000..ab5ea01 --- /dev/null +++ b/j2_environ.py @@ -0,0 +1,13 @@ +# https://github.com/kolypto/j2cli#customization + +# http://jinja.pocoo.org/docs/2.10/api/#jinja2.Environment +def j2_environment_params(): return dict( + autoescape = True, + trim_blocks = True, + lstrip_blocks = True, + keep_trailing_newline = True, + extensions = ( + "jinja2.ext.do", + "jinja2.ext.loopcontrols" + ), +) diff --git a/lib.nix b/lib.nix new file mode 100644 index 0000000..bd875e9 --- /dev/null +++ b/lib.nix @@ -0,0 +1,33 @@ +{ lib, pkgs }: + +rec { + + modules2options = modules': let + eval = lib.evalModules { + modules = [{ _module.check = false; }] ++ modules'; + }; + options = lib.optionAttrSetToDocList eval.options; + in builtins.filter (x: x.visible && !x.internal) options; + + mkHTML = name: source: modules: let + options = modules2options modules; + declarations = lib.pipe options [ + (builtins.map (option: option.declarations)) + lib.flatten + ]; + in pkgs.runCommand "${name}-html" { + nativeBuildInputs = with pkgs; [ aha bat j2cli ]; + passAsFile = [ "jsonData" ]; + jsonData = builtins.toJSON { inherit name options source; }; + } ('' + mkdir $out + j2 --customize ${./j2_environ.py} -f json ${./template.html.j2} $jsonDataPath > $out/index.html + # ${source} # just here to make declarations available + '' + lib.concatStrings (lib.forEach declarations (declaration: with lib; '' + BASE="$(basename ${lib.escapeShellArg declaration})" + DIR="$(dirname ${lib.escapeShellArg declaration} | cut -d/ -f5-)" + mkdir -p $out/sources/"$DIR" + bat --color always --style plain ${escapeShellArg declaration} | aha --black --title "$BASE" > $out/sources/"$DIR"/"$BASE".html + ''))); + +} diff --git a/template.html.j2 b/template.html.j2 new file mode 100644 index 0000000..e6af467 --- /dev/null +++ b/template.html.j2 @@ -0,0 +1,52 @@ + + + +{{ name }} + + + + +{% macro show_value(val) %} + {% if val._type == "literalExpression" %} +
{{ val.text }}
+ {% else %} +
{{ val.text }}
+ {% endif %} +{% endmacro %} + + +{% for option in options %} +
+ + {{ option.name }} + + +
Desc:
+
{{ option.description | safe }}
+ +
Type:
+
{{ option.type }}
+ +{% if option.default is defined %} +
Type:
+
{{ show_value(option.default) }}
+{% endif %} + +{% if option.example is defined %} +
example:
+
{{ show_value(option.example) }}
+{% endif %} + +{% for declaration in option.declarations %} +
Source:
+
+ + {{ declaration.replace(source, "<%s>" % name) }} + +
+{% endfor %} + +
+{% endfor %}