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