about summary refs log tree commit diff
path: root/nixos/modules/services/networking/envoy.nix
blob: f41eaa62425e2acd698348a834936f5be95b774f (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
85
{ 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 (lib.mdDoc "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}";
        CacheDirectory = [ "envoy" ];
        LogsDirectory = [ "envoy" ];
        Restart = "no";
        # Hardening
        AmbientCapabilities = [ "CAP_NET_BIND_SERVICE" ];
        CapabilityBoundingSet = [ "CAP_NET_BIND_SERVICE" ];
        DynamicUser = true;
        LockPersonality = true;
        PrivateDevices = true;
        PrivateUsers = false; # breaks CAP_NET_BIND_SERVICE
        ProtectClock = true;
        ProtectControlGroups = true;
        ProtectHome = true;
        ProtectHostname = true;
        ProtectKernelLogs = true;
        ProtectKernelModules = true;
        ProtectKernelTunables = true;
        ProtectProc = "ptraceable";
        ProtectSystem = "strict";
        RestrictAddressFamilies = [ "AF_UNIX" "AF_INET" "AF_INET6" "AF_NETLINK" "AF_XDP" ];
        RestrictNamespaces = true;
        RestrictRealtime = true;
        SystemCallArchitectures = "native";
        SystemCallFilter = [ "~@clock" "~@module" "~@mount" "~@reboot" "~@swap" "~@obsolete" "~@cpu-emulation" ];
        UMask = "0066";
      };
    };
  };
}