From abb2b8a5f90fb88fbf483c8188c1bd67d0c28225 Mon Sep 17 00:00:00 2001 From: h7x4 Date: Fri, 22 Aug 2025 18:43:45 +0200 Subject: [PATCH] module.nix: init --- flake.nix | 6 ++++ module.nix | 101 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 107 insertions(+) create mode 100644 module.nix diff --git a/flake.nix b/flake.nix index 056575b..5c233ac 100644 --- a/flake.nix +++ b/flake.nix @@ -25,6 +25,12 @@ }; }); + overlays.default = final: prev: { + inherit (self.packages.${final.system}) mclog2psql; + }; + + nixosModules.default = ./module.nix; + packages = forAllSystems (system: pkgs: { default = self.packages.${system}.mclog2psql; mclog2psql = pkgs.writers.writePython3Bin "mclog2psql" { diff --git a/module.nix b/module.nix new file mode 100644 index 0000000..19e6ecd --- /dev/null +++ b/module.nix @@ -0,0 +1,101 @@ +{ config, lib, pkgs, ... }: +let + cfg = config.services.minecraft-heatmap; +in +{ + options.services.minecraft-heatmap = { + enable = lib.mkEnableOption ""; + + mclog2psqlPackage = lib.mkPackageOption pkgs "mclog2psql" { }; + + onCalendar = lib.mkOption { + type = lib.types.str; + default = "05:00:00"; + description = "Systemd timer OnCalendar setting for the Minecraft Heatmap log ingester."; + }; + + minecraftLogsDir = lib.mkOption { + type = lib.types.path; + default = "/var/cache/minecraft-heatmap/logs"; + description = "Directory containing Minecraft server log files to be ingested."; + }; + + database = { + host = lib.mkOption { + type = lib.types.str; + default = "localhost"; + description = "Database host for Minecraft Heatmap."; + }; + + port = lib.mkOption { + type = lib.types.port; + default = 5432; + description = "Database port for Minecraft Heatmap."; + }; + + name = lib.mkOption { + type = lib.types.str; + default = "minecraft_heatmap"; + description = "Database name for Minecraft Heatmap."; + }; + + user = lib.mkOption { + type = lib.types.str; + default = "minecraft_heatmap"; + description = "Database user for Minecraft Heatmap."; + }; + + passwordFile = lib.mkOption { + type = with lib.types; nullOr path; + default = null; + description = "Path to the file containing the database password for Minecraft Heatmap."; + }; + }; + }; + + config = lib.mkIf cfg.enable { + systemd.services.minecraft-heatmap-ingest-logs = { + description = "Minecraft Heatmap log ingester service"; + environment = { + MINECRAFT_HEATMAP_DIR = cfg.minecraftLogsDir; + MINECRAFT_HEATMAP_DB_HOST = cfg.database.host; + MINECRAFT_HEATMAP_DB_PORT = toString cfg.database.port; + MINECRAFT_HEATMAP_DB_NAME = cfg.database.name; + MINECRAFT_HEATMAP_DB_USER = cfg.database.user; + MINECRAFT_HEATMAP_DB_PASSWORD_FILE = lib.optionalString (cfg.database.passwordFile != null) "%d/minecraft-heatmap-db-password"; + }; + + serviceConfig = { + Type = "oneshot"; + ExecStart = "${cfg.mclog2psqlPackage}/bin/mclog2psql"; + Restart = "on-failure"; + RestartSec = "5s"; + + User = "minecraft-heatmap"; + Group = "minecraft-heatmap"; + DynamicUser = true; + + LoadCredential = lib.mkIf (cfg.database.passwordFile != null) [ + "minecraft-heatmap-db-password:${cfg.database.passwordFile}" + ]; + + CacheDirectory = "minecraft-heatmap"; + CacheDirectoryMode = "0700"; + }; + }; + + systemd.timers.minecraft-heatmap-ingest-logs = { + description = "Minecraft Heatmap log ingester timer"; + wantedBy = [ "timers.target" ]; + timerConfig = { + OnCalendar = cfg.onCalendar; + Persistent = true; + RandomizedDelaySec = "1h"; + }; + }; + + # systemd.sockets.minecraft-heatmap-rest-endpoint = {}; + + # systemd.sockets.minecraft-heatmap-rest-endpoint = {}; + }; +}