aboutsummaryrefslogtreecommitdiff
path: root/modules/customs/cgit/default.nix
blob: 63320441fbe078454b23d805297acd1826d1cd5a (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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
{
  config,
  lib,
  pkgs,
  ...
}:
with lib;
let
  cfg = config.modules.customs.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.url}/$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.modules.customs.cgit = {
    enable = mkEnableOption "cgit";
    package = mkPackageOption pkgs "cgit" { };
    user = mkOption {
      default = "git";
      type = types.str;
    };
    group = mkOption {
      default = "git";
      type = types.str;
    };
    scanPath = mkOption {
      default = "/var/lib/cgit";
      type = types.path;
    };
    url = mkOption {
      default = null;
      type = types.str;
    };
    authorizedKeys = mkOption {
      default = [ ];
      type = types.listOf types.str;
    };
    settings = mkOption {
      default = { };
      type =
        with types;
        let
          settingType = oneOf [
            bool
            int
            str
          ];
        in
        attrsOf (oneOf [
          settingType
          (listOf settingType)
        ]);
    };
  };

  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;
      openFirewall = true;
      startWhenNeeded = true;
      settings = {
        AllowUsers = [ cfg.user ];
        PermitRootLogin = "no";
        PasswordAuthentication = false;
      };
      extraConfig = ''
        Match user ${cfg.user}
          AllowTCPForwarding no
          AllowAgentForwarding 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; };
    };

    networking.firewall.allowedTCPPorts = mkIf (cfg.url != null) [
      80
      443
    ];

    services.caddy = mkIf (cfg.url != null) {
      enable = true;
      virtualHosts = {
        ${cfg.url}.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"
            ]}
          '';
      };
    };
  };
}