3 , srvsrht ? "${srv}srht" # Because "buildsrht" does not follow that pattern (missing an "s").
4 , iniKey ? "${srv}.sr.ht"
14 { config, lib, pkgs, ... }:
18 inherit (config.services) postgresql redis;
19 inherit (config.users) users;
20 cfg = config.services.sourcehut;
21 configIni = configIniOfService srv;
23 baseService = serviceName: { noStripe ? true }: extraService: let
24 runDir = "/run/sourcehut/${serviceName}";
25 rootDir = "/run/sourcehut/chroots/${serviceName}";
27 mkMerge [ commonService extraService {
29 environment.HOME = runDir;
32 Group = mkDefault srvCfg.user;
34 "sourcehut/${serviceName}"
35 "sourcehut/chroots/${serviceName}"
37 RuntimeDirectoryMode = "2750";
38 # No need for the chroot path once inside the chroot
39 InaccessiblePaths = [ "-+${rootDir}" ];
40 # For intermediate directories created by BindPaths= and others
42 RootDirectory = rootDir;
43 RootDirectoryStartOnly = true;
46 # config.ini is looked up in there, before /etc/srht/config.ini
47 # Note that it fails to be set in ExecStartPre=
48 WorkingDirectory = mkDefault ("-"+runDir);
57 optional cfg.redis.enable "/run/redis" ++
58 optional cfg.postgresql.enable "/run/postgresql";
59 # LoadCredential= are unfortunately not available in ExecStartPre=
60 # Hence this one is run as root (the +) with RootDirectoryStartOnly=
61 # to reach credentials wherever they are.
62 # Note that each systemd service gets its own ${runDir}/config.ini file.
63 ExecStartPre = mkBefore [("+"+pkgs.writeShellScript "${serviceName}-credentials" ''
65 # Replace values begining with a '<' by the content of the file whose name is after.
66 gawk '{ if (match($0,/^([^=]+=)<(.+)/,m)) { getline f < m[2]; print m[1] f } else print $0 }' ${configIni} |
67 ${optionalString noStripe "gawk '!/^stripe-secret-key=/' |"}
68 install -o ${srvCfg.user} -g root -m 400 /dev/stdin ${runDir}/config.ini
70 LogsDirectory = [ "sourcehut/${serviceName}" ];
71 # The following options are only for optimizing:
72 # systemd-analyze security
73 AmbientCapabilities = "";
74 CapabilityBoundingSet = "";
75 # ProtectClock= adds DeviceAllow=char-rtc r
77 LockPersonality = true;
78 MemoryDenyWriteExecute = true;
79 NoNewPrivileges = true;
80 PrivateDevices = true;
82 PrivateNetwork = mkDefault false;
86 ProtectControlGroups = true;
88 ProtectHostname = true;
89 ProtectKernelLogs = true;
90 ProtectKernelModules = true;
91 ProtectKernelTunables = true;
92 ProtectProc = "invisible";
93 ProtectSystem = "strict";
95 RestrictAddressFamilies = [ "AF_UNIX" "AF_INET" "AF_INET6" ];
96 RestrictNamespaces = true;
97 RestrictRealtime = true;
98 RestrictSUIDSGID = true;
99 #SocketBindAllow = [ "tcp:${toString srvCfg.port}" "tcp:${toString srvCfg.prometheusPort}" ];
100 #SocketBindDeny = "any";
103 "~@aio" "~@keyring" "~@memlock" "~@privileged" "~@resources" "~@timer"
106 SystemCallArchitectures = "native";
111 options.services.sourcehut.${srv} = {
112 enable = mkEnableOption "${srv} service";
118 User for ${srv}.sr.ht.
126 Port on which the "${srv}" module should listen.
130 database = mkOption {
132 default = "${srv}.sr.ht";
134 PostgreSQL database name for ${srv}.sr.ht.
139 config = lib.mkIf (cfg.enable && srvCfg.enable) (mkMerge [ extraConfig {
144 group = mkDefault srvCfg.user;
145 description = mkDefault "sourcehut user for ${srv}.sr.ht";
149 "${srvCfg.user}" = { };
150 } // optionalAttrs (cfg.postgresql.enable
151 && hasSuffix "0" (postgresql.settings.unix_socket_permissions or "")) {
152 "postgres".members = [ srvCfg.user ];
156 services.nginx.virtualHosts = mkIf cfg.nginx.enable {
157 "${srv}.${cfg.settings."sr.ht".global-domain}" = {
159 locations."/".proxyPass = "http://${cfg.listenAddress}:${toString srvCfg.port}";
160 locations."/query".proxyPass = cfg.settings."meta.sr.ht".api-origin;
161 locations."/static".root = "${pkgs.sourcehut.${srvsrht}}/${pkgs.sourcehut.python.sitePackages}/${srvsrht}";
165 services.postgresql = mkIf cfg.postgresql.enable {
167 local ${srvCfg.database} ${srvCfg.user} trust
169 # shrt-keys stores SSH keys in the PostgreSQL database of the service calling it
170 + optionalString (elem srv ["builds" "git" "hg"]) ''
171 local ${srvCfg.database} ${users."sshsrht".name} trust
173 ensureDatabases = [ srvCfg.database ];
174 ensureUsers = map (name: {
176 ensurePermissions = { "DATABASE \"${srvCfg.database}\"" = "ALL PRIVILEGES"; };
177 }) ([srvCfg.user] ++ optional (elem srv ["builds" "git" "hg"]) users."sshsrht".name);
180 services.sourcehut.services = mkDefault (filter (s: cfg.${s}.enable)
181 [ "builds" "dispatch" "git" "hg" "hub" "lists" "man" "meta" "pages" "paste" "todo" ]);
183 services.sourcehut.settings = mkMerge [
186 origin = mkDefault "https://${srv}.${cfg.settings."sr.ht".global-domain}";
190 (mkIf (cfg.redis.enable && webhooks != null) {
191 "${srv}.sr.ht".webhooks = mkDefault "redis://localhost:${toString redis.port}/${toString (cfg.redis.firstDatabase + webhooks.redisDatabase)}";
194 (mkIf (cfg.redis.enable && redisDatabase != null) {
195 "${srv}.sr.ht".redis = mkDefault "redis://localhost:${toString redis.port}/${toString (cfg.redis.firstDatabase + redisDatabase)}";
198 (mkIf cfg.postgresql.enable {
199 "${srv}.sr.ht".connection-string = mkDefault "postgresql:///${srvCfg.database}?user=${srvCfg.user}&host=/run/postgresql";
203 systemd.services = mkMerge [
204 { "${srvsrht}" = baseService srvsrht { noStripe = srv != "meta"; }
206 description = "sourcehut ${srv}.sr.ht website service";
207 after = [ "network.target" ]
208 ++ optional cfg.postgresql.enable "postgresql.service"
209 ++ optional (srv != "meta" && cfg.meta.enable) "metasrht-api.service";
210 requires = optional cfg.postgresql.enable "postgresql.service";
211 wantedBy = [ "multi-user.target" ];
212 path = optional cfg.postgresql.enable config.services.postgresql.package;
213 # Beware: change in credentials' content will not trigger restart.
214 restartTriggers = [ configIni ];
217 Restart = mkDefault "always";
218 #RestartSec = mkDefault "2min";
219 StateDirectory = [ "sourcehut/${srvsrht}" ];
220 StateDirectoryMode = "2750";
221 ExecStart = "${cfg.python}/bin/gunicorn ${srvsrht}.app:app -b ${cfg.listenAddress}:${toString srvCfg.port}";
224 version = pkgs.sourcehut.${srvsrht}.version;
225 stateDir = "/var/lib/sourcehut/${srvsrht}";
228 # Use the /run/sourcehut/${srvsrht}/config.ini
229 # installed by a previous ExecStartPre= in baseService
230 cd /run/sourcehut/${srvsrht}
232 if test ! -e ${stateDir}/db; then
233 # Setup the initial database.
234 # Note that it stamps the alembic head afterward
235 ${cfg.python}/bin/${srvsrht}-initdb
236 echo ${version} >${stateDir}/db
239 ${optionalString cfg.settings.${iniKey}.migrate-on-upgrade ''
240 if [ "$(cat ${stateDir}/db)" != "${version}" ]; then
241 # Manage schema migrations using alembic
242 ${cfg.python}/bin/${srvsrht}-migrate -a upgrade head
243 echo ${version} >${stateDir}/db
247 # Update copy of each users' profile to the latest
248 # See https://lists.sr.ht/~sircmpwn/sr.ht-admins/<20190302181207.GA13778%40cirno.my.domain>
249 if test ! -e ${stateDir}/webhook; then
250 # Update ${iniKey}'s users' profile copy to the latest
251 ${cfg.python}/bin/srht-update-profiles ${iniKey}
252 touch ${stateDir}/webhook
258 (mkIf (webhooks != null) {
259 "${srvsrht}-webhooks" = baseService "${srvsrht}-webhooks" {}
261 description = "sourcehut ${srv}.sr.ht webhooks service";
262 after = [ "${srvsrht}.service" ]
263 ++ optional cfg.redis.enable "redis.service";
264 requires = [ "${srvsrht}.service" ]
265 ++ optional cfg.redis.enable "redis.service";
266 wantedBy = [ "${srvsrht}.service" ];
267 requiredBy = [ "${srvsrht}.service" ];
271 Group = mkDefault srvCfg.user;
273 ExecStart = "${cfg.python}/bin/celery -A ${srvsrht}.webhooks worker --loglevel INFO --pool eventlet";
274 # Avoid crashing: os.getloadavg()
275 ProcSubset = mkForce "all";
280 (mapAttrs (timerName: timerConfig: (baseService timerName {}
282 description = "sourcehut ${timerName} service";
283 after = [ "${srvsrht}.service" ];
284 requires = [ "${srvsrht}.service" ];
288 Group = mkDefault srvCfg.user;
290 ExecStart = "${cfg.python}/bin/${timerName}";
295 (mapAttrs (serviceName: service: baseService serviceName {}
297 description = "sourcehut ${serviceName}";
298 after = [ "${srvsrht}.service" ];
299 requires = [ "${srvsrht}.service" ];
300 wantedBy = [ "multi-user.target" ];
304 Group = mkDefault srvCfg.user;
305 Restart = mkDefault "always";
311 systemd.timers = mapAttrs (timerName: timerConfig:
313 description = "sourcehut timer for ${timerName}";
314 wantedBy = [ "timers.target" ];