summary refs log tree commit diff
path: root/services/cgit.nix
blob: 125b9203402ce3eccb3d248c395ba4277c3badaf (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
{ pkgs, auxiliaryPkgs, ... }:

# TODO: bring in cgit text configuration in `/srv` into nixos repository.
let
  inherit (pkgs) dockerTools;
  inherit (auxiliaryPkgs) common;

  # https://pygments.org/styles/
  cgitDefaultStyle = "pastie";
  cgitNewStyle = "rrt";

  cgit = pkgs.cgit-pink.overrideAttrs (attrs: {
    postPatch = attrs.postPatch + ''
      substituteInPlace filters/syntax-highlighting.py filters/html-converters/md2html \
        --replace-fail '${cgitDefaultStyle}' '${cgitNewStyle}'

      # remove all lines with color, to use the default ones
      sed -i -n "/color/!p" filters/html-converters/md2html
    '';
  });

  cgitLocalPort = 3792;
  cgitDir = "/srv/cgit";
  gitDir = "/srv/git";

  cgitImage = dockerTools.streamLayeredImage {
    name = "cgit";
    tag = cgit.version;
    fromImage = common.alpine.base;

    contents = with pkgs; [
      lighttpd zstd
      python311 python311Packages.pygments
    ] ++ [ cgit ];

    # create cache folder, otherwise
    # the cache is not used.
    # NOTE: `mkdir` here can only create dirs relative
    # to the current folder, but it will be linked
    # to `/` later anyway.
    extraCommands = ''
      mkdir -p ./var/cache/cgit
    '';
  };

in
{
  foundation.services.cgit = {
    image = cgitImage;
    ports = [ [ cgitLocalPort 80 ] ];

    volumes = [
      [ "${cgitDir}/config/cgitrc" "/etc/cgitrc" ]
      [ "${cgitDir}/config/lighttpd.conf" "/etc/lighttpd/cgit.conf" ]
      [ "${cgitDir}/data" "/data" ]

      [ "${gitDir}" "/var/www/cgit" ]
    ];

    entrypoint = "${pkgs.lighttpd}/bin/lighttpd";
    cmd = [
      "-D" # run in foreground
      "-f" "/etc/lighttpd/cgit.conf"
    ];
  };

  services.nginx.virtualHosts."git.rnrd.eu" = {
    useACMEHost = "rnrd.eu";
    forceSSL = true;
    locations = {
      "/" = {
        proxyPass = "http://127.0.0.1:3792";
      };

      "/static/" = {
        alias = "/srv/cgit/static/";
      };
    };
    extraConfig = ''
      access_log /var/log/nginx/git.access.log json_combined;
    '';
  };
}