From 8dbd8f9ec2276145a8954ff15d82d0d2c944b7ab Mon Sep 17 00:00:00 2001
From: matt1432 <matt@nelim.org>
Date: Sat, 8 Feb 2025 01:31:20 -0500
Subject: [PATCH] feat(nos): setup jellystat

---
 .../cluster/modules/caddy/default.nix         |  1 +
 configurations/nos/modules/docker/default.nix |  1 +
 .../docker/media/jellystat/compose.nix        | 49 +++++++++++++++++++
 .../media/jellystat/images/jellystat.nix      |  8 +++
 .../media/jellystat/images/postgres.nix       |  8 +++
 .../nos/modules/homepage/default.nix          |  7 +++
 flake.lock                                    |  8 +--
 7 files changed, 78 insertions(+), 4 deletions(-)
 create mode 100644 configurations/nos/modules/docker/media/jellystat/compose.nix
 create mode 100644 configurations/nos/modules/docker/media/jellystat/images/jellystat.nix
 create mode 100644 configurations/nos/modules/docker/media/jellystat/images/postgres.nix

diff --git a/configurations/cluster/modules/caddy/default.nix b/configurations/cluster/modules/caddy/default.nix
index 19604be1..de6047ce 100644
--- a/configurations/cluster/modules/caddy/default.nix
+++ b/configurations/cluster/modules/caddy/default.nix
@@ -139,6 +139,7 @@ in {
 
             subDirectories = {
               bazarr.reverseProxy = "${nosIP}:6767";
+              jellystat.reverseProxy = "${nosIP}:3070";
               prowlarr.reverseProxy = "${nosIP}:9696";
               radarr.reverseProxy = "${nosIP}:7878";
               sabnzbd.reverseProxy = "${nosIP}:8382";
diff --git a/configurations/nos/modules/docker/default.nix b/configurations/nos/modules/docker/default.nix
index 451f1e3e..dbf861fa 100644
--- a/configurations/nos/modules/docker/default.nix
+++ b/configurations/nos/modules/docker/default.nix
@@ -15,6 +15,7 @@ in {
 
     (import ./media/bazarr/compose.nix configPath)
     (import ./media/joal/compose.nix configPath)
+    (import ./media/jellystat/compose.nix configPath)
     (import ./media/prowlarr/compose.nix configPath)
     (import ./media/radarr/compose.nix configPath)
     (import ./media/sabnzbd/compose.nix configPath)
diff --git a/configurations/nos/modules/docker/media/jellystat/compose.nix b/configurations/nos/modules/docker/media/jellystat/compose.nix
new file mode 100644
index 00000000..0f303467
--- /dev/null
+++ b/configurations/nos/modules/docker/media/jellystat/compose.nix
@@ -0,0 +1,49 @@
+rwDataDir: {
+  config,
+  pkgs,
+  ...
+}: let
+  inherit (config.sops) secrets;
+
+  rwPath = rwDataDir + "/jellystat";
+in {
+  virtualisation.docker.compose."jellystat" = {
+    networks.proxy_net = {external = true;};
+
+    services = {
+      "jellystat" = {
+        image = pkgs.callPackage ./images/jellystat.nix pkgs;
+        restart = "always";
+
+        env_file = [secrets.jellystat.path];
+        environment = {
+          JS_BASE_URL = "jellystat";
+
+          POSTGRES_IP = "jellystat-db";
+          POSTGRES_PORT = 5432;
+          TZ = "America/New_York";
+        };
+
+        ports = ["3070:3000"];
+        networks = ["proxy_net"];
+
+        volumes = ["${rwPath}/data:/app/backend/backup-data"];
+
+        depends_on = ["jellystat-db"];
+      };
+
+      "jellystat-db" = {
+        image = pkgs.callPackage ./images/postgres.nix pkgs;
+        restart = "always";
+
+        env_file = [secrets.jellystat.path];
+        networks = ["proxy_net"];
+
+        volumes = ["${rwPath}/db:/var/lib/postgresql/data"];
+      };
+    };
+  };
+
+  # For accurate stack trace
+  _file = ./compose.nix;
+}
diff --git a/configurations/nos/modules/docker/media/jellystat/images/jellystat.nix b/configurations/nos/modules/docker/media/jellystat/images/jellystat.nix
new file mode 100644
index 00000000..048e0d7c
--- /dev/null
+++ b/configurations/nos/modules/docker/media/jellystat/images/jellystat.nix
@@ -0,0 +1,8 @@
+pkgs:
+pkgs.dockerTools.pullImage rec {
+  imageName = "cyfershepard/jellystat";
+  imageDigest = "sha256:cc634936b69260548715953c0a4fcfb2dde6f6daa8eed3a6d08d0dcf0a72b9ed";
+  hash = "sha256-UZq435kIxakrwaStbe7LbQRwB0XRSzxYw9CVnctrku0=";
+  finalImageName = imageName;
+  finalImageTag = "latest";
+}
diff --git a/configurations/nos/modules/docker/media/jellystat/images/postgres.nix b/configurations/nos/modules/docker/media/jellystat/images/postgres.nix
new file mode 100644
index 00000000..08621794
--- /dev/null
+++ b/configurations/nos/modules/docker/media/jellystat/images/postgres.nix
@@ -0,0 +1,8 @@
+pkgs:
+pkgs.dockerTools.pullImage rec {
+  imageName = "postgres";
+  imageDigest = "sha256:78a275d4c891f7b3a33d3f1a78eda9f1d744954d9e20122bfdc97cdda25cddaf";
+  hash = "sha256-0cShBpvsoDyE5NmkPX5svlYTYVWVy0rrjwKLn0qYovY=";
+  finalImageName = imageName;
+  finalImageTag = "15.2";
+}
diff --git a/configurations/nos/modules/homepage/default.nix b/configurations/nos/modules/homepage/default.nix
index 0abde8b8..8f3b5dbb 100644
--- a/configurations/nos/modules/homepage/default.nix
+++ b/configurations/nos/modules/homepage/default.nix
@@ -67,6 +67,13 @@
               description = "manage jellyfin users";
             };
           }
+          {
+            jellystat = {
+              href = "https://lan.nelim.org/jellystat";
+              icon = "jellyfin.png";
+              description = "view jellyfin stats";
+            };
+          }
           {
             jellyseerr = rec {
               href = "https://seerr.nelim.org";
diff --git a/flake.lock b/flake.lock
index 41eafe21..9cd82d1d 100644
--- a/flake.lock
+++ b/flake.lock
@@ -1790,11 +1790,11 @@
         ]
       },
       "locked": {
-        "lastModified": 1737762708,
-        "narHash": "sha256-S5NgFKNsx17zVcVcdUl8UvxdtFed+3/xfULkAmQOi6w=",
+        "lastModified": 1738994490,
+        "narHash": "sha256-Y5sEdnFopkJ2MfJr+irL6JLdXrLK2utEal9dXsf+0P4=",
         "ref": "refs/heads/main",
-        "rev": "279291d37b542b88fcf5cea090d81759e9b247b7",
-        "revCount": 88,
+        "rev": "4777580bde7e86968083a2125d3946120aca067f",
+        "revCount": 91,
         "type": "git",
         "url": "ssh://git@git.nelim.org/matt1432/nixos-secrets"
       },