| 
									
										
										
										
											2014-04-14 16:26:48 +02:00
										 |  |  | { config, lib, pkgs, ... }: | 
					
						
							| 
									
										
										
										
											2013-08-21 11:43:37 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-04-14 16:26:48 +02:00
										 |  |  | with lib; | 
					
						
							| 
									
										
										
										
											2013-08-21 11:43:37 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | let | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   cfg = config.services.statsd; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-27 21:36:47 +01:00
										 |  |  |   isBuiltinBackend = name: | 
					
						
							|  |  |  |     builtins.elem name [ "graphite" "console" "repeater" ]; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-25 23:54:37 +01:00
										 |  |  |   backendsToPackages = let | 
					
						
							|  |  |  |     mkMap = list: name: | 
					
						
							|  |  |  |       if isBuiltinBackend name then list | 
					
						
							|  |  |  |       else list ++ [ pkgs.nodePackages.${name} ]; | 
					
						
							|  |  |  |   in foldl mkMap []; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-21 11:43:37 +02:00
										 |  |  |   configFile = pkgs.writeText "statsd.conf" ''
 | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2015-12-24 00:20:56 +01:00
										 |  |  |       address: "${cfg.listenAddress}", | 
					
						
							| 
									
										
										
										
											2013-08-21 11:43:37 +02:00
										 |  |  |       port: "${toString cfg.port}", | 
					
						
							| 
									
										
										
										
											2013-10-12 12:37:52 +02:00
										 |  |  |       mgmt_address: "${cfg.mgmt_address}", | 
					
						
							|  |  |  |       mgmt_port: "${toString cfg.mgmt_port}", | 
					
						
							| 
									
										
										
										
											2015-11-27 21:36:47 +01:00
										 |  |  |       backends: [${ | 
					
						
							|  |  |  |         concatMapStringsSep "," (name: | 
					
						
							|  |  |  |           if (isBuiltinBackend name) | 
					
						
							|  |  |  |           then ''"./backends/${name}"'' | 
					
						
							|  |  |  |           else ''"${name}"'' | 
					
						
							|  |  |  |         ) cfg.backends}], | 
					
						
							| 
									
										
										
										
											2014-09-11 18:11:16 +02:00
										 |  |  |       ${optionalString (cfg.graphiteHost!=null) ''graphiteHost: "${cfg.graphiteHost}",''} | 
					
						
							|  |  |  |       ${optionalString (cfg.graphitePort!=null) ''graphitePort: "${toString cfg.graphitePort}",''} | 
					
						
							|  |  |  |       console: { | 
					
						
							|  |  |  |         prettyprint: false | 
					
						
							|  |  |  |       }, | 
					
						
							|  |  |  |       log: { | 
					
						
							| 
									
										
										
										
											2017-11-25 23:54:37 +01:00
										 |  |  |         backend: "stdout" | 
					
						
							| 
									
										
										
										
											2014-09-11 18:11:16 +02:00
										 |  |  |       }, | 
					
						
							|  |  |  |       automaticConfigReload: false${optionalString (cfg.extraConfig != null) ","} | 
					
						
							| 
									
										
										
										
											2013-08-21 11:43:37 +02:00
										 |  |  |       ${cfg.extraConfig} | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   '';
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-25 23:54:37 +01:00
										 |  |  |   deps = pkgs.buildEnv { | 
					
						
							|  |  |  |     name = "statsd-runtime-deps"; | 
					
						
							|  |  |  |     pathsToLink = [ "/lib" ]; | 
					
						
							|  |  |  |     ignoreCollisions = true; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     paths = backendsToPackages cfg.backends; | 
					
						
							|  |  |  |   }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-21 11:43:37 +02:00
										 |  |  | in | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   ###### interface | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   options.services.statsd = { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-25 23:54:37 +01:00
										 |  |  |     enable = mkEnableOption "statsd"; | 
					
						
							| 
									
										
										
										
											2013-08-21 11:43:37 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-24 00:20:56 +01:00
										 |  |  |     listenAddress = mkOption { | 
					
						
							| 
									
										
										
										
											2013-08-21 11:43:37 +02:00
										 |  |  |       description = "Address that statsd listens on over UDP"; | 
					
						
							|  |  |  |       default = "127.0.0.1"; | 
					
						
							| 
									
										
										
										
											2013-10-30 11:02:04 +01:00
										 |  |  |       type = types.str; | 
					
						
							| 
									
										
										
										
											2013-08-21 11:43:37 +02:00
										 |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     port = mkOption { | 
					
						
							|  |  |  |       description = "Port that stats listens for messages on over UDP"; | 
					
						
							|  |  |  |       default = 8125; | 
					
						
							| 
									
										
										
										
											2015-06-15 18:11:32 +02:00
										 |  |  |       type = types.int; | 
					
						
							| 
									
										
										
										
											2013-08-21 11:43:37 +02:00
										 |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-12 12:37:52 +02:00
										 |  |  |     mgmt_address = mkOption { | 
					
						
							| 
									
										
										
										
											2014-12-30 03:31:03 +01:00
										 |  |  |       description = "Address to run management TCP interface on"; | 
					
						
							| 
									
										
										
										
											2013-10-12 12:37:52 +02:00
										 |  |  |       default = "127.0.0.1"; | 
					
						
							| 
									
										
										
										
											2013-10-30 11:02:04 +01:00
										 |  |  |       type = types.str; | 
					
						
							| 
									
										
										
										
											2013-10-12 12:37:52 +02:00
										 |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     mgmt_port = mkOption { | 
					
						
							|  |  |  |       description = "Port to run the management TCP interface on"; | 
					
						
							|  |  |  |       default = 8126; | 
					
						
							| 
									
										
										
										
											2015-06-15 18:11:32 +02:00
										 |  |  |       type = types.int; | 
					
						
							| 
									
										
										
										
											2013-10-12 12:37:52 +02:00
										 |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-21 11:43:37 +02:00
										 |  |  |     backends = mkOption { | 
					
						
							| 
									
										
										
										
											2014-12-30 03:31:03 +01:00
										 |  |  |       description = "List of backends statsd will use for data persistence"; | 
					
						
							| 
									
										
										
										
											2015-11-27 21:36:47 +01:00
										 |  |  |       default = []; | 
					
						
							|  |  |  |       example = [ | 
					
						
							|  |  |  |         "graphite" | 
					
						
							|  |  |  |         "console" | 
					
						
							|  |  |  |         "repeater" | 
					
						
							|  |  |  |         "statsd-librato-backend" | 
					
						
							|  |  |  |         "stackdriver-statsd-backend" | 
					
						
							|  |  |  |         "statsd-influxdb-backend" | 
					
						
							|  |  |  |       ]; | 
					
						
							|  |  |  |       type = types.listOf types.str; | 
					
						
							| 
									
										
										
										
											2013-08-21 11:43:37 +02:00
										 |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     graphiteHost = mkOption { | 
					
						
							|  |  |  |       description = "Hostname or IP of Graphite server"; | 
					
						
							| 
									
										
										
										
											2014-09-11 18:11:16 +02:00
										 |  |  |       default = null; | 
					
						
							|  |  |  |       type = types.nullOr types.str; | 
					
						
							| 
									
										
										
										
											2013-08-21 11:43:37 +02:00
										 |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     graphitePort = mkOption { | 
					
						
							| 
									
										
										
										
											2014-06-08 15:59:35 +02:00
										 |  |  |       description = "Port of Graphite server (i.e. carbon-cache)."; | 
					
						
							| 
									
										
										
										
											2014-09-11 18:11:16 +02:00
										 |  |  |       default = null; | 
					
						
							|  |  |  |       type = types.nullOr types.int; | 
					
						
							| 
									
										
										
										
											2013-08-21 11:43:37 +02:00
										 |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     extraConfig = mkOption { | 
					
						
							|  |  |  |       description = "Extra configuration options for statsd"; | 
					
						
							| 
									
										
										
										
											2014-09-11 18:11:16 +02:00
										 |  |  |       default = ""; | 
					
						
							|  |  |  |       type = types.nullOr types.str; | 
					
						
							| 
									
										
										
										
											2013-08-21 11:43:37 +02:00
										 |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   ###### implementation | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   config = mkIf cfg.enable { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-25 23:54:37 +01:00
										 |  |  |     assertions = map (backend: { | 
					
						
							|  |  |  |       assertion = !isBuiltinBackend backend -> hasAttrByPath [ backend ] pkgs.nodePackages; | 
					
						
							|  |  |  |       message = "Only builtin backends (graphite, console, repeater) or backends enumerated in `pkgs.nodePackages` are allowed!"; | 
					
						
							|  |  |  |     }) cfg.backends; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-10 17:56:35 +01:00
										 |  |  |     users.users.statsd = { | 
					
						
							| 
									
										
										
										
											2013-08-21 11:43:37 +02:00
										 |  |  |       uid = config.ids.uids.statsd; | 
					
						
							|  |  |  |       description = "Statsd daemon user"; | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     systemd.services.statsd = { | 
					
						
							|  |  |  |       description = "Statsd Server"; | 
					
						
							|  |  |  |       wantedBy = [ "multi-user.target" ]; | 
					
						
							| 
									
										
										
										
											2014-09-11 18:11:16 +02:00
										 |  |  |       environment = { | 
					
						
							| 
									
										
										
										
											2017-11-25 23:54:37 +01:00
										 |  |  |         NODE_PATH = "${deps}/lib/node_modules"; | 
					
						
							| 
									
										
										
										
											2014-09-11 18:11:16 +02:00
										 |  |  |       }; | 
					
						
							| 
									
										
										
										
											2013-08-21 11:43:37 +02:00
										 |  |  |       serviceConfig = { | 
					
						
							| 
									
										
										
										
											2015-11-27 21:36:47 +01:00
										 |  |  |         ExecStart = "${pkgs.statsd}/bin/statsd ${configFile}"; | 
					
						
							| 
									
										
										
										
											2013-08-21 11:43:37 +02:00
										 |  |  |         User = "statsd"; | 
					
						
							|  |  |  |       }; | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-27 21:36:47 +01:00
										 |  |  |     environment.systemPackages = [ pkgs.statsd ]; | 
					
						
							| 
									
										
										
										
											2013-08-21 11:43:37 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |   }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } |