8 cfgs = config.services.cgit;
24 genAttrs' = names: f: lib.listToAttrs (map f names);
28 # taken from https://github.com/python/cpython/blob/05cb728d68a278d11466f9a6c8258d914135c96c/Lib/re.py#L251-L266
56 lib.replaceStrings special (map (c: "\\${c}") special);
58 stripLocation = cfg: lib.removeSuffix "/" cfg.nginx.location;
60 regexLocation = cfg: regexEscape (stripLocation cfg);
62 mkFastcgiPass = name: cfg: ''
64 if cfg.nginx.location == "/" then
66 fastcgi_param PATH_INFO $uri;
70 fastcgi_split_path_info ^(${regexLocation cfg})(/.+)$;
71 fastcgi_param PATH_INFO $fastcgi_path_info;
73 }fastcgi_pass unix:${config.services.fcgiwrap.instances."cgit-${name}".socket.address};
81 else if value == false then
87 # list value as multiple lines (for "readme" for example)
89 name: value: if lib.isList value then map (cgitrcLine name) value else [ (cgitrcLine name value) ];
93 pkgs.writeText "cgitrc" ''
95 ${lib.concatStringsSep "\n" (
97 lib.mapAttrsToList cgitrcEntry ({ virtual-root = cfg.nginx.location; } // cfg.settings)
100 ${lib.optionalString (cfg.scanPath != null) (cgitrcLine "scan-path" cfg.scanPath)}
102 # repository settings
103 ${lib.concatStrings (
104 lib.mapAttrsToList (url: settings: ''
105 ${cgitrcLine "repo.url" url}
106 ${lib.concatStringsSep "\n" (lib.mapAttrsToList (name: cgitrcLine "repo.${name}") settings)}
114 fcgiwrapUnitName = name: "fcgiwrap-cgit-${name}";
115 fcgiwrapRuntimeDir = name: "/run/${fcgiwrapUnitName name}";
117 name: cfg: if cfg.scanPath != null then cfg.scanPath else "${fcgiwrapRuntimeDir name}/repos";
122 services.cgit = lib.mkOption {
123 description = "Configure cgit instances.";
125 type = lib.types.attrsOf (
126 lib.types.submodule (
130 enable = lib.mkEnableOption "cgit";
132 package = lib.mkPackageOption pkgs "cgit" { };
134 nginx.virtualHost = lib.mkOption {
135 description = "VirtualHost to serve cgit on, defaults to the attribute name.";
136 type = lib.types.str;
137 default = config._module.args.name;
138 example = "git.example.com";
141 nginx.location = lib.mkOption {
142 description = "Location to serve cgit under.";
143 type = lib.types.str;
148 repos = lib.mkOption {
149 description = "cgit repository settings, see cgitrc(5)";
150 type = with lib.types; attrsOf (attrsOf settingType);
154 path = "/var/lib/git/example";
155 desc = "An example repository";
160 scanPath = lib.mkOption {
161 description = "A path which will be scanned for repositories.";
162 type = lib.types.nullOr lib.types.path;
164 example = "/var/lib/git";
167 settings = lib.mkOption {
168 description = "cgit configuration, see cgitrc(5)";
169 type = lib.types.attrsOf repeatedSettingType;
171 example = lib.literalExpression ''
173 enable-follow-links = true;
174 source-filter = "''${pkgs.cgit}/lib/cgit/filters/syntax-highlighting.py";
179 extraConfig = lib.mkOption {
180 description = "These lines go to the end of cgitrc verbatim.";
181 type = lib.types.lines;
185 user = lib.mkOption {
186 description = "User to run the cgit service as.";
187 type = lib.types.str;
191 group = lib.mkOption {
192 description = "Group to run the cgit service as.";
193 type = lib.types.str;
203 config = lib.mkIf (lib.any (cfg: cfg.enable) (lib.attrValues cfgs)) {
204 assertions = lib.mapAttrsToList (vhost: cfg: {
205 assertion = !cfg.enable || (cfg.scanPath == null) != (cfg.repos == { });
206 message = "Exactly one of services.cgit.${vhost}.scanPath or services.cgit.${vhost}.repos must be set.";
209 users = lib.mkMerge (
210 lib.flip lib.mapAttrsToList cfgs (
212 users.${cfg.user} = {
216 groups.${cfg.group} = { };
221 services.fcgiwrap.instances = lib.flip lib.mapAttrs' cfgs (
223 lib.nameValuePair "cgit-${name}" {
224 process = { inherit (cfg) user group; };
225 socket = { inherit (config.services.nginx) user group; };
229 systemd.services = lib.flip lib.mapAttrs' cfgs (
231 lib.nameValuePair (fcgiwrapUnitName name) (
232 lib.mkIf (cfg.repos != { }) {
233 serviceConfig.RuntimeDirectory = fcgiwrapUnitName name;
235 GIT_PROJECT_ROOT=${lib.escapeShellArg (gitProjectRoot name cfg)}
236 mkdir -p "$GIT_PROJECT_ROOT"
237 cd "$GIT_PROJECT_ROOT"
239 lib.flip lib.mapAttrsToList cfg.repos (
241 ln -s ${lib.escapeShellArg repo.path} ${lib.escapeShellArg name}
250 services.nginx.enable = true;
252 services.nginx.virtualHosts = lib.mkMerge (
253 lib.mapAttrsToList (name: cfg: {
254 ${cfg.nginx.virtualHost} = {
256 (genAttrs' [ "cgit.css" "cgit.png" "favicon.ico" "robots.txt" ] (
258 lib.nameValuePair "= ${stripLocation cfg}/${fileName}" {
260 alias ${cfg.package}/cgit/${fileName};
265 "~ ${regexLocation cfg}/.+/(info/refs|git-upload-pack)" = {
266 fastcgiParams = rec {
267 SCRIPT_FILENAME = "${pkgs.git}/libexec/git-core/git-http-backend";
268 GIT_HTTP_EXPORT_ALL = "1";
269 GIT_PROJECT_ROOT = gitProjectRoot name cfg;
270 HOME = GIT_PROJECT_ROOT;
272 extraConfig = mkFastcgiPass name cfg;
274 "${stripLocation cfg}/" = {
276 SCRIPT_FILENAME = "${cfg.package}/cgit/cgit.cgi";
277 QUERY_STRING = "$args";
278 HTTP_HOST = "$server_name";
279 CGIT_CONFIG = mkCgitrc cfg;
281 extraConfig = mkFastcgiPass name cfg;