nixos/invoiceplane: Add cron option

authored by Jonas Heinrich and committed by Yt fd76db7c 8f1cbf14

+54 -3
+54 -3
nixos/modules/services/web-apps/invoiceplane.nix
··· 184 ''; 185 }; 186 187 }; 188 189 }; ··· 224 } 225 { assertion = cfg.database.createLocally -> cfg.database.passwordFile == null; 226 message = ''services.invoiceplane.sites."${hostName}".database.passwordFile cannot be specified if services.invoiceplane.sites."${hostName}".database.createLocally is set to true.''; 227 - }] 228 - ) eachSite); 229 230 services.mysql = mkIf (any (v: v.database.createLocally) (attrValues eachSite)) { 231 enable = true; ··· 255 } 256 257 { 258 systemd.tmpfiles.rules = flatten (mapAttrsToList (hostName: cfg: [ 259 "d ${cfg.stateDir} 0750 ${user} ${webserver.group} - -" 260 "f ${cfg.stateDir}/ipconfig.php 0750 ${user} ${webserver.group} - -" ··· 284 group = webserver.group; 285 isSystemUser = true; 286 }; 287 } 288 289 (mkIf (cfg.webserver == "caddy") { ··· 301 )) eachSite; 302 }; 303 }) 304 - 305 306 ]); 307 }
··· 184 ''; 185 }; 186 187 + cron = { 188 + 189 + enable = mkOption { 190 + type = types.bool; 191 + default = false; 192 + description = lib.mdDoc '' 193 + Enable cron service which periodically runs Invoiceplane tasks. 194 + Requires key taken from the administration page. Refer to 195 + <https://wiki.invoiceplane.com/en/1.0/modules/recurring-invoices> 196 + on how to configure it. 197 + ''; 198 + }; 199 + 200 + key = mkOption { 201 + type = types.str; 202 + description = lib.mdDoc "Cron key taken from the administration page."; 203 + }; 204 + 205 + }; 206 + 207 }; 208 209 }; ··· 244 } 245 { assertion = cfg.database.createLocally -> cfg.database.passwordFile == null; 246 message = ''services.invoiceplane.sites."${hostName}".database.passwordFile cannot be specified if services.invoiceplane.sites."${hostName}".database.createLocally is set to true.''; 247 + } 248 + { assertion = cfg.cron.enable -> cfg.cron.key != null; 249 + message = ''services.invoiceplane.sites."${hostName}".cron.key must be set in order to use cron service.''; 250 + } 251 + ]) eachSite); 252 253 services.mysql = mkIf (any (v: v.database.createLocally) (attrValues eachSite)) { 254 enable = true; ··· 278 } 279 280 { 281 + 282 systemd.tmpfiles.rules = flatten (mapAttrsToList (hostName: cfg: [ 283 "d ${cfg.stateDir} 0750 ${user} ${webserver.group} - -" 284 "f ${cfg.stateDir}/ipconfig.php 0750 ${user} ${webserver.group} - -" ··· 308 group = webserver.group; 309 isSystemUser = true; 310 }; 311 + 312 + } 313 + { 314 + 315 + # Cron service implementation 316 + 317 + systemd.timers = mapAttrs' (hostName: cfg: ( 318 + nameValuePair "invoiceplane-cron-${hostName}" (mkIf cfg.cron.enable { 319 + wantedBy = [ "timers.target" ]; 320 + timerConfig = { 321 + OnBootSec = "5m"; 322 + OnUnitActiveSec = "5m"; 323 + Unit = "invoiceplane-cron-${hostName}.service"; 324 + }; 325 + }) 326 + )) eachSite; 327 + 328 + systemd.services = 329 + (mapAttrs' (hostName: cfg: ( 330 + nameValuePair "invoiceplane-cron-${hostName}" (mkIf cfg.cron.enable { 331 + serviceConfig = { 332 + Type = "oneshot"; 333 + User = user; 334 + ExecStart = "${pkgs.curl}/bin/curl --header 'Host: ${hostName}' http://localhost/index.php/invoices/cron/recur/${cfg.cron.key}"; 335 + }; 336 + }) 337 + )) eachSite); 338 + 339 } 340 341 (mkIf (cfg.webserver == "caddy") { ··· 353 )) eachSite; 354 }; 355 }) 356 357 ]); 358 }