about summary refs log tree commit diff
path: root/modules/server/cgit/default.nix
blob: c09863c34ad369dbe7900216494759f9b33eebbc (plain) (blame)
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
{ lib, pkgs, config, ... }:

let
  inherit (builtins) split;
  inherit (lib) foldr groupBy head mapAttrs mapAttrsToList mkOption nameValuePair
    optionalAttrs types;

  cfg = config.services.cgit;

  instancesByVhost = groupBy ({ value, ... }: value.vhost)
    (mapAttrsToList nameValuePair cfg.instances);

  vhostConfigs = mapAttrs (vhost: instances:
    foldr (l: r: l // r) {} (map ({ name, value }: let
      unslashedPath = head (split "/+$" value.path);
      # We'll be dealing almost exclusively with paths ending in /,
      # since otherwise Nginx likes to do simple prefix matching.
      path = "${unslashedPath}/";
    in {
      locations = {
        ${path} = {
          alias = "${value.package}/cgit/";
          tryFiles = "$uri @${name}-cgit";
        };
        "@${name}-cgit" = {
          root = "${value.package}/cgit";

          fastcgiParams.CGIT_CONFIG = "${value.config}";
          fastcgiParams.SCRIPT_FILENAME = "$document_root/cgit.cgi";
          fastcgiParams.PATH_INFO = "$fastcgi_path_info";
          fastcgiParams.QUERY_STRING = "$args";
          fastcgiParams.HTTP_HOST = "$server_name";

          extraConfig = ''
            fastcgi_split_path_info ^(${path})(.*)$;
            fastcgi_pass unix:/run/fcgiwrap.sock;
          '';
        };
      } // optionalAttrs (unslashedPath != "") {
        ${unslashedPath} = {
          return = "301 ${path}";
        };
      };
    }) instances)
  ) instancesByVhost;
in

{
  options.services.cgit = {
    instances = mkOption {
      type = types.attrsOf (types.submodule {
        options = {
          vhost = mkOption {
            type = types.str;
            example = "spectrum-os.org";
            description = "Nginx vhost for the cgit";
          };

          path = mkOption {
            type = types.strMatching "/(.*[^/])?";
            default = "/";
            example = "/git";
            description = ''
              Path to be appended to all cgit URLs.

              Leading slashes are mandatory; trailing slashes are forbidden.
            '';
          };

          package = mkOption {
            type = types.package;
            default = pkgs.cgit;
            description = "cgit package to use";
          };

          config = mkOption {
            type = types.package;
            description = ''
              Configuration file for cgit.  See
              <citerefentry><refentrytitle>cgitrc</refentrytitle>
              <manvolnum>5</manvolnum></citerefentry>.
            '';
          };
        };
      });
      default = {};
      description = "List of cgit instances to run";
    };
  };

  config = {
    services.fcgiwrap = optionalAttrs (cfg.instances != {}) { enable = true; };
    services.nginx.virtualHosts = vhostConfigs;
  };
}