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 184 ''; 185 185 }; 186 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 + 187 207 }; 188 208 189 209 }; ··· 224 244 } 225 245 { assertion = cfg.database.createLocally -> cfg.database.passwordFile == null; 226 246 message = ''services.invoiceplane.sites."${hostName}".database.passwordFile cannot be specified if services.invoiceplane.sites."${hostName}".database.createLocally is set to true.''; 227 - }] 228 - ) eachSite); 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); 229 252 230 253 services.mysql = mkIf (any (v: v.database.createLocally) (attrValues eachSite)) { 231 254 enable = true; ··· 255 278 } 256 279 257 280 { 281 + 258 282 systemd.tmpfiles.rules = flatten (mapAttrsToList (hostName: cfg: [ 259 283 "d ${cfg.stateDir} 0750 ${user} ${webserver.group} - -" 260 284 "f ${cfg.stateDir}/ipconfig.php 0750 ${user} ${webserver.group} - -" ··· 284 308 group = webserver.group; 285 309 isSystemUser = true; 286 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 + 287 339 } 288 340 289 341 (mkIf (cfg.webserver == "caddy") { ··· 301 353 )) eachSite; 302 354 }; 303 355 }) 304 - 305 356 306 357 ]); 307 358 }