1{ config, lib, pkgs, ... }:
2
3with lib;
4
5let
6 cfg = config.services.gitolite;
7 pubkeyFile = pkgs.writeText "gitolite-admin.pub" cfg.adminPubkey;
8 hooks = lib.concatMapStrings (hook: "${hook} ") cfg.commonHooks;
9in
10{
11 options = {
12 services.gitolite = {
13 enable = mkOption {
14 type = types.bool;
15 default = false;
16 description = ''
17 Enable gitolite management under the
18 <literal>gitolite</literal> user. After
19 switching to a configuration with Gitolite enabled, you can
20 then run <literal>git clone
21 gitolite@host:gitolite-admin.git</literal> to manage it further.
22 '';
23 };
24
25 dataDir = mkOption {
26 type = types.str;
27 default = "/var/lib/gitolite";
28 description = ''
29 Gitolite home directory (used to store all the repositories).
30 '';
31 };
32
33 adminPubkey = mkOption {
34 type = types.str;
35 description = ''
36 Initial administrative public key for Gitolite. This should
37 be an SSH Public Key. Note that this key will only be used
38 once, upon the first initialization of the Gitolite user.
39 The key string cannot have any line breaks in it.
40 '';
41 };
42
43 commonHooks = mkOption {
44 type = types.listOf types.path;
45 default = [];
46 description = ''
47 A list of custom git hooks that get copied to <literal>~/.gitolite/hooks/common</literal>.
48 '';
49 };
50
51 user = mkOption {
52 type = types.str;
53 default = "gitolite";
54 description = ''
55 Gitolite user account. This is the username of the gitolite endpoint.
56 '';
57 };
58 };
59 };
60
61 config = mkIf cfg.enable {
62 users.extraUsers.${cfg.user} = {
63 description = "Gitolite user";
64 home = cfg.dataDir;
65 createHome = true;
66 uid = config.ids.uids.gitolite;
67 useDefaultShell = true;
68 };
69
70 systemd.services."gitolite-init" = {
71 description = "Gitolite initialization";
72 wantedBy = [ "multi-user.target" ];
73
74 serviceConfig.User = "${cfg.user}";
75 serviceConfig.Type = "oneshot";
76 serviceConfig.RemainAfterExit = true;
77
78 path = [ pkgs.gitolite pkgs.git pkgs.perl pkgs.bash config.programs.ssh.package ];
79 script = ''
80 cd ${cfg.dataDir}
81 mkdir -p .gitolite/logs
82 if [ ! -d repositories ]; then
83 gitolite setup -pk ${pubkeyFile}
84 fi
85 if [ -n "${hooks}" ]; then
86 cp ${hooks} .gitolite/hooks/common/
87 chmod +x .gitolite/hooks/common/*
88 fi
89 gitolite setup # Upgrade if needed
90 '';
91 };
92
93 environment.systemPackages = [ pkgs.gitolite pkgs.git ];
94 };
95}