]> Git — Sourcephile - sourcephile-nix.git/blob - nixos/modules/services/misc/sourcehut/service.nix
sourcehut: replace the services option by enable options
[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 path = [ config.services.postgresql.package ] ++ (attrs.path or [ ]);
13 restartTriggers = [ config.environment.etc."sr.ht/config.ini".source ];
14 serviceConfig = {
15 Type = "simple";
16 User = user;
17 Group = user;
18 Restart = "always";
19 WorkingDirectory = statePath;
20 StateDirectory = [ "sourcehut/${serviceDrv.pname}" ];
21 };
22
23 preStart = ''
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
28 db.create()
29 EOF
30
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
35 fi
36
37 printf "%s" "${serviceDrv.version}" > ${statePath}/db
38 fi
39
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}
45
46 touch ${statePath}/webhook
47 fi
48
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
53
54 # Mark down current package version
55 printf "%s" "${serviceDrv.version}" > ${statePath}/db
56 fi
57 ''}
58
59 ${attrs.preStart or ""}
60 '';
61 }
62 (builtins.removeAttrs attrs [ "path" "preStart" ])
63 ]