1 { config, lib, pkgs, ... }:
5 cfg = config.services.sourcehut;
9 statePath = "/var/lib/sourcehut/metasrht";
11 drv = pkgs.sourcehut.metasrht;
14 options.services.sourcehut.meta = {
15 enable = mkEnableOption "meta service";
29 Port on which the "meta" module should listen.
35 default = "meta.sr.ht";
37 PostgreSQL database name for meta.sr.ht.
42 config = with scfg; lib.mkIf (cfg.enable && scfg.enable) {
46 assertion = with cfgIni."meta.sr.ht::billing"; enabled == "yes" -> (stripe-public-key != null && stripe-secret-key != null);
47 message = "If meta.sr.ht::billing is enabled, the keys should be defined.";
56 description = "meta.sr.ht user";
65 services.postgresql = {
67 local ${database} ${user} trust
69 ensureDatabases = [ database ];
73 ensurePermissions = { "DATABASE \"${database}\"" = "ALL PRIVILEGES"; };
81 # Configure client(s) as "preauthorized"
82 ${concatMapStringsSep "\n\n"
84 if ! test -e "${statePath}/${srv}.oauth" || [ "$(cat ${statePath}/${srv}.oauth)" != "${cfgIni.${srv}.oauth-client-id}" ]; then
85 # Configure ${srv}'s OAuth client as "preauthorized"
87 -c "UPDATE oauthclient SET preauthorized = true WHERE client_id = '${cfgIni.${srv}.oauth-client-id}'"
89 printf "%s" "${cfgIni.${srv}.oauth-client-id}" > "${statePath}/${srv}.oauth"
92 (builtins.attrNames (filterAttrs (k: v:
93 let srv = builtins.match "^([a-z]*)\\.sr\\.ht$" k; in
94 srv != null && cfg.${head srv}.enable &&
95 ((v.oauth-client-id or null) != null)
99 metasrht = import ./service.nix { inherit config pkgs lib; initDB = true; } scfg drv iniKey {
100 after = [ "postgresql.service" "network.target" ];
101 requires = [ "postgresql.service" ];
102 wantedBy = [ "multi-user.target" ];
104 description = "meta.sr.ht website service";
106 preStart = commonPreStart;
108 serviceConfig.ExecStart = "${cfg.python}/bin/gunicorn ${drv.pname}.app:app -b ${cfg.address}:${toString port}";
111 metasrht-api = import ./service.nix { inherit config pkgs lib; } scfg drv iniKey {
112 after = [ "postgresql.service" "network.target" ];
113 requires = [ "postgresql.service" ];
114 wantedBy = [ "metasrht.service" ];
116 description = "meta.sr.ht api service";
118 preStart = commonPreStart;
120 serviceConfig.ExecStart = "${pkgs.sourcehut.metasrht}/bin/metasrht-api -b :${toString (port + 100)}";
123 metasrht-daily = import ./service.nix { inherit config pkgs lib; } scfg drv iniKey {
124 after = [ "metasrht.service" ];
125 requires = [ "metasrht.service" ];
127 description = "meta.sr.ht daily service";
129 serviceConfig.Type = mkForce "oneshot";
130 serviceConfig.Restart = mkForce "no";
131 serviceConfig.ExecStart = "${cfg.python}/bin/metasrht-daily";
134 metasrht-webhooks = {
135 after = [ "postgresql.service" "network.target" ];
136 requires = [ "postgresql.service" ];
137 wantedBy = [ "multi-user.service" ];
139 description = "meta.sr.ht webhooks service";
144 ExecStart = "${cfg.python}/bin/celery -A ${drv.pname}.webhooks worker --loglevel INFO --pool eventlet";
153 description = "metasrht daily timer";
154 wantedBy = [ "timers.target" ];
156 OnCalendar = "daily";
163 services.nginx.virtualHosts."meta.${cfg.originBase}" = {
165 locations."/".proxyPass = "http://${cfg.address}:${toString port}";
166 locations."/query".proxyPass = cfgIni."meta.sr.ht".api-origin;
167 locations."/static".root = "${pkgs.sourcehut.metasrht}/${pkgs.sourcehut.python.sitePackages}/metasrht";
170 environment.systemPackages = [
171 (pkgs.linkFarm "metasrht" [
172 { name = "bin/metasrht-manageuser";
173 path = "${cfg.python}/bin/metasrht-manageuser";