-
-
Notifications
You must be signed in to change notification settings - Fork 12.7k
/
buildkite-agent.nix
116 lines (99 loc) · 3.62 KB
/
buildkite-agent.nix
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
{ config, lib, pkgs, ... }:
with lib;
let
## isPath :: String -> Bool
isPath = x: !(isAttrs x || isList x || isFunction x || isString x || isInt x || isBool x || isNull x)
|| (isString x && builtins.substring 0 1 x == "/");
cfg = config.services.buildkite-agent;
in
{
options = {
services.buildkite-agent = {
enable = mkEnableOption "buildkite-agent";
token = mkOption {
type = types.either types.str types.path;
description = ''
The token from your Buildkite "Agents" page.
Either a literal string value, or a path to the token file.
'';
};
name = mkOption {
type = types.str;
description = ''
The name of the agent.
'';
};
meta-data = mkOption {
type = types.str;
default = "";
description = ''
Meta data for the agent.
'';
};
openssh =
{ privateKey = mkOption {
type = types.either types.str types.path;
description = ''
Private agent key.
Either a literal string value, or a path to the token file.
'';
};
publicKey = mkOption {
type = types.either types.str types.path;
description = ''
Public agent key.
Either a literal string value, or a path to the token file.
'';
};
};
};
};
config = mkIf config.services.buildkite-agent.enable {
users.extraUsers.buildkite-agent =
{ name = "buildkite-agent";
home = "/var/lib/buildkite-agent";
createHome = true;
description = "Buildkite agent user";
extraGroups = [ "keys" ];
};
environment.systemPackages = [ pkgs.buildkite-agent ];
systemd.services.buildkite-agent =
let copyOrEcho = x: target: perms:
(if isPath x
then "cp -f ${x} ${target}; "
else "echo '${x}' > ${target}; ")
+ "${pkgs.coreutils}/bin/chmod ${toString perms} ${target}; ";
catOrLiteral = x:
(if isPath x
then "$(cat ${toString x})"
else "${x}");
in
{ description = "Buildkite Agent";
wantedBy = [ "multi-user.target" ];
after = [ "network.target" ];
environment.HOME = "/var/lib/buildkite-agent";
## NB: maximum care is taken so that secrets (ssh keys and the CI token)
## don't end up in the Nix store.
preStart = ''
${pkgs.coreutils}/bin/mkdir -m 0700 -p /var/lib/buildkite-agent/.ssh
${copyOrEcho (toString cfg.openssh.privateKey) "/var/lib/buildkite-agent/.ssh/id_rsa" 600}
${copyOrEcho (toString cfg.openssh.publicKey) "/var/lib/buildkite-agent/.ssh/id_rsa.pub" 600}
cat > "/var/lib/buildkite-agent/buildkite-agent.cfg" <<EOF
token="${catOrLiteral cfg.token}"
name="${cfg.name}"
meta-data="${cfg.meta-data}"
hooks-path="${pkgs.buildkite-agent}/share/hooks"
build-path="/var/lib/buildkite-agent/builds"
bootstrap-script="${pkgs.buildkite-agent}/share/bootstrap.sh"
EOF
'';
serviceConfig =
{ ExecStart = "${pkgs.buildkite-agent}/bin/buildkite-agent start --config /var/lib/buildkite-agent/buildkite-agent.cfg";
User = "buildkite-agent";
RestartSec = 5;
Restart = "on-failure";
TimeoutSec = 10;
};
};
};
}