gitolite: update
[sourcephile-nix.git] / nixos / modules / services / misc / sourcehut / service.nix
index 845ccdb22cb47adc8e3b04d766877d8a8a0cc83b..b3c0efc07ddfd453851acddf78d7cfe3a596dceb 100644 (file)
@@ -2,8 +2,7 @@ srv:
 { configIniOfService
 , srvsrht ? "${srv}srht" # Because "buildsrht" does not follow that pattern (missing an "s").
 , iniKey ? "${srv}.sr.ht"
-, webhooks ? null
-, redisDatabase ? null
+, webhooks ? false
 , extraTimers ? {}
 , mainService ? {}
 , extraServices ? {}
@@ -14,7 +13,8 @@ srv:
 
 with lib;
 let
-  inherit (config.services) postgresql redis;
+  inherit (config.services) postgresql;
+  redis = config.services.redis.servers."sourcehut-${srvsrht}";
   inherit (config.users) users;
   cfg = config.services.sourcehut;
   configIni = configIniOfService srv;
@@ -24,6 +24,12 @@ let
     rootDir = "/run/sourcehut/chroots/${serviceName}";
     in
     mkMerge [ extraService {
+    after = [ "network.target" ] ++
+      optional cfg.postgresql.enable "postgresql.service" ++
+      optional cfg.redis.enable "redis-sourcehut-${srvsrht}.service";
+    requires =
+      optional cfg.postgresql.enable "postgresql.service" ++
+      optional cfg.redis.enable "redis-sourcehut-${srvsrht}.service";
     path = [ pkgs.gawk ];
     environment.HOME = runDir;
     serviceConfig = {
@@ -31,12 +37,14 @@ let
       Group = mkDefault srvCfg.group;
       RuntimeDirectory = [
         "sourcehut/${serviceName}"
+        # Used by *srht-keys which reads ../config.ini
+        "sourcehut/${serviceName}/subdir"
         "sourcehut/chroots/${serviceName}"
       ];
       RuntimeDirectoryMode = "2750";
       # No need for the chroot path once inside the chroot
       InaccessiblePaths = [ "-+${rootDir}" ];
-      # g+rx is for group members (eg. nginx)
+      # g+rx is for group members (eg. fcgiwrap or nginx)
       # to read Git/Mercurial repositories, buildlogs, etc.
       # o+x is for intermediate directories created by BindPaths= and like,
       # as they're owned by root:root.
@@ -56,7 +64,7 @@ let
         "/run/systemd"
         ] ++
         optional cfg.postgresql.enable "/run/postgresql" ++
-        optional cfg.redis.enable "/run/redis";
+        optional cfg.redis.enable "/run/redis-sourcehut-${srvsrht}";
       # LoadCredential= are unfortunately not available in ExecStartPre=
       # Hence this one is run as root (the +) with RootDirectoryStartOnly=
       # to reach credentials wherever they are.
@@ -133,16 +141,54 @@ in
       type = types.port;
       default = port;
       description = ''
-        Port on which the "${srv}" module should listen.
+        Port on which the "${srv}" backend should listen.
       '';
     };
 
-    database = mkOption {
-      type = types.str;
-      default = "${srv}.sr.ht";
-      description = ''
-        PostgreSQL database name for ${srv}.sr.ht.
-      '';
+    redis = {
+      host = mkOption {
+        type = types.str;
+        default = "unix:/run/redis-sourcehut-${srvsrht}/redis.sock?db=0";
+        example = "redis://shared.wireguard:6379/0";
+        description = ''
+          The redis host URL. This is used for caching and temporary storage, and must
+          be shared between nodes (e.g. git1.sr.ht and git2.sr.ht), but need not be
+          shared between services. It may be shared between services, however, with no
+          ill effect, if this better suits your infrastructure.
+        '';
+      };
+    };
+
+    postgresql = {
+      database = mkOption {
+        type = types.str;
+        default = "${srv}.sr.ht";
+        description = ''
+          PostgreSQL database name for the ${srv}.sr.ht service,
+          used if <xref linkend="opt-services.sourcehut.postgresql.enable"/> is <literal>true</literal>.
+        '';
+      };
+    };
+
+    gunicorn = {
+      extraArgs = mkOption {
+        type = with types; listOf str;
+        default = ["--timeout 120" "--workers 1" "--log-level=info"];
+        description = "Extra arguments passed to Gunicorn.";
+      };
+    };
+  } // optionalAttrs webhooks {
+    webhooks = {
+      extraArgs = mkOption {
+        type = with types; listOf str;
+        default = ["--loglevel DEBUG" "--pool eventlet" "--without-heartbeat"];
+        description = "Extra arguments passed to the Celery responsible for webhooks.";
+      };
+      celeryConfig = mkOption {
+        type = types.lines;
+        default = "";
+        description = "Content of the <literal>celeryconfig.py</literal> used by the Celery responsible for webhooks.";
+      };
     };
   };
 
@@ -160,36 +206,34 @@ in
       } // optionalAttrs (cfg.postgresql.enable
         && hasSuffix "0" (postgresql.settings.unix_socket_permissions or "")) {
         "postgres".members = [ srvCfg.user ];
+      } // optionalAttrs (cfg.redis.enable
+        && hasSuffix "0" (redis.settings.unixsocketperm or "")) {
+        "redis-sourcehut-${srvsrht}".members = [ srvCfg.user ];
       };
     };
 
     services.nginx = mkIf cfg.nginx.enable {
-      virtualHosts."${srv}.${cfg.settings."sr.ht".global-domain}" = {
+      virtualHosts."${srv}.${cfg.settings."sr.ht".global-domain}" = mkMerge [ {
         forceSSL = true;
         locations."/".proxyPass = "http://${cfg.listenAddress}:${toString srvCfg.port}";
-        locations."/query".proxyPass = cfg.settings."meta.sr.ht".api-origin;
         locations."/static" = {
           root = "${pkgs.sourcehut.${srvsrht}}/${pkgs.sourcehut.python.sitePackages}/${srvsrht}";
           extraConfig = mkDefault ''
             expires 30d;
           '';
         };
-      };
+      } cfg.nginx.virtualHost ];
     };
 
     services.postgresql = mkIf cfg.postgresql.enable {
       authentication = ''
-        local ${srvCfg.database} ${srvCfg.user} trust
-      ''
-      # shrt-keys stores SSH keys in the PostgreSQL database of the service calling it
-      + optionalString (elem srv ["builds" "git" "hg"]) ''
-        local ${srvCfg.database} ${users."sshsrht".name} trust
+        local ${srvCfg.postgresql.database} ${srvCfg.user} trust
       '';
-      ensureDatabases = [ srvCfg.database ];
+      ensureDatabases = [ srvCfg.postgresql.database ];
       ensureUsers = map (name: {
           inherit name;
-          ensurePermissions = { "DATABASE \"${srvCfg.database}\"" = "ALL PRIVILEGES"; };
-        }) ([srvCfg.user] ++ optional (elem srv ["builds" "git" "hg"]) users."sshsrht".name);
+          ensurePermissions = { "DATABASE \"${srvCfg.postgresql.database}\"" = "ALL PRIVILEGES"; };
+        }) [srvCfg.user];
     };
 
     services.sourcehut.services = mkDefault (filter (s: cfg.${s}.enable)
@@ -197,36 +241,36 @@ in
 
     services.sourcehut.settings = mkMerge [
       {
-        "${srv}.sr.ht" = {
-          origin = mkDefault "https://${srv}.${cfg.settings."sr.ht".global-domain}";
-        };
+        "${srv}.sr.ht".origin = mkDefault "https://${srv}.${cfg.settings."sr.ht".global-domain}";
       }
 
-      (mkIf (cfg.redis.enable && webhooks != null) {
-        "${srv}.sr.ht".webhooks = mkDefault "redis://localhost:${toString redis.port}/${toString (cfg.redis.firstDatabase + webhooks.redisDatabase)}";
-      })
-
-      (mkIf (cfg.redis.enable && redisDatabase != null) {
-        "${srv}.sr.ht".redis = mkDefault "redis://localhost:${toString redis.port}/${toString (cfg.redis.firstDatabase + redisDatabase)}";
-      })
-
       (mkIf cfg.postgresql.enable {
-        "${srv}.sr.ht".connection-string = mkDefault "postgresql:///${srvCfg.database}?user=${srvCfg.user}&host=/run/postgresql";
+        "${srv}.sr.ht".connection-string = mkDefault "postgresql:///${srvCfg.postgresql.database}?user=${srvCfg.user}&host=/run/postgresql";
       })
     ];
 
+    services.redis.servers."sourcehut-${srvsrht}" = mkIf cfg.redis.enable {
+      enable = true;
+      databases = 3;
+      syslog = true;
+      # TODO: set a more informed value
+      save = mkDefault [ [1800 10] [300 100] ];
+      settings = {
+        # TODO: set a more informed value
+        maxmemory = "128MB";
+        maxmemory-policy = "volatile-ttl";
+      };
+    };
+
     systemd.services = mkMerge [
       {
         "${srvsrht}" = baseService srvsrht { allowStripe = srv == "meta"; } (mkMerge [
         {
           description = "sourcehut ${srv}.sr.ht website service";
-          after = [ "network.target" ]
-            ++ optional cfg.postgresql.enable "postgresql.service"
-            ++ optional (srv != "meta" && cfg.meta.enable) "metasrht-api.service";
-          requires = optional cfg.postgresql.enable "postgresql.service";
-          wantedBy = [ "multi-user.target" ];
           before = optional cfg.nginx.enable "nginx.service";
-          path = optional cfg.postgresql.enable config.services.postgresql.package;
+          wants = optional cfg.nginx.enable "nginx.service";
+          wantedBy = [ "multi-user.target" ];
+          path = optional cfg.postgresql.enable postgresql.package;
           # Beware: change in credentials' content will not trigger restart.
           restartTriggers = [ configIni ];
           serviceConfig = {
@@ -235,7 +279,7 @@ in
             #RestartSec = mkDefault "2min";
             StateDirectory = [ "sourcehut/${srvsrht}" ];
             StateDirectoryMode = "2750";
-            ExecStart = "${cfg.python}/bin/gunicorn ${srvsrht}.app:app -b ${cfg.listenAddress}:${toString srvCfg.port}";
+            ExecStart = "${cfg.python}/bin/gunicorn ${srvsrht}.app:app --name ${srvsrht} --bind ${cfg.listenAddress}:${toString srvCfg.port} " + concatStringsSep " " srvCfg.gunicorn.extraArgs;
           };
           preStart = let
             version = pkgs.sourcehut.${srvsrht}.version;
@@ -272,37 +316,33 @@ in
         } mainService ]);
       }
 
-      (mkIf (webhooks != null) {
-        "${srvsrht}-webhooks" = baseService "${srvsrht}-webhooks" {} (mkMerge [
+      (mkIf webhooks {
+        "${srvsrht}-webhooks" = baseService "${srvsrht}-webhooks" {}
           {
             description = "sourcehut ${srv}.sr.ht webhooks service";
-            after = [ "${srvsrht}.service" ]
-              ++ optional cfg.redis.enable "redis.service";
-            requires = [ "${srvsrht}.service" ]
-              ++ optional cfg.redis.enable "redis.service";
+            after = [ "${srvsrht}.service" ];
             wantedBy = [ "${srvsrht}.service" ];
-            requiredBy = [ "${srvsrht}.service" ];
+            partOf = [ "${srvsrht}.service" ];
+            preStart = ''
+              cp ${pkgs.writeText "${srvsrht}-webhooks-celeryconfig.py" srvCfg.webhooks.celeryConfig} \
+                 /run/sourcehut/${srvsrht}-webhooks/celeryconfig.py
+            '';
             serviceConfig = {
               Type = "simple";
               Restart = "always";
-              ExecStart = "${cfg.python}/bin/celery -A ${srvsrht}.webhooks worker --loglevel INFO --pool eventlet";
+              ExecStart = "${cfg.python}/bin/celery --app ${srvsrht}.webhooks worker --hostname ${srvsrht}-webhooks@%%h " + concatStringsSep " " srvCfg.webhooks.extraArgs;
               # Avoid crashing: os.getloadavg()
               ProcSubset = mkForce "all";
-              InaccessiblePaths = [ "-/run/postgresql" ];
             };
-          }
-          (webhooks.service or {})
-        ]);
+          };
       })
 
       (mapAttrs (timerName: timer: (baseService timerName {} (mkMerge [
         {
           description = "sourcehut ${timerName} service";
-          after = [ "${srvsrht}.service" ];
-          requires = [ "${srvsrht}.service" ];
+          after = [ "network.target" "${srvsrht}.service" ];
           serviceConfig = {
             Type = "oneshot";
-            Restart = "no";
             ExecStart = "${cfg.python}/bin/${timerName}";
           };
         }
@@ -311,10 +351,11 @@ in
 
       (mapAttrs (serviceName: extraService: baseService serviceName {} (mkMerge [
         {
-          description = "sourcehut ${serviceName}";
+          description = "sourcehut ${serviceName} service";
+          # So that extraServices have the PostgreSQL database initialized.
           after = [ "${srvsrht}.service" ];
-          requires = [ "${srvsrht}.service" ];
-          wantedBy = [ "multi-user.target" ];
+          wantedBy = [ "${srvsrht}.service" ];
+          partOf = [ "${srvsrht}.service" ];
           serviceConfig = {
             Type = "simple";
             Restart = mkDefault "always";