]> Git — Sourcephile - sourcephile-nix.git/blob - nixos/modules/services/misc/sourcehut/service.nix
sourcehut: fix preStart merging
[sourcephile-nix.git] / nixos / modules / services / misc / sourcehut / service.nix
1 { config, pkgs, lib }:
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 WorkingDirectory = statePath;
22 StateDirectory = [ "sourcehut/${serviceDrv.pname}" ];
23 };
24
25 preStart = mkBefore ''
26 if ! test -e ${statePath}/db; then
27 # Setup the initial database
28 ${cfg.python}/bin/python <<EOF
29 from ${serviceDrv.pname}.app import db
30 db.create()
31 EOF
32
33 # Set the initial state of the database for future database upgrades
34 if test -e ${cfg.python}/bin/${serviceDrv.pname}-migrate; then
35 # Run alembic stamp head once to tell alembic the schema is up-to-date
36 ${cfg.python}/bin/${serviceDrv.pname}-migrate stamp head
37 fi
38
39 printf "%s" "${serviceDrv.version}" > ${statePath}/db
40 fi
41
42 # Update copy of each users' profile to the latest
43 # See https://lists.sr.ht/~sircmpwn/sr.ht-admins/<20190302181207.GA13778%40cirno.my.domain>
44 if ! test -e ${statePath}/webhook; then
45 # Update ${iniKey}'s users' profile copy to the latest
46 ${cfg.python}/bin/srht-update-profiles ${iniKey}
47
48 touch ${statePath}/webhook
49 fi
50
51 ${optionalString cfgIni.migrate-on-upgrade ''
52 if [ "$(cat ${statePath}/db)" != "${serviceDrv.version}" ]; then
53 # Manage schema migrations using alembic
54 ${cfg.python}/bin/${serviceDrv.pname}-migrate -a upgrade head
55
56 # Mark down current package version
57 printf "%s" "${serviceDrv.version}" > ${statePath}/db
58 fi
59 ''}
60 '';
61 }
62 attrs
63 ]