1 { config, lib, pkgs, ... }:
5 cfg = config.services.sourcehut;
9 rcfg = config.services.redis;
10 drv = pkgs.sourcehut.gitsrht;
13 options.services.sourcehut.git = {
29 Port on which the "git" module should listen.
35 default = "git.sr.ht";
37 PostgreSQL database name for git.sr.ht.
41 statePath = mkOption {
43 default = "${cfg.statePath}/gitsrht";
45 State path for git.sr.ht.
52 example = literalExample "pkgs.gitFull";
54 Git package for git.sr.ht. This can help silence collisions.
59 config = with scfg; lib.mkIf (cfg.enable && elem "git" cfg.services) {
60 # sshd refuses to run with `Unsafe AuthorizedKeysCommand ... bad ownership or modes for directory /nix/store`
61 environment.etc."ssh/gitsrht-dispatch" = {
64 #! ${pkgs.stdenv.shell}
65 ${cfg.python}/bin/gitsrht-dispatch "$@"
69 # Needs this in the $PATH when sshing into the server
70 environment.systemPackages = [ cfg.git.package ];
77 # https://stackoverflow.com/questions/22314298/git-push-results-in-fatal-protocol-error-bad-line-length-character-this
78 # Probably could use gitsrht-shell if output is restricted to just parameters...
80 description = "git.sr.ht user";
90 cron.systemCronJobs = [ "*/20 * * * * ${cfg.python}/bin/gitsrht-periodic" ];
91 fcgiwrap.enable = true;
93 openssh.authorizedKeysCommand = ''/etc/ssh/gitsrht-dispatch "%u" "%h" "%t" "%k"'';
94 openssh.authorizedKeysCommandUser = "root";
95 openssh.extraConfig = ''
96 PermitUserEnvironment SRHT_*
101 local ${database} ${user} trust
103 ensureDatabases = [ database ];
107 ensurePermissions = { "DATABASE \"${database}\"" = "ALL PRIVILEGES"; };
115 # /var/log is owned by root
116 "f /var/log/git-srht-shell 0644 ${user} ${user} -"
118 "d ${statePath} 0750 ${user} ${user} -"
119 "d ${cfg.settings."${iniKey}".repos} 2755 ${user} ${user} -"
123 gitsrht = import ./service.nix { inherit config pkgs lib; } scfg drv iniKey {
124 after = [ "redis.service" "postgresql.service" "network.target" ];
125 requires = [ "redis.service" "postgresql.service" ];
126 wantedBy = [ "multi-user.target" ];
128 # Needs internally to create repos at the very least
130 description = "git.sr.ht website service";
132 serviceConfig.ExecStart = "${cfg.python}/bin/gunicorn ${drv.pname}.app:app -b ${cfg.address}:${toString port}";
136 after = [ "postgresql.service" "network.target" ];
137 requires = [ "postgresql.service" ];
138 wantedBy = [ "multi-user.target" ];
140 description = "git.sr.ht webhooks service";
147 serviceConfig.ExecStart = "${cfg.python}/bin/celery -A ${drv.pname}.webhooks worker --loglevel INFO --pool eventlet";
152 services.sourcehut.settings = {
153 # URL git.sr.ht is being served at (protocol://domain)
154 "git.sr.ht".origin = mkDefault "http://git.${cfg.originBase}";
155 # Address and port to bind the debug server to
156 "git.sr.ht".debug-host = mkDefault "0.0.0.0";
157 "git.sr.ht".debug-port = mkDefault port;
158 # Configures the SQLAlchemy connection string for the database.
159 "git.sr.ht".connection-string = mkDefault "postgresql:///${database}?user=${user}&host=/var/run/postgresql";
160 # The redis connection used for the webhooks worker
161 "git.sr.ht".webhooks = mkDefault "redis://${rcfg.bind}:${toString rcfg.port}/1";
163 # A post-update script which is installed in every git repo.
164 "git.sr.ht".post-update-script = mkDefault "${pkgs.sourcehut.gitsrht}/bin/gitsrht-update-hook";
166 # git.sr.ht's OAuth client ID and secret for meta.sr.ht
167 # Register your client at meta.example.org/oauth
168 "git.sr.ht".oauth-client-id = mkDefault null;
169 "git.sr.ht".oauth-client-secret = mkDefault null;
170 # Path to git repositories on disk
171 "git.sr.ht".repos = mkDefault "/var/lib/git";
173 "git.sr.ht".outgoing-domain = mkDefault "http://git.${cfg.originBase}";
175 # The authorized keys hook uses this to dispatch to various handlers
176 # The format is a program to exec into as the key, and the user to match as the
177 # value. When someone tries to log in as this user, this program is executed
178 # and is expected to omit an AuthorizedKeys file.
180 # Discard of the string context is in order to allow derivation-derived strings.
181 # This is safe if the relevant package is installed which will be the case if the setting is utilized.
182 "git.sr.ht::dispatch".${builtins.unsafeDiscardStringContext "${pkgs.sourcehut.gitsrht}/bin/gitsrht-keys"} = mkDefault "${user}:${user}";
185 services.nginx.virtualHosts."git.${cfg.originBase}" = {
187 locations."/".proxyPass = "http://${cfg.address}:${toString port}";
188 locations."/query".proxyPass = "http://${cfg.address}:${toString (port + 100)}";
189 locations."/static".root = "${pkgs.sourcehut.gitsrht}/${pkgs.sourcehut.python.sitePackages}/gitsrht";
191 location = /authorize {
192 proxy_pass http://${cfg.address}:${toString port};
193 proxy_pass_request_body off;
194 proxy_set_header Content-Length "";
195 proxy_set_header X-Original-URI $request_uri;
197 location ~ ^/([^/]+)/([^/]+)/(HEAD|info/refs|objects/info/.*|git-upload-pack).*$ {
198 auth_request /authorize;
200 fastcgi_pass unix:/run/fcgiwrap.sock;
201 fastcgi_param SCRIPT_FILENAME ${pkgs.git}/bin/git-http-backend;
202 fastcgi_param PATH_INFO $uri;
203 fastcgi_param GIT_PROJECT_ROOT $document_root;
204 fastcgi_read_timeout 500s;
205 include ${pkgs.nginx}/conf/fastcgi_params;