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