aboutsummaryrefslogtreecommitdiff
path: root/modules/services/nixos/cgit.nix
blob: 235335fca8193fa4c848b60a943ade195c39d38d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
{
  pkgs,
  config,
  lib,
  hosts,
  ...
}: let
  cfg = config.collinux.services.cgit;

  authorizedKeys =
    hosts
    |> builtins.mapAttrs (_: data: ''command="git-shell -c \"$SSH_ORIGINAL_COMMAND\"" ${data.user_pubkey or null}'')
    |> builtins.attrValues
    |> builtins.filter (x: x != null);
in {
  config = lib.mkIf cfg.enable {
    users.groups."git" = {};
    users.users."git" = {
      isSystemUser = true;
      group = "git";
      home = "/var/lib/cgit";
      homeMode = 755;
      shell = "${pkgs.git}/libexec/git-core/git-shell";

      openssh.authorizedKeys.keys = authorizedKeys;
    };

    services.openssh.extraConfig = lib.mkAfter ''
      Match User git
        AllowTcpForwarding no
        X11Forwarding no
        PermitTunnel no
        ForceCommand git-shell
        PubkeyAuthentication yes
        AuthenticationMethods publickey
    '';

    environment.etc."cgitrc" = ''
      scan-path=/var/lib/cgit
      virtual-root=/
      repo.sort=age

      readme=:README.md
    '';

    services.fcgiwrap.instance."cgit" = {
      enable = true;

      process = {
        user = "git";
        group = "git";
      };

      socket = {
        user = "caddy";
        group = "caddy";
        type = "unix";
        address = "/run/fcgiwrap-cgit.sock";
      };
    };

    services.caddy.virtualHosts."git.ganymede".extraConfig = ''
      tls internal

      @assets path /cgit.css /cgit.js /cgit.png /favicon.ico /robots.txt
      handle @assets {
      	root * ${pkgs.cgit}/cgit
      	file_server
      }

      reverse_proxy unix//run/fcgiwrap-cgit.sock {
      	transport fastcgi {
      		env SCRIPT_FILENAME ${pkgs.cgit}/cgit/cgit.cgi
      	}
      }
    '';
  };
}