1
Fork 0
satellite/modules/nixos/cloudflared.nix

69 lines
1.9 KiB
Nix
Raw Normal View History

2024-05-09 15:20:03 +02:00
{ config, lib, ... }:
let cfg = config.satellite.cloudflared;
in
{
options.satellite.cloudflared = {
tunnel = lib.mkOption {
type = lib.types.str;
2024-07-06 19:32:14 +02:00
description = "Cloudflare tunnel id to use for the `satellite.cloudflared.at` helper";
2024-05-09 15:20:03 +02:00
};
2024-07-08 03:06:27 +02:00
domain = lib.mkOption {
description = "Root domain to use as a default for configurations.";
type = lib.types.str;
default = config.satellite.dns.domain;
};
2024-07-06 19:32:14 +02:00
at = lib.mkOption {
2024-05-11 01:09:43 +02:00
description = "List of hosts to set up ingress rules for";
default = { };
2024-07-08 03:06:27 +02:00
type = lib.types.attrsOf (lib.types.submodule ({ name, config, ... }: {
2024-05-11 01:09:43 +02:00
options = {
2024-07-08 03:06:27 +02:00
subdomain = lib.mkOption {
description = ''
Subdomain to use for host generation.
Only required if `host` is not set manually.
'';
type = lib.types.str;
default = name;
};
2024-05-11 01:09:43 +02:00
port = lib.mkOption {
description = "Localhost port to point the tunnel at";
2024-07-08 03:06:27 +02:00
type = lib.types.port;
2024-05-11 01:09:43 +02:00
};
2024-05-09 15:20:03 +02:00
2024-05-11 01:09:43 +02:00
host = lib.mkOption {
description = "Host to direct traffic from";
2024-07-08 03:06:27 +02:00
type = lib.types.str;
default = "${config.subdomain}.${cfg.domain}";
};
url = lib.mkOption {
description = "External https url used to access this host";
type = lib.types.str;
2024-05-11 01:09:43 +02:00
};
};
2024-07-08 03:06:27 +02:00
config.url = "https://${config.host}";
2024-05-11 01:09:43 +02:00
}));
2024-05-09 15:20:03 +02:00
};
};
2024-05-11 01:09:43 +02:00
config.services.cloudflared.tunnels.${cfg.tunnel}.ingress = lib.attrsets.mapAttrs'
2024-07-08 03:11:21 +02:00
(_: { port, host, ... }: {
2024-05-11 01:09:43 +02:00
name = host;
value = "http://localhost:${toString port}";
})
2024-07-06 19:32:14 +02:00
cfg.at;
2024-07-08 03:06:27 +02:00
config.satellite.dns.records =
let mkDnsRecord = { subdomain, ... }: {
type = "CNAME";
at = subdomain;
zone = cfg.domain;
value = "${cfg.tunnel}.cfargotunnel.com.";
};
in lib.attrsets.mapAttrsToList (_: mkDnsRecord) cfg.at;
2024-05-09 15:20:03 +02:00
}