2 serviceCfg: serviceDrv: iniKey: attrs:
4 cfg = config.services.sourcehut;
5 cfgIni = cfg.settings."${iniKey}";
6 pgSuperUser = config.services.postgresql.superUser;
8 setupDB = pkgs.writeScript "${serviceDrv.pname}-gen-db" ''
9 #! ${cfg.python}/bin/python
10 from ${serviceDrv.pname}.app import db
14 with serviceCfg; with lib; recursiveUpdate
16 environment.HOME = statePath;
17 path = [ config.services.postgresql.package ] ++ (attrs.path or [ ]);
18 restartTriggers = [ config.environment.etc."sr.ht/config.ini".source ];
24 WorkingDirectory = statePath;
25 } // (if (cfg.statePath == "/var/lib/sourcehut/${serviceDrv.pname}") then {
26 StateDirectory = [ "sourcehut/${serviceDrv.pname}" ];
31 if ! test -e ${statePath}/db; then
32 # Setup the initial database
35 # Set the initial state of the database for future database upgrades
36 if test -e ${cfg.python}/bin/${serviceDrv.pname}-migrate; then
37 # Run alembic stamp head once to tell alembic the schema is up-to-date
38 ${cfg.python}/bin/${serviceDrv.pname}-migrate stamp head
41 printf "%s" "${serviceDrv.version}" > ${statePath}/db
44 # Update copy of each users' profile to the latest
45 # See https://lists.sr.ht/~sircmpwn/sr.ht-admins/<20190302181207.GA13778%40cirno.my.domain>
46 if ! test -e ${statePath}/webhook; then
47 # Update ${iniKey}'s users' profile copy to the latest
48 ${cfg.python}/bin/srht-update-profiles ${iniKey}
50 touch ${statePath}/webhook
53 ${optionalString cfgIni.migrate-on-upgrade ''
54 if [ "$(cat ${statePath}/db)" != "${serviceDrv.version}" ]; then
55 # Manage schema migrations using alembic
56 ${cfg.python}/bin/${serviceDrv.pname}-migrate -a upgrade head
58 # Mark down current package version
59 printf "%s" "${serviceDrv.version}" > ${statePath}/db
63 ${attrs.preStart or ""}
66 (builtins.removeAttrs attrs [ "path" "preStart" ])