at 22.05-pre 2.2 kB view raw
1{ config, pkgs, lib }: 2serviceCfg: serviceDrv: iniKey: attrs: 3let 4 cfg = config.services.sourcehut; 5 cfgIni = cfg.settings."${iniKey}"; 6 pgSuperUser = config.services.postgresql.superUser; 7 8 setupDB = pkgs.writeScript "${serviceDrv.pname}-gen-db" '' 9 #! ${cfg.python}/bin/python 10 from ${serviceDrv.pname}.app import db 11 db.create() 12 ''; 13in 14with serviceCfg; with lib; recursiveUpdate 15{ 16 environment.HOME = statePath; 17 path = [ config.services.postgresql.package ] ++ (attrs.path or [ ]); 18 restartTriggers = [ config.environment.etc."sr.ht/config.ini".source ]; 19 serviceConfig = { 20 Type = "simple"; 21 User = user; 22 Group = user; 23 Restart = "always"; 24 WorkingDirectory = statePath; 25 } // (if (cfg.statePath == "/var/lib/sourcehut/${serviceDrv.pname}") then { 26 StateDirectory = [ "sourcehut/${serviceDrv.pname}" ]; 27 } else {}) 28 ; 29 30 preStart = '' 31 if ! test -e ${statePath}/db; then 32 # Setup the initial database 33 ${setupDB} 34 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 39 fi 40 41 printf "%s" "${serviceDrv.version}" > ${statePath}/db 42 fi 43 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} 49 50 touch ${statePath}/webhook 51 fi 52 53 ${optionalString (builtins.hasAttr "migrate-on-upgrade" cfgIni && cfgIni.migrate-on-upgrade == "yes") '' 54 if [ "$(cat ${statePath}/db)" != "${serviceDrv.version}" ]; then 55 # Manage schema migrations using alembic 56 ${cfg.python}/bin/${serviceDrv.pname}-migrate -a upgrade head 57 58 # Mark down current package version 59 printf "%s" "${serviceDrv.version}" > ${statePath}/db 60 fi 61 ''} 62 63 ${attrs.preStart or ""} 64 ''; 65} 66 (builtins.removeAttrs attrs [ "path" "preStart" ])