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

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

327 lines
11 KiB
Nix
Raw Normal View History

2021-05-18 01:19:12 +00:00
{ config, lib, pkgs, ... }:
let
cfg = config.services.cloudflared;
originRequest = {
connectTimeout = lib.mkOption {
type = with lib.types; nullOr str;
2021-05-18 01:19:12 +00:00
default = null;
example = "30s";
description = ''
Timeout for establishing a new TCP connection to your origin server. This excludes the time taken to establish TLS, which is controlled by [tlsTimeout](https://developers.cloudflare.com/cloudflare-one/connections/connect-apps/configuration/local-management/ingress/#tlstimeout).
2021-05-18 01:19:12 +00:00
'';
};
tlsTimeout = lib.mkOption {
type = with lib.types; nullOr str;
2021-05-18 01:19:12 +00:00
default = null;
example = "10s";
description = ''
Timeout for completing a TLS handshake to your origin server, if you have chosen to connect Tunnel to an HTTPS server.
'';
};
tcpKeepAlive = lib.mkOption {
type = with lib.types; nullOr str;
2021-05-18 01:19:12 +00:00
default = null;
example = "30s";
description = ''
The timeout after which a TCP keepalive packet is sent on a connection between Tunnel and the origin server.
'';
};
noHappyEyeballs = lib.mkOption {
type = with lib.types; nullOr bool;
2021-05-18 01:19:12 +00:00
default = null;
example = false;
description = ''
Disable the happy eyeballs algorithm for IPv4/IPv6 fallback if your local network has misconfigured one of the protocols.
'';
};
keepAliveConnections = lib.mkOption {
type = with lib.types; nullOr int;
2021-05-18 01:19:12 +00:00
default = null;
example = 100;
description = ''
Maximum number of idle keepalive connections between Tunnel and your origin. This does not restrict the total number of concurrent connections.
'';
};
keepAliveTimeout = lib.mkOption {
type = with lib.types; nullOr str;
2021-05-18 01:19:12 +00:00
default = null;
example = "1m30s";
description = ''
Timeout after which an idle keepalive connection can be discarded.
'';
};
httpHostHeader = lib.mkOption {
type = with lib.types; nullOr str;
2021-05-18 01:19:12 +00:00
default = null;
example = "";
description = ''
Sets the HTTP `Host` header on requests sent to the local service.
'';
};
originServerName = lib.mkOption {
type = with lib.types; nullOr str;
2021-05-18 01:19:12 +00:00
default = null;
example = "";
description = ''
Hostname that `cloudflared` should expect from your origin server certificate.
'';
};
caPool = lib.mkOption {
type = with lib.types; nullOr (either str path);
2021-05-18 01:19:12 +00:00
default = null;
example = "";
description = ''
Path to the certificate authority (CA) for the certificate of your origin. This option should be used only if your certificate is not signed by Cloudflare.
'';
};
noTLSVerify = lib.mkOption {
type = with lib.types; nullOr bool;
2021-05-18 01:19:12 +00:00
default = null;
example = false;
description = ''
Disables TLS verification of the certificate presented by your origin. Will allow any certificate from the origin to be accepted.
'';
};
disableChunkedEncoding = lib.mkOption {
type = with lib.types; nullOr bool;
2021-05-18 01:19:12 +00:00
default = null;
example = false;
description = ''
Disables chunked transfer encoding. Useful if you are running a WSGI server.
'';
};
proxyAddress = lib.mkOption {
type = with lib.types; nullOr str;
2021-05-18 01:19:12 +00:00
default = null;
example = "127.0.0.1";
description = ''
`cloudflared` starts a proxy server to translate HTTP traffic into TCP when proxying, for example, SSH or RDP. This configures the listen address for that proxy.
'';
};
proxyPort = lib.mkOption {
type = with lib.types; nullOr int;
2021-05-18 01:19:12 +00:00
default = null;
example = 0;
description = ''
`cloudflared` starts a proxy server to translate HTTP traffic into TCP when proxying, for example, SSH or RDP. This configures the listen port for that proxy. If set to zero, an unused port will randomly be chosen.
'';
};
proxyType = lib.mkOption {
type = with lib.types; nullOr (enum [ "" "socks" ]);
2021-05-18 01:19:12 +00:00
default = null;
example = "";
description = ''
`cloudflared` starts a proxy server to translate HTTP traffic into TCP when proxying, for example, SSH or RDP. This configures what type of proxy will be started. Valid options are:
- `""` for the regular proxy
- `"socks"` for a SOCKS5 proxy. Refer to the [tutorial on connecting through Cloudflare Access using kubectl](https://developers.cloudflare.com/cloudflare-one/tutorials/kubectl/) for more information.
2021-05-18 01:19:12 +00:00
'';
};
};
in
{
options.services.cloudflared = {
enable = lib.mkEnableOption "Cloudflare Tunnel client daemon (formerly Argo Tunnel)";
2021-05-18 01:19:12 +00:00
user = lib.mkOption {
type = lib.types.str;
2021-05-18 01:19:12 +00:00
default = "cloudflared";
description = "User account under which Cloudflared runs.";
};
group = lib.mkOption {
type = lib.types.str;
2021-05-18 01:19:12 +00:00
default = "cloudflared";
description = "Group under which cloudflared runs.";
};
package = lib.mkPackageOption pkgs "cloudflared" { };
2021-05-18 01:19:12 +00:00
tunnels = lib.mkOption {
2021-05-18 01:19:12 +00:00
description = ''
Cloudflare tunnels.
'';
type = lib.types.attrsOf (lib.types.submodule ({ name, ... }: {
2021-05-18 01:19:12 +00:00
options = {
inherit originRequest;
credentialsFile = lib.mkOption {
type = lib.types.str;
2021-05-18 01:19:12 +00:00
description = ''
Credential file.
See [Credentials file](https://developers.cloudflare.com/cloudflare-one/connections/connect-apps/install-and-setup/tunnel-useful-terms/#credentials-file).
2021-05-18 01:19:12 +00:00
'';
};
warp-routing = {
enabled = lib.mkOption {
type = with lib.types; nullOr bool;
2021-05-18 01:19:12 +00:00
default = null;
description = ''
Enable warp routing.
See [Connect from WARP to a private network on Cloudflare using Cloudflare Tunnel](https://developers.cloudflare.com/cloudflare-one/tutorials/warp-to-tunnel/).
2021-05-18 01:19:12 +00:00
'';
};
};
default = lib.mkOption {
type = lib.types.str;
2021-05-18 01:19:12 +00:00
description = ''
Catch-all service if no ingress matches.
See `service`.
'';
example = "http_status:404";
};
ingress = lib.mkOption {
type = with lib.types; attrsOf (either str (submodule ({ hostname, ... }: {
2021-05-18 01:19:12 +00:00
options = {
inherit originRequest;
service = lib.mkOption {
type = with lib.types; nullOr str;
2021-05-18 01:19:12 +00:00
default = null;
description = ''
Service to pass the traffic.
See [Supported protocols](https://developers.cloudflare.com/cloudflare-one/connections/connect-apps/configuration/local-management/ingress/#supported-protocols).
2021-05-18 01:19:12 +00:00
'';
example = "http://localhost:80, tcp://localhost:8000, unix:/home/production/echo.sock, hello_world or http_status:404";
};
path = lib.mkOption {
type = with lib.types; nullOr str;
2021-05-18 01:19:12 +00:00
default = null;
description = ''
Path filter.
If not specified, all paths will be matched.
'';
example = "/*.(jpg|png|css|js)";
};
};
})));
default = { };
description = ''
Ingress rules.
See [Ingress rules](https://developers.cloudflare.com/cloudflare-one/connections/connect-apps/configuration/local-management/ingress/).
2021-05-18 01:19:12 +00:00
'';
example = {
"*.domain.com" = "http://localhost:80";
"*.anotherone.com" = "http://localhost:80";
};
};
};
}));
default = { };
example = {
"00000000-0000-0000-0000-000000000000" = {
credentialsFile = "/tmp/test";
ingress = {
"*.domain1.com" = {
service = "http://localhost:80";
};
};
default = "http_status:404";
};
};
};
};
config = lib.mkIf cfg.enable {
2021-05-18 01:19:12 +00:00
systemd.targets =
lib.mapAttrs'
2021-05-18 01:19:12 +00:00
(name: tunnel:
lib.nameValuePair "cloudflared-tunnel-${name}" {
description = "Cloudflare tunnel '${name}' target";
2021-05-18 01:19:12 +00:00
requires = [ "cloudflared-tunnel-${name}.service" ];
after = [ "cloudflared-tunnel-${name}.service" ];
unitConfig.StopWhenUnneeded = true;
}
2021-05-18 01:19:12 +00:00
)
config.services.cloudflared.tunnels;
systemd.services =
lib.mapAttrs'
2021-05-18 01:19:12 +00:00
(name: tunnel:
let
filterConfig = lib.attrsets.filterAttrsRecursive (_: v: ! builtins.elem v [ null [ ] { } ]);
filterIngressSet = lib.filterAttrs (_: v: builtins.typeOf v == "set");
filterIngressStr = lib.filterAttrs (_: v: builtins.typeOf v == "string");
2021-05-18 01:19:12 +00:00
ingressesSet = filterIngressSet tunnel.ingress;
ingressesStr = filterIngressStr tunnel.ingress;
fullConfig = lib.filterConfig {
2021-05-18 01:19:12 +00:00
tunnel = name;
"credentials-file" = tunnel.credentialsFile;
warp-routing = lib.filterConfig tunnel.warp-routing;
originRequest = lib.filterConfig tunnel.originRequest;
2021-05-18 01:19:12 +00:00
ingress =
(map
(key: {
hostname = key;
} // lib.getAttr key (filterConfig (filterConfig ingressesSet)))
(lib.attrNames ingressesSet))
2021-05-18 01:19:12 +00:00
++
(map
(key: {
hostname = key;
service = lib.getAttr key ingressesStr;
2021-05-18 01:19:12 +00:00
})
(lib.attrNames ingressesStr))
2021-05-18 01:19:12 +00:00
++ [{ service = tunnel.default; }];
};
2021-05-18 01:19:12 +00:00
mkConfigFile = pkgs.writeText "cloudflared.yml" (builtins.toJSON fullConfig);
in
lib.nameValuePair "cloudflared-tunnel-${name}" ({
after = [ "network.target" "network-online.target" ];
wants = [ "network.target" "network-online.target" ];
2021-05-18 01:19:12 +00:00
wantedBy = [ "multi-user.target" ];
serviceConfig = {
User = cfg.user;
Group = cfg.group;
ExecStart = "${cfg.package}/bin/cloudflared tunnel --config=${mkConfigFile} --no-autoupdate run";
Restart = "on-failure";
2021-05-18 01:19:12 +00:00
};
})
)
config.services.cloudflared.tunnels;
users.users = lib.mkIf (cfg.user == "cloudflared") {
2021-05-18 01:19:12 +00:00
cloudflared = {
group = cfg.group;
isSystemUser = true;
};
};
users.groups = lib.mkIf (cfg.group == "cloudflared") {
2021-05-18 01:19:12 +00:00
cloudflared = { };
};
};
meta.maintainers = with lib.maintainers; [ bbigras anpin ];
2021-05-18 01:19:12 +00:00
}