Something went wrong. Try again.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187{ config, lib, pkgs, ...}:
with lib;
let cfg = config.services.persista;in{ options.services.persista = { enable = mkEnableOption "Persista supporter API";
package = mkOption { type = types.package; description = "The Persista package to use."; };
port = mkOption { type = types.port; default = 8080; description = "Port Persista listens on."; };
database = { enable = mkEnableOption "Enables the PostgreSQL service"; name = mkOption { type = types.str; default = "persista"; description = "PostgreSQL database name."; }; user = mkOption { type = types.str; default = "persista"; description = "PostgreSQL user."; }; };
jwtSecretFile = mkOption { type = types.path; description = "File containing the JWT secret."; };
dbPasswordFile = mkOption { type = types.path; description = "File containing the DB password."; };
adminSecretFile = mkOption { type = types.path; description = "File containing the admin password."; };
openFirewall = mkOption { type = types.bool; default = false; description = "Whether to open the firewall for the Persista port."; };
nginx = { enable = mkOption { type = types.bool; default = false; description = "Whether to enable nginx reverse proxy."; };
domain = mkOption { type = types.str; default = ""; description = "Domain name for the nginx virtual host."; example = "persista.example.com"; };
enableACME = mkOption { type = types.bool; default = true; description = "Whether to enable ACME/Let's Encrypt SSL certificates."; };
acmeEmail = mkOption { type = types.str; default = ""; description = "Email address for ACME registration."; example = "admin@example.com"; };
forceSSL = mkOption { type = types.bool; default = true; description = "Whether to force SSL."; }; }; };
config = mkIf cfg.enable { assertions = [ { assertion = !cfg.nginx.enable || cfg.nginx.domain != ""; message = "services.persista.nginx.domain must be set when nginx is enabled"; } { assertion = !cfg.nginx.enable || !cfg.nginx.enableACME || cfg.nginx.acmeEmail != ""; message = "services.persista.nginx.acmeEmail must be set when ACME is enabled"; } ];
services.postgresql = mkIf cfg.database.enable { enable = true; ensureDatabases = [ cfg.database.name ]; ensureUsers = [ { name = cfg.database.user; ensureDBOwnership = true; } ]; };
networking.firewall.allowedTCPPorts = mkMerge [ (mkIf cfg.openFirewall [ cfg.port ]) (mkIf cfg.nginx.enable [ 80 443 ]) ];
systemd.services.persista = { description = "Persista supporter API"; after = [ "network.target" "postgresql.service" ]; requires = [ "postgresql.service" ]; wantedBy = [ "multi-user.target" ];
serviceConfig = { Restart = "on-failure"; RestartSec = "10s"; DynamicUser = true; NoNewPrivileges = true; PrivateTmp = true; ProtectSystem = "strict"; ProtectHome = true; LoadCredential = [ "jwt_secret:${cfg.jwtSecretFile}" "db_password:${cfg.dbPasswordFile}" "admin_secret:${cfg.adminSecretFile}" ]; };
script = '' export PORT=${toString cfg.port} export DATABASE_URL="jdbc:postgresql://localhost/${cfg.database.name}" export DB_USER="${cfg.database.user}" export DB_PASSWORD="$(cat $CREDENTIALS_DIRECTORY/db_password)" export JWT_SECRET="$(cat $CREDENTIALS_DIRECTORY/jwt_secret)" export ADMIN_SECRET="$(cat $CREDENTIALS_DIRECTORY/admin_secret)" exec ${lib.getExe cfg.package} ''; };
services.nginx = mkIf cfg.nginx.enable { enable = true; virtualHosts.${cfg.nginx.domain} = { enableACME = cfg.nginx.enableACME; forceSSL = cfg.nginx.forceSSL; locations."/" = { proxyPass = "http://127.0.0.1:${toString cfg.port}"; extraConfig = '' proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; ''; }; }; };
security.acme = mkIf (cfg.nginx.enable && cfg.nginx.enableACME) { acceptTerms = true; defaults.email = cfg.nginx.acmeEmail; }; };}