summary refs log tree commit diff
diff options
context:
space:
mode:
authorJonas Heinrich <onny@project-insanity.org>2022-09-10 14:51:56 +0200
committerYt <happysalada@proton.me>2022-11-08 09:59:10 -0500
commitfd76db7cb42ec0347a0d72e69734da4722405e1d (patch)
treec499877d875dfbd53692b36747919ba8674ca8e2
parent8f1cbf146f64d4167a2940dd25d5a70596a387f2 (diff)
downloadnixpkgs-fd76db7cb42ec0347a0d72e69734da4722405e1d.tar
nixpkgs-fd76db7cb42ec0347a0d72e69734da4722405e1d.tar.gz
nixpkgs-fd76db7cb42ec0347a0d72e69734da4722405e1d.tar.bz2
nixpkgs-fd76db7cb42ec0347a0d72e69734da4722405e1d.tar.lz
nixpkgs-fd76db7cb42ec0347a0d72e69734da4722405e1d.tar.xz
nixpkgs-fd76db7cb42ec0347a0d72e69734da4722405e1d.tar.zst
nixpkgs-fd76db7cb42ec0347a0d72e69734da4722405e1d.zip
nixos/invoiceplane: Add cron option
-rw-r--r--nixos/modules/services/web-apps/invoiceplane.nix57
1 files changed, 54 insertions, 3 deletions
diff --git a/nixos/modules/services/web-apps/invoiceplane.nix b/nixos/modules/services/web-apps/invoiceplane.nix
index c54915b10a2..8f0acbea4ee 100644
--- a/nixos/modules/services/web-apps/invoiceplane.nix
+++ b/nixos/modules/services/web-apps/invoiceplane.nix
@@ -184,6 +184,26 @@ let
           '';
         };
 
+        cron = {
+
+          enable = mkOption {
+            type = types.bool;
+            default = false;
+            description = lib.mdDoc ''
+              Enable cron service which periodically runs Invoiceplane tasks.
+              Requires key taken from the administration page. Refer to
+              <https://wiki.invoiceplane.com/en/1.0/modules/recurring-invoices>
+              on how to configure it.
+            '';
+          };
+
+          key = mkOption {
+            type = types.str;
+            description = lib.mdDoc "Cron key taken from the administration page.";
+          };
+
+        };
+
       };
 
     };
@@ -224,8 +244,11 @@ in
       }
       { assertion = cfg.database.createLocally -> cfg.database.passwordFile == null;
         message = ''services.invoiceplane.sites."${hostName}".database.passwordFile cannot be specified if services.invoiceplane.sites."${hostName}".database.createLocally is set to true.'';
-      }]
-    ) eachSite);
+      }
+      { assertion = cfg.cron.enable -> cfg.cron.key != null;
+        message = ''services.invoiceplane.sites."${hostName}".cron.key must be set in order to use cron service.'';
+      }
+    ]) eachSite);
 
     services.mysql = mkIf (any (v: v.database.createLocally) (attrValues eachSite)) {
       enable = true;
@@ -255,6 +278,7 @@ in
   }
 
   {
+
     systemd.tmpfiles.rules = flatten (mapAttrsToList (hostName: cfg: [
       "d ${cfg.stateDir} 0750 ${user} ${webserver.group} - -"
       "f ${cfg.stateDir}/ipconfig.php 0750 ${user} ${webserver.group} - -"
@@ -284,6 +308,34 @@ in
       group = webserver.group;
       isSystemUser = true;
     };
+
+  }
+  {
+
+    # Cron service implementation
+
+    systemd.timers = mapAttrs' (hostName: cfg: (
+      nameValuePair "invoiceplane-cron-${hostName}" (mkIf cfg.cron.enable {
+        wantedBy = [ "timers.target" ];
+        timerConfig = {
+          OnBootSec = "5m";
+          OnUnitActiveSec = "5m";
+          Unit = "invoiceplane-cron-${hostName}.service";
+        };
+      })
+    )) eachSite;
+
+    systemd.services =
+      (mapAttrs' (hostName: cfg: (
+        nameValuePair "invoiceplane-cron-${hostName}" (mkIf cfg.cron.enable {
+          serviceConfig = {
+            Type = "oneshot";
+            User = user;
+            ExecStart = "${pkgs.curl}/bin/curl --header 'Host: ${hostName}' http://localhost/index.php/invoices/cron/recur/${cfg.cron.key}";
+          };
+        })
+    )) eachSite);
+
   }
 
   (mkIf (cfg.webserver == "caddy") {
@@ -302,6 +354,5 @@ in
     };
   })
 
-
   ]);
 }