about summary refs log tree commit diff
path: root/machines/srv1/cgit.nix
blob: 5c0749c6bcb9ab9a6dfa4aa47fd10f92318f494b (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
{ config, lib, pkgs, ... }:

with lib;

let
  cfg = config.services.lighttpd.pn-cgit;
  pathPrefix = if stringLength cfg.subdir == 0 then "" else "/" + cfg.subdir;
  configFile = pkgs.writeText "cgitrc"
    ''
      # default paths to static assets
      css=${pathPrefix}/cgit.css
      logo=${pathPrefix}/cgit.png
      favicon=${pathPrefix}/favicon.ico
      # user configuration
      ${cfg.configText}
    '';
in
{

  options.services.lighttpd.pn-cgit = {

    enable = mkOption {
      default = false;
      type = types.bool;
      description = ''
        If true, enable cgit (fast web interface for git repositories) as a
        sub-service in lighttpd.
      '';
    };

    subdir = mkOption {
      default = "cgit";
      example = "";
      type = types.str;
      description = ''
        The subdirectory in which to serve cgit. The web application will be
        accessible at http://yourserver/''${subdir}
      '';
    };

    logo = mkOption {
      default = "${pkgs.cgit}/cgit/cgit.png";
      example = "";
      type = types.str;
      description = ''
        Logo for your cgit server
      '';
    };

    configText = mkOption {
      default = "";
      example = ''
        source-filter=''${pkgs.cgit}/lib/cgit/filters/syntax-highlighting.py
        about-filter=''${pkgs.cgit}/lib/cgit/filters/about-formatting.sh
        cache-size=1000
        scan-path=/srv/git
      '';
      type = types.lines;
      description = ''
        Verbatim contents of the cgit runtime configuration file. Documentation
        (with cgitrc example file) is available in "man cgitrc". Or online:
        http://git.zx2c4.com/cgit/tree/cgitrc.5.txt
      '';
    };

  };

  config = mkIf cfg.enable {

    # make the cgitrc manpage available
    environment.systemPackages = [ pkgs.cgit ];

    # declare module dependencies
    services.lighttpd.enableModules = [ "mod_cgi" "mod_alias" "mod_setenv" ];

    services.lighttpd.extraConfig = ''
      $HTTP["url"] =~ "^/${cfg.subdir}" {
          cgi.assign = (
              "cgit.cgi" => "${pkgs.cgit}/cgit/cgit.cgi"
          )
          alias.url = (
              "${pathPrefix}/cgit.css" => "${pkgs.cgit}/cgit/cgit.css",
              "${pathPrefix}/cgit.png" => "${cfg.logo}",
              "${pathPrefix}"          => "${pkgs.cgit}/cgit/cgit.cgi"
          )
          setenv.add-environment = (
              "CGIT_CONFIG" => "${configFile}"
          )
      }
    '';

    systemd.services.lighttpd.preStart = ''
      mkdir -p /var/cache/cgit
      chown lighttpd:lighttpd /var/cache/cgit
    '';

  };

}