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 path = [ config.services.postgresql.package ] ++ (attrs.path or [ ]);
13 restartTriggers = [ config.environment.etc."sr.ht/config.ini".source ];
19 WorkingDirectory = statePath;
20 StateDirectory = [ "sourcehut/${serviceDrv.pname}" ];
24 if ! test -e ${statePath}/db; then
25 # Setup the initial database
26 ${cfg.python}/bin/python <<EOF
27 from ${serviceDrv.pname}.app import db
31 # Set the initial state of the database for future database upgrades
32 if test -e ${cfg.python}/bin/${serviceDrv.pname}-migrate; then
33 # Run alembic stamp head once to tell alembic the schema is up-to-date
34 ${cfg.python}/bin/${serviceDrv.pname}-migrate stamp head
37 printf "%s" "${serviceDrv.version}" > ${statePath}/db
40 # Update copy of each users' profile to the latest
41 # See https://lists.sr.ht/~sircmpwn/sr.ht-admins/<20190302181207.GA13778%40cirno.my.domain>
42 if ! test -e ${statePath}/webhook; then
43 # Update ${iniKey}'s users' profile copy to the latest
44 ${cfg.python}/bin/srht-update-profiles ${iniKey}
46 touch ${statePath}/webhook
49 ${optionalString cfgIni.migrate-on-upgrade ''
50 if [ "$(cat ${statePath}/db)" != "${serviceDrv.version}" ]; then
51 # Manage schema migrations using alembic
52 ${cfg.python}/bin/${serviceDrv.pname}-migrate -a upgrade head
54 # Mark down current package version
55 printf "%s" "${serviceDrv.version}" > ${statePath}/db
59 ${attrs.preStart or ""}
62 (builtins.removeAttrs attrs [ "path" "preStart" ])