From 1bba38be77f14028bdca6b2579c0db99c2723cde Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kier=C3=A1n=20Meinhardt?= Date: Fri, 19 Dec 2025 15:05:29 +0100 Subject: [PATCH] tarot: replace htgen by flask --- flake.nix | 4 -- modules/htgen.nix | 110 -------------------------------------- packages/htgen.nix | 26 --------- systems/makanek/tarot.nix | 71 ++++++++++++++---------- 4 files changed, 42 insertions(+), 169 deletions(-) delete mode 100644 modules/htgen.nix delete mode 100644 packages/htgen.nix diff --git a/flake.nix b/flake.nix index 42f2222..fc8fe29 100644 --- a/flake.nix +++ b/flake.nix @@ -130,7 +130,6 @@ # TODO remove flake-utils dependency from my own repos nixosModules = { - htgen = import modules/htgen.nix; moodle-dl = import modules/moodle-dl.nix; networkmanager-declarative = import modules/networkmanager-declarative.nix; passport = import modules/passport.nix; @@ -170,7 +169,6 @@ inputs.self.nixosModules.passport inputs.self.nixosModules.panoptikon inputs.self.nixosModules.go-webring - inputs.self.nixosModules.htgen inputs.stockholm.nixosModules.reaktor2 retiolum.nixosModules.retiolum nur.modules.nixos.default @@ -212,7 +210,6 @@ modules = [ systems/makanek/configuration.nix inputs.self.nixosModules.telegram-bot - inputs.self.nixosModules.htgen inputs.self.nixosModules.passport agenix.nixosModules.default retiolum.nixosModules.retiolum @@ -319,7 +316,6 @@ # krebs brainmelter = pkgs.callPackage packages/brainmelter.nix {}; cyberlocker-tools = pkgs.callPackage packages/cyberlocker-tools.nix {}; - htgen = pkgs.callPackage packages/htgen.nix {}; hc = pkgs.callPackage packages/hc.nix {}; kpaste = pkgs.callPackage packages/kpaste.nix {}; pls = pkgs.callPackage packages/pls.nix {}; diff --git a/modules/htgen.nix b/modules/htgen.nix deleted file mode 100644 index 9cdd0bf..0000000 --- a/modules/htgen.nix +++ /dev/null @@ -1,110 +0,0 @@ -{ - config, - lib, - pkgs, - ... -}: - -let - optionalAttr = name: value: if name != null then { ${name} = value; } else { }; - - cfg = config.services.htgen; - - out = { - options.services.htgen = api; - config = imp; - }; - - htgen = pkgs.callPackage ../packages/htgen.nix { }; - - api = - with lib; - mkOption { - default = { }; - type = types.attrsOf ( - types.submodule ( - { config, ... }: - { - options = { - enable = mkEnableOption "services.htgen-${config._module.args.name}"; - - name = mkOption { - type = types.str; - default = config._module.args.name; - }; - - package = mkOption { - default = htgen; - type = types.package; - }; - - port = mkOption { - type = types.port; - }; - - script = mkOption { - type = types.nullOr types.str; - default = null; - }; - - scriptFile = mkOption { - type = types.nullOr (types.either types.package types.pathname); - default = null; - }; - - user = mkOption { - default = { - name = "htgen-${config.name}"; - home = "/var/lib/htgen-${config.name}"; - }; - defaultText = { - name = "htgen-‹name›"; - home = "/var/lib/htgen-‹name›"; - }; - }; - }; - } - ) - ); - }; - imp = { - - systemd.services = lib.mapAttrs' ( - name: htgen: - lib.nameValuePair "htgen-${name}" { - wantedBy = [ "multi-user.target" ]; - after = [ "network.target" ]; - environment = { - HTGEN_PORT = toString htgen.port; - } - // optionalAttr "HTGEN_SCRIPT" htgen.script - // optionalAttr "HTGEN_SCRIPT_FILE" htgen.scriptFile; - serviceConfig = { - SyslogIdentifier = "htgen"; - User = htgen.user.name; - PrivateTmp = true; - Restart = "always"; - ExecStart = "${htgen.package}/bin/htgen --serve"; - }; - } - ) cfg; - - users.users = lib.mapAttrs' ( - name: htgen: - lib.nameValuePair htgen.user.name { - inherit (htgen.user) home name; - group = htgen.user.name; - createHome = true; - isSystemUser = true; - } - ) cfg; - - users.groups = lib.mapAttrs' ( - name: htgen: - lib.nameValuePair htgen.user.name { - name = htgen.user.name; - } - ) cfg; - }; -in -out diff --git a/packages/htgen.nix b/packages/htgen.nix deleted file mode 100644 index 1ee1378..0000000 --- a/packages/htgen.nix +++ /dev/null @@ -1,26 +0,0 @@ -{ fetchgit, lib, pkgs, stdenv }: -stdenv.mkDerivation rec { - pname = "htgen"; - version = "1.4.0"; - - src = fetchgit { - url = "https://cgit.krebsco.de/htgen"; - rev = "refs/tags/${version}"; - sha256 = "1k6xdr4g1p2wjiyizwh33ihw3azbar7kmhyxywcq0whpip9inpmj"; - }; - - installPhase = '' - mkdir -p $out/bin - { - echo '#! ${pkgs.dash}/bin/dash' - echo 'export PATH=${lib.makeBinPath [ - pkgs.coreutils - pkgs.jq - pkgs.ucspi-tcp - ]}''${PATH+":$PATH"}' - sed 's:^Server=htgen$:&/${version}:' htgen - } > $out/bin/htgen - chmod +x $out/bin/htgen - cp -r examples $out - ''; -} diff --git a/systems/makanek/tarot.nix b/systems/makanek/tarot.nix index 849a8c2..65a5bb6 100644 --- a/systems/makanek/tarot.nix +++ b/systems/makanek/tarot.nix @@ -3,7 +3,8 @@ pkgs, lib, ... -}: let +}: +let tarotPort = 7407; tarotFiles = pkgs.fetchzip { url = "https://c.krebsco.de/tarot.zip"; @@ -14,33 +15,45 @@ url = "http://c.krebsco.de/tarot.pdf"; sha256 = "1n2m53kjg2vj9dbr70b9jrsbqwdfrcb48l4wswn21549fi24g6dx"; }; -in { - services.htgen.tarot = { - port = tarotPort; - script = pkgs.writers.writeDash "tarot" '' - case "$Method $Request_URI" in - "GET /") - if item=$(${pkgs.findutils}/bin/find ${toString tarotFiles} -type f | ${pkgs.coreutils}/bin/shuf -n1); then - card=$(mktemp --tmpdir tarot.XXX) - trap 'rm $card' EXIT - reverse=$(${pkgs.coreutils}/bin/shuf -i0-1 -n1) - if [ "$reverse" -eq 1 ]; then - ${pkgs.imagemagick}/bin/convert -rotate 180 "$item" "$card" - else - ${pkgs.coreutils}/bin/cp "$item" "$card" - fi - printf 'HTTP/1.1 200 OK\r\n' - printf 'Content-Type: %s\r\n' "$(${pkgs.file}/bin/file -ib "$card")" - printf 'Server: %s\r\n' "$Server" - printf 'Connection: close\r\n' - printf 'Content-Length: %d\r\n' $(${pkgs.coreutils}/bin/wc -c < "$card") - printf '\r\n' - cat "$card" - exit - fi - ;; - esac - ''; +in +{ + systemd.services.tarot = { + enable = true; + serviceConfig.Type = "simple"; + wantedBy = [ "multi-user.target" ]; + serviceConfig.ExecStart = pkgs.writers.writePython3 "tarot-server" { + libraries = py: [ py.pillow py.flask ]; + } '' + from flask import Flask, send_file + from pathlib import Path + from random import choice, randint + from io import BytesIO + from PIL import Image + + app = Flask(__name__) + TAROT_DIR = Path("${tarotFiles}") + + + @app.route("/") + def tarot(): + card_path = choice(list(TAROT_DIR.glob("*"))) + + with Image.open(card_path) as img: + if randint(0, 1): + img = img.rotate(180) + buf = BytesIO() + img.save(buf, format="JPEG") + buf.seek(0) + return send_file( + buf, + mimetype='image/jpeg', + as_attachment=False + ) + + + if __name__ == "__main__": + app.run(port=${toString tarotPort}) + ''; }; niveum.passport.services = [ @@ -55,7 +68,7 @@ in { enableACME = true; forceSSL = true; locations = { - "/".proxyPass = "http://127.0.0.1:${toString tarotPort}"; + "/".proxyPass = "http://127.0.0.1:${toString tarotPort}/"; "/files/" = { root = pkgs.linkFarm "tarot" [ {