1{
2 config,
3 pkgs,
4 lib,
5 ...
6}:
7let
8 cfg = config.services.cachix-agent;
9in
10{
11 meta.maintainers = [ lib.maintainers.domenkozar ];
12
13 options.services.cachix-agent = {
14 enable = lib.mkEnableOption "Cachix Deploy Agent: https://docs.cachix.org/deploy/";
15
16 name = lib.mkOption {
17 type = lib.types.str;
18 description = "Agent name, usually same as the hostname";
19 default = config.networking.hostName;
20 defaultText = "config.networking.hostName";
21 };
22
23 verbose = lib.mkOption {
24 type = lib.types.bool;
25 description = "Enable verbose output";
26 default = false;
27 };
28
29 profile = lib.mkOption {
30 type = lib.types.nullOr lib.types.str;
31 default = null;
32 description = "Profile name, defaults to 'system' (NixOS).";
33 };
34
35 host = lib.mkOption {
36 type = lib.types.nullOr lib.types.str;
37 default = null;
38 description = "Cachix uri to use.";
39 };
40
41 package = lib.mkPackageOption pkgs "cachix" { };
42
43 credentialsFile = lib.mkOption {
44 type = lib.types.path;
45 default = "/etc/cachix-agent.token";
46 description = ''
47 Required file that needs to contain CACHIX_AGENT_TOKEN=...
48 '';
49 };
50 };
51
52 config = lib.mkIf cfg.enable {
53 systemd.services.cachix-agent = {
54 description = "Cachix Deploy Agent";
55 wants = [ "network-online.target" ];
56 after = [ "network-online.target" ];
57 path = [ config.nix.package ];
58 wantedBy = [ "multi-user.target" ];
59
60 # Cachix requires $USER to be set
61 environment.USER = "root";
62
63 # don't stop the service if the unit disappears
64 unitConfig.X-StopOnRemoval = false;
65
66 serviceConfig = {
67 # we don't want to kill children processes as those are deployments
68 KillMode = "process";
69 Restart = "always";
70 RestartSec = 5;
71 EnvironmentFile = cfg.credentialsFile;
72 ExecStart = ''
73 ${cfg.package}/bin/cachix ${lib.optionalString cfg.verbose "--verbose"} ${
74 lib.optionalString (cfg.host != null) "--host ${cfg.host}"
75 } \
76 deploy agent ${cfg.name} ${lib.optionalString (cfg.profile != null) cfg.profile}
77 '';
78 };
79 };
80 };
81}