1{ config, pkgs, lib, ... }:
2
3with lib;
4
5let
6 cfg = config.services.confd;
7
8 confdConfig = ''
9 backend = "${cfg.backend}"
10 confdir = "${cfg.confDir}"
11 interval = ${toString cfg.interval}
12 nodes = [ ${concatMapStringsSep "," (s: ''"${s}"'') cfg.nodes}, ]
13 prefix = "${cfg.prefix}"
14 log-level = "${cfg.logLevel}"
15 watch = ${boolToString cfg.watch}
16 '';
17
18in {
19 options.services.confd = {
20 enable = mkEnableOption "confd, a service to manage local application configuration files using templates and data from etcd/consul/redis/zookeeper";
21
22 backend = mkOption {
23 description = "Confd config storage backend to use.";
24 default = "etcd";
25 type = types.enum ["etcd" "consul" "redis" "zookeeper"];
26 };
27
28 interval = mkOption {
29 description = "Confd check interval.";
30 default = 10;
31 type = types.int;
32 };
33
34 nodes = mkOption {
35 description = "Confd list of nodes to connect to.";
36 default = [ "http://127.0.0.1:2379" ];
37 type = types.listOf types.str;
38 };
39
40 watch = mkOption {
41 description = "Confd, whether to watch etcd config for changes.";
42 default = true;
43 type = types.bool;
44 };
45
46 prefix = mkOption {
47 description = "The string to prefix to keys.";
48 default = "/";
49 type = types.path;
50 };
51
52 logLevel = mkOption {
53 description = "Confd log level.";
54 default = "info";
55 type = types.enum ["info" "debug"];
56 };
57
58 confDir = mkOption {
59 description = "The path to the confd configs.";
60 default = "/etc/confd";
61 type = types.path;
62 };
63
64 package = mkPackageOption pkgs "confd" { };
65 };
66
67 config = mkIf cfg.enable {
68 systemd.services.confd = {
69 description = "Confd Service.";
70 wantedBy = [ "multi-user.target" ];
71 after = [ "network.target" ];
72 serviceConfig = {
73 ExecStart = "${cfg.package}/bin/confd";
74 };
75 };
76
77 environment.etc = {
78 "confd/confd.toml".text = confdConfig;
79 };
80
81 environment.systemPackages = [ cfg.package ];
82
83 services.etcd.enable = mkIf (cfg.backend == "etcd") (mkDefault true);
84 };
85}