From 835a05dd6411bae603dcc0406c0f94e43ed6c007 Mon Sep 17 00:00:00 2001
From: Julien Malka <julien.malka@me.com>
Date: Tue, 21 Dec 2021 00:44:23 +0100
Subject: [PATCH] Created Hydra module

---
 machines/newton/default.nix | 24 ++++---------------
 modules/hydra/default.nix   | 47 +++++++++++++++++++++++++++++++++++++
 2 files changed, 52 insertions(+), 19 deletions(-)
 create mode 100644 modules/hydra/default.nix

diff --git a/machines/newton/default.nix b/machines/newton/default.nix
index ea3e4f2..9d7de91 100644
--- a/machines/newton/default.nix
+++ b/machines/newton/default.nix
@@ -14,25 +14,11 @@ in
   luj = {
     filerun.enable = true;
     zfs-mails.enable = true;
-  };
-
-
-  services.hydra = {
-    enable = true;
-    hydraURL = "https://hydra.julienmalka.me";
-    notificationSender = "hydra@localhost";
-    port = 9876;
-    buildMachinesFiles = [ ];
-    useSubstitutes = true;
-  };
-
-  services.nginx = {
-    enable = true;
-    virtualHosts = {
-      "hydra.julienmalka.me" = {
-        forceSSL = true;
-        enableACME = true;
-        locations."/" = { proxyPass = "http://127.0.0.1:9876"; };
+    hydra = {
+      enable = true;
+      nginx = {
+        enable = true;
+        subdomain = "hydra";
       };
     };
   };
diff --git a/modules/hydra/default.nix b/modules/hydra/default.nix
new file mode 100644
index 0000000..807fe96
--- /dev/null
+++ b/modules/hydra/default.nix
@@ -0,0 +1,47 @@
+{ lib, pkgs, config, ... }:
+with lib;
+let
+  cfg = config.luj.hydra;
+  port = 9876;
+in
+{
+
+  options.luj.hydra = {
+    enable = mkEnableOption "activate hydra service";
+    nginx.enable = mkEnableOption "activate nginx";
+    nginx.subdomain = mkOption {
+      type = types.str;
+    };
+  };
+
+  config = mkIf cfg.enable (
+    mkMerge [{
+
+      services.hydra = {
+        enable = true;
+        notificationSender = "hydra@localhost";
+        port = port;
+        buildMachinesFiles = [ ];
+        useSubstitutes = true;
+      };
+
+      networking.firewall = { allowedTCPPorts = [ port ]; };
+    }
+
+      (mkIf cfg.nginx.enable {
+        luj.nginx.enable = true;
+        services.hydra.hydraURL = "${cfg.nginx.subdomain}.julienmalka.me";
+        services.nginx.virtualHosts."${cfg.nginx.subdomain}.julienmalka.me" = {
+          enableACME = true;
+          forceSSL = true;
+          locations."/" = {
+            proxyPass = "http://localhost:${toString port}";
+          };
+        };
+
+      })]);
+
+
+
+
+}