1{ config, lib, pkgs, ... }: 2let 3 cfg = config.services.haproxy; 4 haproxyCfg = pkgs.writeText "haproxy.conf" cfg.config; 5in 6with lib; 7{ 8 options = { 9 services.haproxy = { 10 11 enable = mkOption { 12 type = types.bool; 13 default = false; 14 description = '' 15 Whether to enable HAProxy, the reliable, high performance TCP/HTTP 16 load balancer. 17 ''; 18 }; 19 20 config = mkOption { 21 type = types.nullOr types.lines; 22 default = null; 23 description = '' 24 Contents of the HAProxy configuration file, 25 <filename>haproxy.conf</filename>. 26 ''; 27 }; 28 29 }; 30 31 }; 32 33 config = mkIf cfg.enable { 34 35 assertions = [{ 36 assertion = cfg.config != null; 37 message = "You must provide services.haproxy.config."; 38 }]; 39 40 systemd.services.haproxy = { 41 description = "HAProxy"; 42 after = [ "network.target" ]; 43 wantedBy = [ "multi-user.target" ]; 44 serviceConfig = { 45 Type = "forking"; 46 PIDFile = "/run/haproxy.pid"; 47 ExecStartPre = "${pkgs.haproxy}/sbin/haproxy -c -q -f ${haproxyCfg}"; 48 ExecStart = "${pkgs.haproxy}/sbin/haproxy -D -f ${haproxyCfg} -p /run/haproxy.pid"; 49 ExecReload = "-${pkgs.bash}/bin/bash -c \"exec ${pkgs.haproxy}/sbin/haproxy -D -f ${haproxyCfg} -p /run/haproxy.pid -sf $MAINPID\""; 50 }; 51 }; 52 53 environment.systemPackages = [ pkgs.haproxy ]; 54 55 users.extraUsers.haproxy = { 56 group = "haproxy"; 57 uid = config.ids.uids.haproxy; 58 }; 59 60 users.extraGroups.haproxy.gid = config.ids.uids.haproxy; 61 }; 62}