]> Git — Sourcephile - sourcephile-nix.git/blob - nixos/modules/services/misc/sourcehut/service.nix
losurdo: sourcehut: sync config with module changes
[sourcephile-nix.git] / nixos / modules / services / misc / sourcehut / service.nix
1 { config, pkgs, lib, initDB ? false }:
2 serviceCfg: serviceDrv: iniKey: attrs:
3 let
4 cfg = config.services.sourcehut;
5 cfgIni = cfg.settings."${iniKey}";
6 pgSuperUser = config.services.postgresql.superUser;
7 statePath = "/var/lib/sourcehut/${serviceDrv.pname}";
8 in
9 with serviceCfg; with lib; mkMerge [
10 {
11 environment.HOME = statePath;
12 after = optional (serviceDrv.pname != "metasrht" && cfg.meta.enable) "metasrht.service";
13 requires = optional (serviceDrv.pname != "metasrht" && cfg.meta.enable) "metasrht.service";
14 path = [ config.services.postgresql.package ];
15 restartTriggers = [ config.environment.etc."sr.ht/config.ini".source ];
16 serviceConfig = {
17 Type = "simple";
18 User = user;
19 Group = user;
20 Restart = "always";
21 RestartSec = "2min";
22 WorkingDirectory = statePath;
23 StateDirectory = [ "sourcehut/${serviceDrv.pname}" ];
24 };
25
26 preStart = mkIf initDB (mkBefore ''
27 if ! test -e ${statePath}/db; then
28 # Setup the initial database
29 ${cfg.python}/bin/python <<EOF
30 from ${serviceDrv.pname}.app import db
31 db.create()
32 EOF
33
34 # Set the initial state of the database for future database upgrades
35 if test -e ${cfg.python}/bin/${serviceDrv.pname}-migrate; then
36 # Run alembic stamp head once to tell alembic the schema is up-to-date
37 ${cfg.python}/bin/${serviceDrv.pname}-migrate stamp head
38 fi
39
40 printf "%s" "${serviceDrv.version}" > ${statePath}/db
41 fi
42
43 # Update copy of each users' profile to the latest
44 # See https://lists.sr.ht/~sircmpwn/sr.ht-admins/<20190302181207.GA13778%40cirno.my.domain>
45 if ! test -e ${statePath}/webhook; then
46 # Update ${iniKey}'s users' profile copy to the latest
47 ${cfg.python}/bin/srht-update-profiles ${iniKey}
48
49 touch ${statePath}/webhook
50 fi
51
52 ${optionalString cfgIni.migrate-on-upgrade ''
53 if [ "$(cat ${statePath}/db)" != "${serviceDrv.version}" ]; then
54 # Manage schema migrations using alembic
55 ${cfg.python}/bin/${serviceDrv.pname}-migrate -a upgrade head
56
57 # Mark down current package version
58 printf "%s" "${serviceDrv.version}" > ${statePath}/db
59 fi
60 ''}
61 '');
62 }
63 attrs
64 ]