1{ config, lib, pkgs, ... }:
2
3with lib;
4
5let
6
7 cfg = config.services.statsd;
8
9 configFile = pkgs.writeText "statsd.conf" ''
10 {
11 address: "${cfg.host}",
12 port: "${toString cfg.port}",
13 mgmt_address: "${cfg.mgmt_address}",
14 mgmt_port: "${toString cfg.mgmt_port}",
15 backends: [${concatMapStringsSep "," (el: if (nixType el) == "string" then ''"./backends/${el}"'' else ''"${head el.names}"'') cfg.backends}],
16 ${optionalString (cfg.graphiteHost!=null) ''graphiteHost: "${cfg.graphiteHost}",''}
17 ${optionalString (cfg.graphitePort!=null) ''graphitePort: "${toString cfg.graphitePort}",''}
18 console: {
19 prettyprint: false
20 },
21 log: {
22 backend: "syslog"
23 },
24 automaticConfigReload: false${optionalString (cfg.extraConfig != null) ","}
25 ${cfg.extraConfig}
26 }
27 '';
28
29in
30
31{
32
33 ###### interface
34
35 options.services.statsd = {
36
37 enable = mkOption {
38 description = "Whether to enable statsd stats aggregation service";
39 default = false;
40 type = types.bool;
41 };
42
43 host = mkOption {
44 description = "Address that statsd listens on over UDP";
45 default = "127.0.0.1";
46 type = types.str;
47 };
48
49 port = mkOption {
50 description = "Port that stats listens for messages on over UDP";
51 default = 8125;
52 type = types.int;
53 };
54
55 mgmt_address = mkOption {
56 description = "Address to run management TCP interface on";
57 default = "127.0.0.1";
58 type = types.str;
59 };
60
61 mgmt_port = mkOption {
62 description = "Port to run the management TCP interface on";
63 default = 8126;
64 type = types.int;
65 };
66
67 backends = mkOption {
68 description = "List of backends statsd will use for data persistence";
69 default = ["graphite"];
70 example = ["graphite" pkgs.nodePackages."statsd-influxdb-backend"];
71 type = types.listOf (types.either types.str types.package);
72 };
73
74 graphiteHost = mkOption {
75 description = "Hostname or IP of Graphite server";
76 default = null;
77 type = types.nullOr types.str;
78 };
79
80 graphitePort = mkOption {
81 description = "Port of Graphite server (i.e. carbon-cache).";
82 default = null;
83 type = types.nullOr types.int;
84 };
85
86 extraConfig = mkOption {
87 description = "Extra configuration options for statsd";
88 default = "";
89 type = types.nullOr types.str;
90 };
91
92 };
93
94 ###### implementation
95
96 config = mkIf cfg.enable {
97
98 users.extraUsers = singleton {
99 name = "statsd";
100 uid = config.ids.uids.statsd;
101 description = "Statsd daemon user";
102 };
103
104 systemd.services.statsd = {
105 description = "Statsd Server";
106 wantedBy = [ "multi-user.target" ];
107 environment = {
108 NODE_PATH=concatMapStringsSep ":" (el: "${el}/lib/node_modules") (filter (el: (nixType el) != "string") cfg.backends);
109 };
110 serviceConfig = {
111 ExecStart = "${pkgs.nodePackages.statsd}/bin/statsd ${configFile}";
112 User = "statsd";
113 };
114 };
115
116 environment.systemPackages = [pkgs.nodePackages.statsd];
117
118 };
119
120}