{ config, lib, pkgs, ... }: with lib; let cfg = config.services.sourcehut; cfgIni = cfg.settings; scfg = cfg.meta; iniKey = "meta.sr.ht"; statePath = "/var/lib/sourcehut/metasrht"; drv = pkgs.sourcehut.metasrht; in { options.services.sourcehut.meta = { enable = mkEnableOption "meta service"; user = mkOption { type = types.str; default = "metasrht"; description = '' User for meta.sr.ht. ''; }; port = mkOption { type = types.port; default = 5000; description = '' Port on which the "meta" module should listen. ''; }; database = mkOption { type = types.str; default = "meta.sr.ht"; description = '' PostgreSQL database name for meta.sr.ht. ''; }; }; config = with scfg; lib.mkIf (cfg.enable && scfg.enable) { assertions = [ { assertion = with cfgIni."meta.sr.ht::billing"; enabled == "yes" -> (stripe-public-key != null && stripe-secret-key != null); message = "If meta.sr.ht::billing is enabled, the keys should be defined."; } ]; users = { users = { ${user} = { isSystemUser = true; group = user; description = "meta.sr.ht user"; }; }; groups = { "${user}" = { }; }; }; services.postgresql = { authentication = '' local ${database} ${user} trust ''; ensureDatabases = [ database ]; ensureUsers = [ { name = user; ensurePermissions = { "DATABASE \"${database}\"" = "ALL PRIVILEGES"; }; } ]; }; systemd = { services = let commonPreStart = '' # Configure client(s) as "preauthorized" ${concatMapStringsSep "\n\n" (srv: '' if ! test -e "${statePath}/${srv}.oauth" || [ "$(cat ${statePath}/${srv}.oauth)" != "${cfgIni.${srv}.oauth-client-id}" ]; then # Configure ${srv}'s OAuth client as "preauthorized" psql '${database}' \ -c "UPDATE oauthclient SET preauthorized = true WHERE client_id = '${cfgIni.${srv}.oauth-client-id}'" printf "%s" "${cfgIni.${srv}.oauth-client-id}" > "${statePath}/${srv}.oauth" fi '') (builtins.attrNames (filterAttrs (k: v: let srv = builtins.match "^([a-z]*)\\.sr\\.ht$" k; in srv != null && cfg.${head srv}.enable && ((v.oauth-client-id or null) != null) ) cfg.settings))} ''; in { metasrht = import ./service.nix { inherit config pkgs lib; } scfg drv iniKey { after = [ "postgresql.service" "network.target" ]; requires = [ "postgresql.service" ]; wantedBy = [ "multi-user.target" ]; description = "meta.sr.ht website service"; preStart = commonPreStart; serviceConfig.ExecStart = "${cfg.python}/bin/gunicorn ${drv.pname}.app:app -b ${cfg.address}:${toString port}"; }; metasrht-api = import ./service.nix { inherit config pkgs lib; } scfg drv iniKey { after = [ "postgresql.service" "network.target" ]; requires = [ "postgresql.service" ]; wantedBy = [ "metasrht.service" ]; description = "meta.sr.ht api service"; preStart = commonPreStart; serviceConfig.ExecStart = "${pkgs.sourcehut.metasrht}/bin/metasrht-api -b :${toString (port + 100)}"; }; metasrht-daily = import ./service.nix { inherit config pkgs lib; } scfg drv iniKey { after = [ "metasrht.service" ]; requires = [ "metasrht.service" ]; description = "meta.sr.ht daily service"; serviceConfig.Type = mkForce "oneshot"; serviceConfig.Restart = mkForce "no"; serviceConfig.ExecStart = "${cfg.python}/bin/metasrht-daily"; }; metasrht-webhooks = { after = [ "postgresql.service" "network.target" ]; requires = [ "postgresql.service" ]; wantedBy = [ "multi-user.service" ]; description = "meta.sr.ht webhooks service"; serviceConfig = { Type = "simple"; User = user; Restart = "always"; ExecStart = "${cfg.python}/bin/celery -A ${drv.pname}.webhooks worker --loglevel INFO --pool eventlet"; }; }; }; }; systemd.timers = { metasrht-daily = { description = "metasrht daily timer"; wantedBy = [ "timers.target" ]; timerConfig = { OnCalendar = "daily"; Persistent = true; AccuracySec = "1h"; }; }; }; services.nginx.virtualHosts."meta.${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.metasrht}/${pkgs.sourcehut.python.sitePackages}/metasrht"; }; environment.systemPackages = [ (pkgs.linkFarm "metasrht" [ { name = "bin/metasrht-manageuser"; path = "${cfg.python}/bin/metasrht-manageuser"; } ]) ]; }; }