1 { config, lib, pkgs, ... }:
5 cfg = config.services.sourcehut;
8 statePath = "/var/lib/sourcehut/gitsrht";
10 rcfg = config.services.redis;
11 drv = pkgs.sourcehut.gitsrht;
14 options.services.sourcehut.git = {
15 enable = mkEnableOption "git service";
32 Port on which the "git" module should listen.
38 default = "git.sr.ht";
40 PostgreSQL database name for git.sr.ht.
47 example = literalExample "pkgs.gitFull";
49 Git package for git.sr.ht. This can help silence collisions.
54 config = with scfg; lib.mkIf (cfg.enable && scfg.enable) {
55 # sshd refuses to run with `Unsafe AuthorizedKeysCommand ... bad ownership or modes for directory /nix/store`
56 environment.etc."ssh/gitsrht-dispatch" = {
59 #! ${pkgs.stdenv.shell}
60 ${cfg.python}/bin/gitsrht-dispatch "$@"
64 # Needs this in the $PATH when sshing into the server
65 environment.systemPackages = [ cfg.git.package ];
72 # https://stackoverflow.com/questions/22314298/git-push-results-in-fatal-protocol-error-bad-line-length-character-this
73 # Probably could use gitsrht-shell if output is restricted to just parameters...
75 description = "git.sr.ht user";
85 fcgiwrap.enable = true;
87 openssh.authorizedKeysCommand = ''/etc/ssh/gitsrht-dispatch "%u" "%h" "%t" "%k"'';
88 openssh.authorizedKeysCommandUser = "root";
89 openssh.extraConfig = ''
90 PermitUserEnvironment SRHT_*
95 local ${database} ${user} trust
97 ensureDatabases = [ database ];
101 ensurePermissions = { "DATABASE \"${database}\"" = "ALL PRIVILEGES"; };
109 # /var/log is owned by root
110 "f /var/log/git-srht-shell 0644 ${user} ${user} -"
112 "d ${cfg.settings."${iniKey}".repos} 2755 ${user} ${user} -"
116 gitsrht = import ./service.nix { inherit config pkgs lib; } scfg drv iniKey {
117 after = [ "redis.service" "postgresql.service" "network.target" ];
118 requires = [ "redis.service" "postgresql.service" ];
119 wantedBy = [ "gitsrht.target" ];
121 # Needs internally to create repos at the very least
123 description = "git.sr.ht website service";
125 serviceConfig.ExecStart = "${cfg.python}/bin/gunicorn ${drv.pname}.app:app -b ${cfg.address}:${toString port}";
128 gitsrht-periodic = import ./service.nix { inherit config pkgs lib; } scfg drv iniKey {
129 after = [ "gitsrht.service" ];
130 requires = [ "gitsrht.service" ];
132 description = "git.sr.ht periodic service";
134 serviceConfig.Type = mkForce "oneshot";
135 serviceConfig.Restart = mkForce "no";
136 serviceConfig.ExecStart = "${cfg.python}/bin/gitsrht-periodic";
140 after = [ "postgresql.service" "network.target" ];
141 requires = [ "postgresql.service" ];
142 wantedBy = [ "multi-user.target" ];
144 description = "git.sr.ht webhooks service";
151 serviceConfig.ExecStart = "${cfg.python}/bin/celery -A ${drv.pname}.webhooks worker --loglevel INFO --pool eventlet";
158 description = "gitsrht periodic timer";
159 wantedBy = [ "timers.target" ];
161 OnCalendar = "20min";
167 services.sourcehut.settings = {
168 # The authorized keys hook uses this to dispatch to various handlers
169 # The format is a program to exec into as the key, and the user to match as the
170 # value. When someone tries to log in as this user, this program is executed
171 # and is expected to omit an AuthorizedKeys file.
173 # Discard of the string context is in order to allow derivation-derived strings.
174 # This is safe if the relevant package is installed which will be the case if the setting is utilized.
175 "git.sr.ht::dispatch".${builtins.unsafeDiscardStringContext "${pkgs.sourcehut.gitsrht}/bin/gitsrht-keys"} = mkDefault "${user}:${user}";
178 services.nginx.virtualHosts."git.${cfg.originBase}" = {
180 locations."/".proxyPass = "http://${cfg.address}:${toString port}";
181 locations."/query".proxyPass = cfgIni."meta.sr.ht".api-origin;
182 locations."/static".root = "${pkgs.sourcehut.gitsrht}/${pkgs.sourcehut.python.sitePackages}/gitsrht";
184 location = /authorize {
185 proxy_pass http://${cfg.address}:${toString port};
186 proxy_pass_request_body off;
187 proxy_set_header Content-Length "";
188 proxy_set_header X-Original-URI $request_uri;
190 location ~ ^/([^/]+)/([^/]+)/(HEAD|info/refs|objects/info/.*|git-upload-pack).*$ {
191 auth_request /authorize;
193 fastcgi_pass unix:/run/fcgiwrap.sock;
194 fastcgi_param SCRIPT_FILENAME ${pkgs.git}/bin/git-http-backend;
195 fastcgi_param PATH_INFO $uri;
196 fastcgi_param GIT_PROJECT_ROOT $document_root;
197 fastcgi_read_timeout 500s;
198 include ${pkgs.nginx}/conf/fastcgi_params;