{ config, lib, pkgs, ... }: with lib; let cfg = config.services.sourcehut; cfgIni = cfg.settings; scfg = cfg.hub; iniKey = "hub.sr.ht"; statePath = "/var/lib/sourcehut/hubsrht"; drv = pkgs.sourcehut.hubsrht; in { options.services.sourcehut.hub = { enable = mkEnableOption "hub service"; user = mkOption { type = types.str; default = "hubsrht"; description = '' User for hub.sr.ht. ''; }; port = mkOption { type = types.port; default = 5014; description = '' Port on which the "hub" module should listen. ''; }; database = mkOption { type = types.str; default = "hub.sr.ht"; description = '' PostgreSQL database name for hub.sr.ht. ''; }; }; config = with scfg; lib.mkIf (cfg.enable && scfg.enable) { users = { users = { "${user}" = { isSystemUser = true; group = user; description = "hub.sr.ht user"; }; }; groups = { "${user}" = { }; }; }; services.postgresql = { authentication = '' local ${database} ${user} trust ''; ensureDatabases = [ database ]; ensureUsers = [ { name = user; ensurePermissions = { "DATABASE \"${database}\"" = "ALL PRIVILEGES"; }; } ]; }; systemd = { services.hubsrht = import ./service.nix { inherit config pkgs lib; } scfg drv iniKey { after = [ "postgresql.service" "network.target" ]; requires = [ "postgresql.service" ]; wantedBy = [ "multi-user.target" ]; description = "hub.sr.ht website service"; serviceConfig.ExecStart = "${cfg.python}/bin/gunicorn ${drv.pname}.app:app -b ${cfg.address}:${toString port}"; }; }; services.nginx.virtualHosts."${cfg.originBase}" = { forceSSL = true; locations."/".proxyPass = "http://${cfg.address}:${toString port}"; locations."/query".proxyPass = cfgIni."meta.sr.ht".api-origin; locations."/static".root = "${pkgs.sourcehut.hubsrht}/${pkgs.sourcehut.python.sitePackages}/hubsrht"; }; services.nginx.virtualHosts."hub.${cfg.originBase}" = { globalRedirect = "${cfg.originBase}"; forceSSL = true; }; }; }