aboutsummaryrefslogtreecommitdiff
path: root/modules/cgit.nix
blob: 1e626316d9ab804efc5885cbe4f933a0b4abce8d (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
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
{ pkgs, lib, config, ... }:
let
    cfg = config.teh-nix.services.cgit;
    cgitrc = pkgs.writeText "cgitrc" ''
css=/static/cgit.css
logo=/static/cgit.png
favicon=/static/favicon.ico
repository-sort=age

root-title=${cfg.title}
root-desc=${cfg.description}

enable-blame=1
enable-commit-graph=1
enable-log-filecount=1
enable-log-linecount=1
enable-index-links=1

snapshots=tar.gz zip
enable-http-clone=1
clone-prefix=https://${cfg.domain}

readme=:README
readme=:readme
readme=:readme.txt
readme=:README.txt
readme=:readme.md
readme=:README.md

${cfg.extraConfig}

about-filter=${cfg.package}/lib/cgit/filters/about-formatting.sh
source-filter=${cfg.package}/lib/cgit/filters/syntax-highlighting.py

enable-git-config=1
scan-path=${cfg.directory}
'';
in
{
    options.teh-nix.services.cgit = with lib;{
        enable = mkEnableOption "Enable cgit";
        user = mkOption {
            type = types.str;
            default = "cgit";
            description = "Username for the user that will run cgit";
        };
        authorizedKeys = lib.mkOption {
            type = types.listOf types.str;
            default = [ ];
            description = "List of ssh keys for the cgit user (cgit user should own all repos)";
        };
        authorizedUsers = lib.mkOption {
            type = types.listOf types.str;
            default = [ ];
            description = "List of users that should have access to the cgit directory";
        };
        directory = mkOption {
            type = types.str;
            default = "/srv/cgit/repos";
            description = "Directory for cgit (cgit user's home directory";
        };
        description = mkOption {
            type = types.str;
            default = "Cgit instance hosted with nixos";
            description = "Description of the cgit website";
        };
        title = mkOption {
            type = types.str;
            default = "Cgit + Nix";
            description = "Title of the cgit website";
        };
        domain = mkOption {
            type = types.str;
            default = "git.example.com";
            description = "Domain to host it on";
        };
        package = mkPackageOption pkgs "cgit" { };
        extraConfig = mkOption {
            type = types.str;
            default = "";
            description = "Extra config to be appended to cgitrc";
        };
    };

    config = lib.mkIf cfg.enable {
        environment.systemPackages = [ pkgs.git cfg.package ];
        users = {
            groups.${cfg.user} = {
                members = cfg.authorizedUsers;
            };
            users.${cfg.user} = {
                createHome = true;
                homeMode = "770";
                home = cfg.directory;
                isSystemUser = true;
                shell = "${pkgs.git}/bin/git-shell";
                openssh.authorizedKeys.keys = cfg.authorizedKeys;
                group = cfg.user;
            };
        };


        services.fcgiwrap.instances.cgit = {
            socket = {
                user = cfg.user;
                group = "nginx";
                type = "unix";
                mode = "0660";
            };
            process = {
                user = cfg.user;
                group = cfg.user;
            };
        };
        
        services.nginx.enable = true;
        services.nginx.virtualHosts.${cfg.domain} = {
            locations."~* ^/static/(.+.(ico|css|png))$" = {
                extraConfig = ''
alias ${cfg.package}/cgit/$1;
'';
            };
            locations."/" = {
                extraConfig = ''
include ${pkgs.nginx}/conf/fastcgi_params;
fastcgi_param CGIT_CONFIG ${cgitrc};
fastcgi_param SCRIPT_FILENAME ${cfg.package}/cgit/cgit.cgi;
fastcgi_split_path_info ^(/?)(.+)$;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param QUERY_STRING $args;
fastcgi_param HTTP_HOST $server_name;
fastcgi_pass unix:${config.services.fcgiwrap.instances.cgit.socket.address};
        '';
            };
        };
    };
}