2 serviceCfg: serviceDrv: iniKey: attrs:
4 cfg = config.services.sourcehut;
5 cfgIni = cfg.settings."${iniKey}";
6 pgSuperUser = config.services.postgresql.superUser;
7 statePath = "/var/lib/sourcehut/${serviceDrv.pname}";
9 with serviceCfg; with lib; mkMerge [
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 ];
22 WorkingDirectory = statePath;
23 StateDirectory = [ "sourcehut/${serviceDrv.pname}" ];
26 preStart = 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
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
40 printf "%s" "${serviceDrv.version}" > ${statePath}/db
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}
49 touch ${statePath}/webhook
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
57 # Mark down current package version
58 printf "%s" "${serviceDrv.version}" > ${statePath}/db