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 cron.systemCronJobs = [ "*/20 * * * * ${cfg.python}/bin/gitsrht-periodic" ];
86 fcgiwrap.enable = true;
88 openssh.authorizedKeysCommand = ''/etc/ssh/gitsrht-dispatch "%u" "%h" "%t" "%k"'';
89 openssh.authorizedKeysCommandUser = "root";
90 openssh.extraConfig = ''
91 PermitUserEnvironment SRHT_*
96 local ${database} ${user} trust
98 ensureDatabases = [ database ];
102 ensurePermissions = { "DATABASE \"${database}\"" = "ALL PRIVILEGES"; };
110 # /var/log is owned by root
111 "f /var/log/git-srht-shell 0644 ${user} ${user} -"
113 "d ${cfg.settings."${iniKey}".repos} 2755 ${user} ${user} -"
117 gitsrht = import ./service.nix { inherit config pkgs lib; } scfg drv iniKey {
118 after = [ "redis.service" "postgresql.service" "network.target" ];
119 requires = [ "redis.service" "postgresql.service" ];
120 wantedBy = [ "multi-user.target" ];
122 # Needs internally to create repos at the very least
124 description = "git.sr.ht website service";
126 serviceConfig.ExecStart = "${cfg.python}/bin/gunicorn ${drv.pname}.app:app -b ${cfg.address}:${toString port}";
130 after = [ "postgresql.service" "network.target" ];
131 requires = [ "postgresql.service" ];
132 wantedBy = [ "multi-user.target" ];
134 description = "git.sr.ht webhooks service";
141 serviceConfig.ExecStart = "${cfg.python}/bin/celery -A ${drv.pname}.webhooks worker --loglevel INFO --pool eventlet";
146 services.sourcehut.settings = {
147 # The authorized keys hook uses this to dispatch to various handlers
148 # The format is a program to exec into as the key, and the user to match as the
149 # value. When someone tries to log in as this user, this program is executed
150 # and is expected to omit an AuthorizedKeys file.
152 # Discard of the string context is in order to allow derivation-derived strings.
153 # This is safe if the relevant package is installed which will be the case if the setting is utilized.
154 "git.sr.ht::dispatch".${builtins.unsafeDiscardStringContext "${pkgs.sourcehut.gitsrht}/bin/gitsrht-keys"} = mkDefault "${user}:${user}";
157 services.nginx.virtualHosts."git.${cfg.originBase}" = {
159 locations."/".proxyPass = "http://${cfg.address}:${toString port}";
160 locations."/query".proxyPass = cfgIni."meta.sr.ht".api-origin;
161 locations."/static".root = "${pkgs.sourcehut.gitsrht}/${pkgs.sourcehut.python.sitePackages}/gitsrht";
163 location = /authorize {
164 proxy_pass http://${cfg.address}:${toString port};
165 proxy_pass_request_body off;
166 proxy_set_header Content-Length "";
167 proxy_set_header X-Original-URI $request_uri;
169 location ~ ^/([^/]+)/([^/]+)/(HEAD|info/refs|objects/info/.*|git-upload-pack).*$ {
170 auth_request /authorize;
172 fastcgi_pass unix:/run/fcgiwrap.sock;
173 fastcgi_param SCRIPT_FILENAME ${pkgs.git}/bin/git-http-backend;
174 fastcgi_param PATH_INFO $uri;
175 fastcgi_param GIT_PROJECT_ROOT $document_root;
176 fastcgi_read_timeout 500s;
177 include ${pkgs.nginx}/conf/fastcgi_params;