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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
|
{
pkgs,
lib,
config,
...
}:
let
cfg = config.services.ugit;
pkg = pkgs.callPackage ./pkg.nix { inherit pkgs; };
yamlFormat = pkgs.formats.yaml { };
instanceOptions =
{ name, config, ... }:
let
inherit (lib) mkEnableOption mkOption types;
in
{
options = {
enable = mkEnableOption "Enable ugit";
package = mkOption {
type = types.package;
description = "ugit package to use";
default = pkg;
};
homeDir = mkOption {
type = types.str;
description = "ugit home directory";
default = "/var/lib/${name}";
};
repoDir = mkOption {
type = types.str;
description = "where ugit stores repositories";
default = "/var/lib/${name}/repos";
};
authorizedKeys = mkOption {
type = types.listOf types.str;
description = "list of keys to use for authorized_keys";
default = [ ];
};
authorizedKeysFile = mkOption {
type = types.str;
description = "path to authorized_keys file ugit uses for auth";
default = "/var/lib/${name}/authorized_keys";
};
hostKeyFile = mkOption {
type = types.str;
description = "path to host key file (will be created if it doesn't exist)";
default = "/var/lib/${name}/ugit_ed25519";
};
config = mkOption {
type = types.attrs;
default = { };
description = "config.yaml contents";
};
user = mkOption {
type = types.str;
default = "ugit-${name}";
description = "User account under which ugit runs";
};
group = mkOption {
type = types.str;
default = "ugit-${name}";
description = "Group account under which ugit runs";
};
hooks = mkOption {
type = types.listOf (
types.submodule {
options = {
name = mkOption {
type = types.str;
description = "Hook name";
};
content = mkOption {
type = types.str;
description = "Hook contents";
};
};
}
);
description = "A list of pre-receive hooks to run";
default = [ ];
};
};
};
in
{
options = {
services.ugit = lib.mkOption {
type = lib.types.attrsOf (lib.types.submodule instanceOptions);
default = { };
description = "Attribute set of ugit instances";
};
};
config = lib.mkIf (cfg != { }) {
users.users = lib.mapAttrs' (
name: instanceCfg:
lib.nameValuePair instanceCfg.user {
home = instanceCfg.homeDir;
createHome = true;
group = instanceCfg.group;
isSystemUser = true;
isNormalUser = false;
description = "user for ugit ${name} service";
}
) (lib.filterAttrs (name: instanceCfg: instanceCfg.enable) cfg);
users.groups = lib.mapAttrs' (name: instanceCfg: lib.nameValuePair instanceCfg.group { }) (
lib.filterAttrs (name: instanceCfg: instanceCfg.enable) cfg
);
systemd.services = lib.foldl' (
acc: name:
let
instanceCfg = cfg.${name};
in
lib.recursiveUpdate acc (
lib.optionalAttrs instanceCfg.enable {
"ugit-${name}" = {
enable = true;
description = "ugit instance ${name}";
wantedBy = [ "multi-user.target" ];
after = [ "network.target" ];
path = [
instanceCfg.package
pkgs.git
pkgs.bash
];
serviceConfig = {
User = instanceCfg.user;
Group = instanceCfg.group;
Restart = "always";
RestartSec = "15";
WorkingDirectory = instanceCfg.homeDir;
ExecStart =
let
configFile = pkgs.writeText "ugit-${name}.yaml" (
builtins.readFile (yamlFormat.generate "ugit-${name}-yaml" instanceCfg.config)
);
authorizedKeysFile = pkgs.writeText "ugit_${name}_keys" (
builtins.concatStringsSep "\n" instanceCfg.authorizedKeys
);
authorizedKeysPath =
if (builtins.length instanceCfg.authorizedKeys) > 0 then
authorizedKeysFile
else
instanceCfg.authorizedKeysFile;
args = [
"--config=${configFile}"
"--repo-dir=${instanceCfg.repoDir}"
"--ssh.authorized-keys=${authorizedKeysPath}"
"--ssh.host-key=${instanceCfg.hostKeyFile}"
];
in
"${instanceCfg.package}/bin/ugitd ${builtins.concatStringsSep " " args}";
};
};
"ugit-${name}-hooks" = {
description = "Setup hooks for ugit instance ${name}";
wantedBy = [ "multi-user.target" ];
after = [ "ugit-${name}.service" ];
requires = [ "ugit-${name}.service" ];
serviceConfig = {
Type = "oneshot";
RemainAfterExit = true;
User = instanceCfg.user;
Group = instanceCfg.group;
ExecStart =
let
hookDir = "${instanceCfg.repoDir}/hooks/pre-receive.d";
mkHookScript =
hook:
let
script = pkgs.writeShellScript "ugit-${name}-${hook.name}" hook.content;
in
''
mkdir -p ${hookDir}
ln -sf ${script} ${hookDir}/${hook.name}
'';
in
pkgs.writeShellScript "ugit-${name}-hooks-setup" ''
${builtins.concatStringsSep "\n" (map mkHookScript instanceCfg.hooks)}
'';
};
};
}
)
) { } (builtins.attrNames cfg);
systemd.tmpfiles.settings = lib.mapAttrs' (
name: instanceCfg:
lib.nameValuePair "ugit-${name}" (
builtins.listToAttrs (
map (
hook:
let
script = pkgs.writeShellScript hook.name hook.content;
path = "${instanceCfg.repoDir}/hooks/pre-receive.d/${hook.name}";
in
{
name = path;
value = {
"L" = {
argument = "${script}";
};
};
}
) instanceCfg.hooks
)
)
) (lib.filterAttrs (name: instanceCfg: instanceCfg.enable) cfg);
};
}
|