about summary refs log tree commit diff
path: root/nixpkgs/nixos/modules/services/networking/envoy.nix
blob: 6f3080d19e2b00aa08a6933ab9f780f258d464b6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
{ config, lib, pkgs, ... }:

with lib;

let
  cfg = config.services.envoy;
  format = pkgs.formats.json { };
  conf = format.generate "envoy.json" cfg.settings;
  validateConfig = file:
    pkgs.runCommand "validate-envoy-conf" { } ''
      ${pkgs.envoy}/bin/envoy --log-level error --mode validate -c "${file}"
      cp "${file}" "$out"
    '';

in

{
  options.services.envoy = {
    enable = mkEnableOption "Envoy reverse proxy";

    settings = mkOption {
      type = format.type;
      default = { };
      example = literalExpression ''
        {
          admin = {
            access_log_path = "/dev/null";
            address = {
              socket_address = {
                protocol = "TCP";
                address = "127.0.0.1";
                port_value = 9901;
              };
            };
          };
          static_resources = {
            listeners = [];
            clusters = [];
          };
        }
      '';
      description = lib.mdDoc ''
        Specify the configuration for Envoy in Nix.
      '';
    };
  };

  config = mkIf cfg.enable {
    environment.systemPackages = [ pkgs.envoy ];
    systemd.services.envoy = {
      description = "Envoy reverse proxy";
      after = [ "network-online.target" ];
      requires = [ "network-online.target" ];
      wantedBy = [ "multi-user.target" ];
      serviceConfig = {
        ExecStart = "${pkgs.envoy}/bin/envoy -c ${validateConfig conf}";
        DynamicUser = true;
        Restart = "no";
        CacheDirectory = "envoy";
        LogsDirectory = "envoy";
        AmbientCapabilities = "CAP_NET_BIND_SERVICE";
        CapabilityBoundingSet = "CAP_NET_BIND_SERVICE";
        RestrictAddressFamilies = "AF_UNIX AF_INET AF_INET6 AF_NETLINK AF_XDP";
        SystemCallArchitectures = "native";
        LockPersonality = true;
        RestrictNamespaces = true;
        RestrictRealtime = true;
        PrivateUsers = false;  # breaks CAP_NET_BIND_SERVICE
        PrivateDevices = true;
        ProtectClock = true;
        ProtectControlGroups = true;
        ProtectHome = true;
        ProtectKernelLogs = true;
        ProtectKernelModules = true;
        ProtectKernelTunables = true;
        ProtectProc = "ptraceable";
        ProtectHostname = true;
        ProtectSystem = "strict";
        UMask = "0066";
        SystemCallFilter = "~@clock @module @mount @reboot @swap @obsolete @cpu-emulation";
      };
    };
  };
}