aboutsummaryrefslogtreecommitdiff
path: root/modules/customs/cgit/default.nix
blob: dc30d2ab84d37bab60c0fffac5806e812370a5a7 (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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
{ config, lib, pkgs, ... }:
with lib;
let
  cfg = config.services.cgit;

  mkCgitrc = cfg:
    pkgs.writeText "cgitrc" (let
      cgitConfig = {
        css = "/cgit.css";
        logo = "/cgit.png";
        favicon = "/favicon.ico";
        about-filter = "${cfg.package}/lib/cgit/filters/about-formatting.sh";
        source-filter = "${cfg.package}/lib/cgit/filters/syntax-highlighting.py";
        enable-git-config = 1;
        enable-http-clone = 1;
        remove-suffix = 1;
        clone-url = "https://${cfg.virtualHost}/$CGIT_REPO_URL";
        scan-path = cfg.scanPath;
      };
    in
      generators.toKeyValue { } (cfg.settings // cgitConfig)
    );

  mkCgitAssets = pkg: files:
    strings.concatStringsSep "\n" (
      builtins.map (f: ''
      handle_path /${f} {
        root * ${pkg}/cgit/${f}
        file_server
      }
      '') files
    );
in
{
  disabledModules = [ "services/networking/cgit.nix" ];

  options = {
    services.cgit = {
      enable = mkEnableOption "cgit";
      package = mkPackageOption pkgs "cgit" { };
      user = mkOption {
        default = "git";
        type = types.str;
        description = "User to run cgit service as.";
      };
      group = mkOption {
        default = "git";
        type = types.str;
        description = "Group to run cgit service as.";
      };
      scanPath = mkOption {
        default = "/var/lib/cgit";
        type = types.path;
        description = "A path which will be scanned for repositories.";
      };
      virtualHost = mkOption {
        default = null;
        type = types.str;
        description = "Virtual host to serve cgit on.";
      };
      authorizedKeys = mkOption {
        default = [ ];
        type = types.listOf types.str;
        description = "SSH keys for authorized git users.";
      };
      settings = mkOption {
        default = { };
        type = with types; let settingType = oneOf [ bool int str ]; in
          attrsOf (oneOf [
            settingType
            (listOf settingType)
          ]);
        description = "Additional cgit configuration. See cgitrc(5).";
      };
    };
  };

  config = mkIf cfg.enable {
    programs.git = {
      enable = true;
      config.init.defaultBranch = "main";
    };

    # Setup git user with git-shell for authenticated pushes.
    users.users.${cfg.user} = {
      isSystemUser = true;
      home = cfg.scanPath;
      group = cfg.group;
      shell = "${pkgs.git}/bin/git-shell";
      openssh.authorizedKeys.keys = cfg.authorizedKeys;
    };

    users.groups.${cfg.group} = {};

    # Harden git user to prevent SSH port forwarding to other servers.
    services.openssh = {
      enable = true;
      settings.AllowUsers = [ cfg.user ];
      extraConfig = ''
        Match user ${cfg.user}
          AllowTCPForwarding no
          AllowAgentForwarding no
          PasswordAuthentication no
          PermitTTY no
          X11Forwarding no
      '';
    };

    # Configure fcgiwrap instance for caddy to properly serve cgi scripts.
    # Reference: https://github.com/NixOS/nixpkgs/blob/nixos-24.11/nixos/modules/services/networking/cgit.nix
    services.fcgiwrap.instances.cgit = {
      process = { inherit (cfg) user group; };
      socket = { inherit (config.services.caddy) user group; };
    };

    services.caddy.virtualHosts.${cfg.virtualHost}.extraConfig =
      let
        socket = config.services.fcgiwrap.instances.cgit.socket.address;
      in ''
        encode zstd gzip

        reverse_proxy unix/${socket} {
          transport fastcgi {
            env SCRIPT_FILENAME ${cfg.package}/cgit/cgit.cgi
            env CGIT_CONFIG ${mkCgitrc cfg}
          }
        }

        ${mkCgitAssets cfg.package [
          "cgit.css" "cgit.png" "favicon.ico" "robots.txt"
        ]}
      '';
  };
}