3
0
Fork 0
forked from mirrors/nixpkgs
nixpkgs/nixos/modules/services/networking/haproxy.nix

89 lines
2.2 KiB
Nix
Raw Normal View History

{ config, lib, pkgs, ... }:
2013-10-29 14:55:25 +00:00
let
cfg = config.services.haproxy;
haproxyCfg = pkgs.writeText "haproxy.conf" ''
global
# needed for hot-reload to work without dropping packets in multi-worker mode
stats socket /run/haproxy/haproxy.sock mode 600 expose-fd listeners level user
${cfg.config}
'';
2013-10-29 14:55:25 +00:00
in
with lib;
2013-10-29 14:55:25 +00:00
{
options = {
services.haproxy = {
enable = mkOption {
type = types.bool;
2013-10-29 14:55:25 +00:00
default = false;
description = ''
Whether to enable HAProxy, the reliable, high performance TCP/HTTP
load balancer.
'';
2013-10-29 14:55:25 +00:00
};
user = mkOption {
type = types.str;
default = "haproxy";
description = "User account under which haproxy runs.";
};
group = mkOption {
type = types.str;
default = "haproxy";
description = "Group account under which haproxy runs.";
};
2013-10-29 14:55:25 +00:00
config = mkOption {
type = types.nullOr types.lines;
default = null;
description = ''
Contents of the HAProxy configuration file,
<filename>haproxy.conf</filename>.
'';
2013-10-29 14:55:25 +00:00
};
};
};
config = mkIf cfg.enable {
assertions = [{
assertion = cfg.config != null;
message = "You must provide services.haproxy.config.";
}];
2013-10-29 14:55:25 +00:00
systemd.services.haproxy = {
description = "HAProxy";
after = [ "network.target" ];
wantedBy = [ "multi-user.target" ];
serviceConfig = {
User = cfg.user;
Group = cfg.group;
Type = "notify";
# when running the config test, don't be quiet so we can see what goes wrong
ExecStartPre = "${pkgs.haproxy}/sbin/haproxy -c -f ${haproxyCfg}";
ExecStart = "${pkgs.haproxy}/sbin/haproxy -Ws -f ${haproxyCfg}";
Restart = "on-failure";
RuntimeDirectory = "haproxy";
# needed in case we bind to port < 1024
AmbientCapabilities = "CAP_NET_BIND_SERVICE";
2013-10-29 14:55:25 +00:00
};
};
users.users = optionalAttrs (cfg.user == "haproxy") {
haproxy = {
group = cfg.group;
isSystemUser = true;
};
};
users.groups = optionalAttrs (cfg.group == "haproxy") {
haproxy = {};
};
2013-10-29 14:55:25 +00:00
};
}