1 { config, lib, pkgs, ... }:
3 cfg = config.services.envoy;
4 format = pkgs.formats.json { };
5 conf = format.generate "envoy.json" cfg.settings;
6 validateConfig = required: file:
7 pkgs.runCommand "validate-envoy-conf" { } ''
8 ${cfg.package}/bin/envoy --log-level error --mode validate -c "${file}" ${lib.optionalString (!required) "|| true"}
14 options.services.envoy = {
15 enable = lib.mkEnableOption "Envoy reverse proxy";
17 package = lib.mkPackageOption pkgs "envoy" { };
19 requireValidConfig = lib.mkOption {
20 type = lib.types.bool;
23 Whether a failure during config validation at build time is fatal.
24 When the config can't be checked during build time, for example when it includes
25 other files, disable this option.
29 settings = lib.mkOption {
32 example = lib.literalExpression ''
35 access_log_path = "/dev/null";
39 address = "127.0.0.1";
51 Specify the configuration for Envoy in Nix.
56 config = lib.mkIf cfg.enable {
57 environment.systemPackages = [ cfg.package ];
58 systemd.services.envoy = {
59 description = "Envoy reverse proxy";
60 after = [ "network-online.target" ];
61 requires = [ "network-online.target" ];
62 wantedBy = [ "multi-user.target" ];
64 ExecStart = "${cfg.package}/bin/envoy -c ${validateConfig cfg.requireValidConfig conf}";
65 CacheDirectory = [ "envoy" ];
66 LogsDirectory = [ "envoy" ];
69 AmbientCapabilities = [ "CAP_NET_BIND_SERVICE" ];
70 CapabilityBoundingSet = [ "CAP_NET_BIND_SERVICE" ];
72 DevicePolicy = "closed";
74 LockPersonality = true;
75 MemoryDenyWriteExecute = false; # at least wasmr needs WX permission
76 PrivateDevices = true;
77 PrivateUsers = false; # breaks CAP_NET_BIND_SERVICE
80 ProtectControlGroups = true;
82 ProtectHostname = true;
83 ProtectKernelLogs = true;
84 ProtectKernelModules = true;
85 ProtectKernelTunables = true;
86 ProtectProc = "ptraceable";
87 ProtectSystem = "strict";
88 RestrictAddressFamilies = [ "AF_UNIX" "AF_INET" "AF_INET6" "AF_NETLINK" "AF_XDP" ];
89 RestrictNamespaces = true;
90 RestrictRealtime = true;
91 SystemCallArchitectures = "native";
92 SystemCallErrorNumber = "EPERM";
93 SystemCallFilter = [ "@system-service" "~@privileged" "~@resources" ];