1 { config, lib, pkgs, ... }:
6 format = pkgs.formats.json { };
7 commonOptions = { pkgName, flavour ? pkgName }: mkOption {
10 Attribute set of ${flavour} instances.
11 Creates independent `${flavour}-''${name}.service` systemd units for each instance defined here.
13 type = with types; attrsOf (submodule ({ name, ... }: {
15 enable = mkEnableOption "this ${flavour} instance" // { default = true; };
17 package = mkPackageOption pkgs pkgName { };
23 User under which this instance runs.
31 Group under which this instance runs.
36 type = types.submodule {
37 freeformType = format.type;
41 default = "/run/${flavour}/${name}.pid";
44 Path to use for the pid file.
50 type = with types; listOf (attrsOf anything);
53 if flavour == "vault-agent"
54 then "https://developer.hashicorp.com/vault/docs/agent/template"
55 else "https://github.com/hashicorp/consul-template/blob/main/docs/configuration.md#templates";
57 Template section of ${flavour}.
58 Refer to <${upstreamDocs}> for supported values.
68 if flavour == "vault-agent"
69 then "https://developer.hashicorp.com/vault/docs/agent#configuration-file-options"
70 else "https://github.com/hashicorp/consul-template/blob/main/docs/configuration.md#configuration-file";
72 Free-form settings written directly to the `config.json` file.
73 Refer to <${upstreamDocs}> for supported values.
76 Resulting format is JSON not HCL.
77 Refer to <https://www.hcl2json.com/> if you are unsure how to convert HCL options to JSON.
85 createAgentInstance = { instance, name, flavour }:
87 configFile = format.generate "${name}.json" instance.settings;
89 mkIf (instance.enable) {
90 description = "${flavour} daemon - ${name}";
91 wantedBy = [ "multi-user.target" ];
92 after = [ "network.target" ];
93 path = [ pkgs.getent ];
94 startLimitIntervalSec = 60;
98 Group = instance.group;
99 RuntimeDirectory = flavour;
100 ExecStart = "${getExe instance.package} ${optionalString ((getName instance.package) == "vault") "agent"} -config ${configFile}";
101 ExecReload = "${pkgs.coreutils}/bin/kill -SIGHUP $MAINPID";
102 KillSignal = "SIGINT";
103 TimeoutStopSec = "30s";
104 Restart = "on-failure";
110 services.consul-template.instances = commonOptions { pkgName = "consul-template"; };
111 services.vault-agent.instances = commonOptions { pkgName = "vault"; flavour = "vault-agent"; };
114 config = mkMerge (map
116 let cfg = config.services.${flavour}; in
117 mkIf (cfg.instances != { }) {
118 systemd.services = mapAttrs'
119 (name: instance: nameValuePair "${flavour}-${name}" (createAgentInstance { inherit name instance flavour; }))
122 [ "consul-template" "vault-agent" ]);
124 meta.maintainers = with maintainers; [ emilylange tcheronneau ];