nixpkgs/nixos/modules/services/networking/envoy.nix

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

102 lines
3.0 KiB
Nix
Raw Normal View History

2021-08-28 02:28:27 +00:00
{ config, lib, pkgs, ... }:
with lib;
let
cfg = config.services.envoy;
format = pkgs.formats.json { };
conf = format.generate "envoy.json" cfg.settings;
validateConfig = required: file:
2021-08-28 02:28:27 +00:00
pkgs.runCommand "validate-envoy-conf" { } ''
${cfg.package}/bin/envoy --log-level error --mode validate -c "${file}" ${lib.optionalString (!required) "|| true"}
2021-08-28 02:28:27 +00:00
cp "${file}" "$out"
'';
in
{
options.services.envoy = {
enable = mkEnableOption "Envoy reverse proxy";
2021-08-28 02:28:27 +00:00
package = mkPackageOption pkgs "envoy" { };
2023-02-09 11:08:52 +00:00
requireValidConfig = mkOption {
type = types.bool;
default = true;
description = ''
Whether a failure during config validation at build time is fatal.
When the config can't be checked during build time, for example when it includes
other files, disable this option.
'';
};
2021-08-28 02:28:27 +00:00
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 = ''
2021-08-28 02:28:27 +00:00
Specify the configuration for Envoy in Nix.
'';
};
};
config = mkIf cfg.enable {
2023-02-09 11:08:52 +00:00
environment.systemPackages = [ cfg.package ];
2021-08-28 02:28:27 +00:00
systemd.services.envoy = {
description = "Envoy reverse proxy";
after = [ "network-online.target" ];
requires = [ "network-online.target" ];
wantedBy = [ "multi-user.target" ];
serviceConfig = {
ExecStart = "${cfg.package}/bin/envoy -c ${validateConfig cfg.requireValidConfig conf}";
CacheDirectory = [ "envoy" ];
LogsDirectory = [ "envoy" ];
Restart = "no";
# Hardening
AmbientCapabilities = [ "CAP_NET_BIND_SERVICE" ];
CapabilityBoundingSet = [ "CAP_NET_BIND_SERVICE" ];
2023-02-09 08:54:48 +00:00
DeviceAllow = [ "" ];
DevicePolicy = "closed";
DynamicUser = true;
2021-08-28 02:28:27 +00:00
LockPersonality = true;
2023-02-09 08:54:48 +00:00
MemoryDenyWriteExecute = false; # at least wasmr needs WX permission
2021-08-28 02:28:27 +00:00
PrivateDevices = true;
PrivateUsers = false; # breaks CAP_NET_BIND_SERVICE
2023-02-09 08:54:48 +00:00
ProcSubset = "pid";
2021-08-28 02:28:27 +00:00
ProtectClock = true;
ProtectControlGroups = true;
ProtectHome = true;
ProtectHostname = true;
2021-08-28 02:28:27 +00:00
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";
2023-02-09 08:54:48 +00:00
SystemCallErrorNumber = "EPERM";
SystemCallFilter = [ "@system-service" "~@privileged" "~@resources" ];
UMask = "0066";
2021-08-28 02:28:27 +00:00
};
};
};
}