@@ -0,0 +1,100 @@
1+{ config, lib, pkgs, ... }:
2+
3+with lib;
4+
5+let
6+ cfg = config.services.buildkite-agent;
7+ configFile = pkgs.writeText "buildkite-agent.cfg"
8+ ''
9+ token="${cfg.token}"
10+ name="${cfg.name}"
11+ meta-data="${cfg.meta-data}"
12+ hooks-path="${pkgs.buildkite-agent}/share/hooks"
13+ build-path="/var/lib/buildkite-agent/builds"
14+ bootstrap-script="${pkgs.buildkite-agent}/share/bootstrap.sh"
15+ '';
16+in
17+
18+{
19+ options = {
20+ services.buildkite-agent = {
21+ enable = mkEnableOption "buildkite-agent";
22+
23+ token = mkOption {
24+ type = types.str;
25+ description = ''
26+ The token from your Buildkite "Agents" page.
27+ '';
28+ };
29+
30+ name = mkOption {
31+ type = types.str;
32+ description = ''
33+ The name of the agent.
34+ '';
35+ };
36+
37+ meta-data = mkOption {
38+ type = types.str;
39+ default = "";
40+ description = ''
41+ Meta data for the agent.
42+ '';
43+ };
44+
45+ openssh =
46+ { privateKey = mkOption {
47+ type = types.str;
48+ description = ''
49+ Private agent key.
50+ '';
51+ };
52+ publicKey = mkOption {
53+ type = types.str;
54+ description = ''
55+ Public agent key.
56+ '';
57+ };
58+ };
59+ };
60+ };
61+
62+ config = mkIf config.services.buildkite-agent.enable {
63+ users.extraUsers.buildkite-agent =
64+ { name = "buildkite-agent";
65+ home = "/var/lib/buildkite-agent";
66+ createHome = true;
67+ description = "Buildkite agent user";
68+ };
69+
70+ environment.systemPackages = [ pkgs.buildkite-agent ];
71+
72+ systemd.services.buildkite-agent =
73+ { description = "Buildkite Agent";
74+ wantedBy = [ "multi-user.target" ];
75+ after = [ "network.target" ];
76+ environment.HOME = "/var/lib/buildkite-agent";
77+ preStart = ''
78+ ${pkgs.coreutils}/bin/mkdir -m 0700 -p /var/lib/buildkite-agent/.ssh
79+
80+ if ! [ -f /var/lib/buildkite-agent/.ssh/id_rsa ]; then
81+ echo "${cfg.openssh.privateKey}" > /var/lib/buildkite-agent/.ssh/id_rsa
82+ ${pkgs.coreutils}/bin/chmod 600 /var/lib/buildkite-agent/.ssh/id_rsa
83+ fi
84+
85+ if ! [ -f /var/lib/buildkite-agent/.ssh/id_rsa.pub ]; then
86+ echo "${cfg.openssh.publicKey}" > /var/lib/buildkite-agent/.ssh/id_rsa.pub
87+ ${pkgs.coreutils}/bin/chmod 600 /var/lib/buildkite-agent/.ssh/id_rsa.pub
88+ fi
89+ '';
90+
91+ serviceConfig =
92+ { ExecStart = "${pkgs.buildkite-agent}/bin/buildkite-agent start --config ${configFile}";
93+ User = "buildkite-agent";
94+ RestartSec = 5;
95+ Restart = "on-failure";
96+ TimeoutSec = 10;
97+ };
98+ };
99+ };
100+}