]> Git — Sourcephile - sourcephile-nix.git/blob - nixos/modules/services/misc/sourcehut/dispatch.nix
sourcehut: factorize commonPreStart
[sourcephile-nix.git] / nixos / modules / services / misc / sourcehut / dispatch.nix
1 { config, lib, pkgs, ... }:
2
3 with lib;
4 let
5 cfg = config.services.sourcehut;
6 cfgIni = cfg.settings;
7 scfg = cfg.dispatch;
8 iniKey = "dispatch.sr.ht";
9
10 drv = pkgs.sourcehut.dispatchsrht;
11 in
12 {
13 options.services.sourcehut.dispatch = {
14 user = mkOption {
15 type = types.str;
16 default = "dispatchsrht";
17 description = ''
18 User for dispatch.sr.ht.
19 '';
20 };
21
22 port = mkOption {
23 type = types.port;
24 default = 5005;
25 description = ''
26 Port on which the "dispatch" module should listen.
27 '';
28 };
29
30 database = mkOption {
31 type = types.str;
32 default = "dispatch.sr.ht";
33 description = ''
34 PostgreSQL database name for dispatch.sr.ht.
35 '';
36 };
37
38 statePath = mkOption {
39 type = types.path;
40 default = "${cfg.statePath}/dispatchsrht";
41 description = ''
42 State path for dispatch.sr.ht.
43 '';
44 };
45 };
46
47 config = with scfg; lib.mkIf (cfg.enable && elem "dispatch" cfg.services) {
48
49 users = {
50 users = {
51 "${user}" = {
52 isSystemUser = true;
53 group = user;
54 description = "dispatch.sr.ht user";
55 };
56 };
57
58 groups = {
59 "${user}" = { };
60 };
61 };
62
63 services.postgresql = {
64 authentication = ''
65 local ${database} ${user} trust
66 '';
67 ensureDatabases = [ database ];
68 ensureUsers = [
69 {
70 name = user;
71 ensurePermissions = { "DATABASE \"${database}\"" = "ALL PRIVILEGES"; };
72 }
73 ];
74 };
75
76 systemd = {
77 tmpfiles.rules = [
78 "d ${statePath} 0750 ${user} ${user} -"
79 ];
80
81 services.dispatchsrht = import ./service.nix { inherit config pkgs lib; } scfg drv iniKey {
82 after = [ "postgresql.service" "network.target" ];
83 requires = [ "postgresql.service" ];
84 wantedBy = [ "multi-user.target" ];
85
86 description = "dispatch.sr.ht website service";
87
88 serviceConfig.ExecStart = "${cfg.python}/bin/gunicorn ${drv.pname}.app:app -b ${cfg.address}:${toString port}";
89 };
90 };
91
92 services.sourcehut.settings = {
93 # URL dispatch.sr.ht is being served at (protocol://domain)
94 "dispatch.sr.ht".origin = mkDefault "http://dispatch.${cfg.originBase}";
95 # Address and port to bind the debug server to
96 "dispatch.sr.ht".debug-host = mkDefault "0.0.0.0";
97 "dispatch.sr.ht".debug-port = mkDefault port;
98 # Configures the SQLAlchemy connection string for the database.
99 "dispatch.sr.ht".connection-string = mkDefault "postgresql:///${database}?user=${user}&host=/var/run/postgresql";
100 # dispatch.sr.ht's OAuth client ID and secret for meta.sr.ht
101 # Register your client at meta.example.org/oauth
102 "dispatch.sr.ht".oauth-client-id = mkDefault null;
103 "dispatch.sr.ht".oauth-client-secret = mkDefault null;
104
105 # Github Integration
106 "dispatch.sr.ht::github".oauth-client-id = mkDefault null;
107 "dispatch.sr.ht::github".oauth-client-secret = mkDefault null;
108
109 # Gitlab Integration
110 "dispatch.sr.ht::gitlab".enabled = mkDefault null;
111 "dispatch.sr.ht::gitlab".canonical-upstream = mkDefault "gitlab.com";
112 "dispatch.sr.ht::gitlab".repo-cache = mkDefault "./repo-cache";
113 # "dispatch.sr.ht::gitlab"."gitlab.com" = mkDefault "GitLab:application id:secret";
114 };
115
116 services.nginx.virtualHosts."dispatch.${cfg.originBase}" = {
117 forceSSL = true;
118 locations."/".proxyPass = "http://${cfg.address}:${toString port}";
119 locations."/query".proxyPass = "http://${cfg.address}:${toString (port + 100)}";
120 locations."/static".root = "${pkgs.sourcehut.dispatchsrht}/${pkgs.sourcehut.python.sitePackages}/dispatchsrht";
121 };
122 };
123 }