mirror of
https://github.com/SebastianStork/nixos-config.git
synced 2026-01-21 18:41:34 +01:00
Add caddy module with tailscale integration
This commit is contained in:
parent
1f4b3e734b
commit
e909dcd866
6 changed files with 164 additions and 147 deletions
|
|
@ -1,8 +1,4 @@
|
||||||
{ config, ... }:
|
{ config, ... }:
|
||||||
let
|
|
||||||
tsDomain = config.custom.services.tailscale.domain;
|
|
||||||
portOf = service: config.custom.services.${service}.port;
|
|
||||||
in
|
|
||||||
{
|
{
|
||||||
system.stateVersion = "24.11";
|
system.stateVersion = "24.11";
|
||||||
|
|
||||||
|
|
@ -20,28 +16,6 @@ in
|
||||||
isFunnel = true;
|
isFunnel = true;
|
||||||
target = toString ./hedgedoc-redirect.html;
|
target = toString ./hedgedoc-redirect.html;
|
||||||
};
|
};
|
||||||
|
|
||||||
caddyServe = {
|
|
||||||
nextcloud = {
|
|
||||||
subdomain = "cloud";
|
|
||||||
port = portOf "nextcloud";
|
|
||||||
};
|
|
||||||
actualbudget = {
|
|
||||||
subdomain = "budget";
|
|
||||||
port = portOf "actualbudget";
|
|
||||||
};
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
||||||
nextcloud = {
|
|
||||||
enable = true;
|
|
||||||
domain = "cloud.${tsDomain}";
|
|
||||||
backups.enable = true;
|
|
||||||
};
|
|
||||||
actualbudget = {
|
|
||||||
enable = true;
|
|
||||||
domain = "budget.${tsDomain}";
|
|
||||||
backups.enable = true;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
syncthing = {
|
syncthing = {
|
||||||
|
|
@ -50,6 +24,26 @@ in
|
||||||
isServer = true;
|
isServer = true;
|
||||||
backups.enable = true;
|
backups.enable = true;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
nextcloud = {
|
||||||
|
enable = true;
|
||||||
|
domain = "cloud.${config.custom.services.tailscale.domain}";
|
||||||
|
backups.enable = true;
|
||||||
|
};
|
||||||
|
actualbudget = {
|
||||||
|
enable = true;
|
||||||
|
domain = "budget.${config.custom.services.tailscale.domain}";
|
||||||
|
backups.enable = true;
|
||||||
|
};
|
||||||
|
|
||||||
|
caddy.virtualHosts = {
|
||||||
|
nextcloud = {
|
||||||
|
inherit (config.custom.services.nextcloud) domain port;
|
||||||
|
};
|
||||||
|
actualbudget = {
|
||||||
|
inherit (config.custom.services.actualbudget) domain port;
|
||||||
|
};
|
||||||
|
};
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -30,23 +30,15 @@
|
||||||
enable = true;
|
enable = true;
|
||||||
domain = "git.sstork.dev";
|
domain = "git.sstork.dev";
|
||||||
};
|
};
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
||||||
services.caddy = {
|
caddy.virtualHosts = {
|
||||||
enable = true;
|
hedgedoc = {
|
||||||
virtualHosts = {
|
inherit (config.custom.services.hedgedoc) domain port;
|
||||||
${config.custom.services.hedgedoc.domain}.extraConfig = ''
|
};
|
||||||
reverse_proxy localhost:${toString config.custom.services.hedgedoc.port}
|
forgejo = {
|
||||||
'';
|
inherit (config.custom.services.forgejo) domain port;
|
||||||
${config.custom.services.forgejo.domain}.extraConfig = ''
|
};
|
||||||
reverse_proxy localhost:${toString config.custom.services.forgejo.port}
|
};
|
||||||
'';
|
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
networking.firewall.allowedTCPPorts = [
|
|
||||||
80
|
|
||||||
443
|
|
||||||
];
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
89
modules/system/services/caddy.nix
Normal file
89
modules/system/services/caddy.nix
Normal file
|
|
@ -0,0 +1,89 @@
|
||||||
|
{
|
||||||
|
config,
|
||||||
|
pkgs,
|
||||||
|
lib,
|
||||||
|
...
|
||||||
|
}:
|
||||||
|
let
|
||||||
|
caddyWithTailscale = pkgs.caddy.withPlugins {
|
||||||
|
plugins = [ "github.com/tailscale/caddy-tailscale@v0.0.0-20250207163903-69a970c84556" ];
|
||||||
|
hash = "sha256-wt3+xCsT83RpPySbL7dKVwgqjKw06qzrP2Em+SxEPto=";
|
||||||
|
};
|
||||||
|
|
||||||
|
allVirtualHosts =
|
||||||
|
config.custom.services.caddy.virtualHosts |> lib.filterAttrs (_: value: value.enable);
|
||||||
|
|
||||||
|
isTailscaleDomain = domain: domain |> lib.hasSuffix config.custom.services.tailscale.domain;
|
||||||
|
|
||||||
|
tailscaleHostsExist = lib.any (v: isTailscaleDomain v.domain) (lib.attrValues allVirtualHosts);
|
||||||
|
nonTailscaleHostsExist = lib.any (v: !isTailscaleDomain v.domain) (lib.attrValues allVirtualHosts);
|
||||||
|
|
||||||
|
getSubdomain = domain: domain |> lib.splitString "." |> lib.head;
|
||||||
|
in
|
||||||
|
{
|
||||||
|
options.custom.services.caddy.virtualHosts = lib.mkOption {
|
||||||
|
type = lib.types.attrsOf (
|
||||||
|
lib.types.submodule (
|
||||||
|
{ name, ... }:
|
||||||
|
{
|
||||||
|
options = {
|
||||||
|
enable = lib.mkOption {
|
||||||
|
type = lib.types.bool;
|
||||||
|
default = true;
|
||||||
|
};
|
||||||
|
domain = lib.mkOption {
|
||||||
|
type = lib.types.nonEmptyStr;
|
||||||
|
default = name;
|
||||||
|
};
|
||||||
|
port = lib.mkOption {
|
||||||
|
type = lib.types.port;
|
||||||
|
default = null;
|
||||||
|
};
|
||||||
|
};
|
||||||
|
}
|
||||||
|
)
|
||||||
|
);
|
||||||
|
default = { };
|
||||||
|
};
|
||||||
|
|
||||||
|
config = lib.mkIf (allVirtualHosts != { }) (
|
||||||
|
lib.mkMerge [
|
||||||
|
{
|
||||||
|
services.caddy = {
|
||||||
|
enable = true;
|
||||||
|
virtualHosts = lib.mapAttrs' (
|
||||||
|
_: v:
|
||||||
|
lib.nameValuePair v.domain {
|
||||||
|
extraConfig = lib.concatStrings [
|
||||||
|
(lib.optionalString (isTailscaleDomain v.domain) ''
|
||||||
|
bind tailscale/${getSubdomain v.domain}
|
||||||
|
tailscale_auth
|
||||||
|
'')
|
||||||
|
"reverse_proxy localhost:${toString v.port}"
|
||||||
|
];
|
||||||
|
}
|
||||||
|
) allVirtualHosts;
|
||||||
|
};
|
||||||
|
|
||||||
|
networking.firewall.allowedTCPPorts = lib.mkIf nonTailscaleHostsExist [
|
||||||
|
80
|
||||||
|
443
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
(lib.mkIf tailscaleHostsExist {
|
||||||
|
sops.secrets."service-tailscale-auth-key".owner = config.services.caddy.user;
|
||||||
|
|
||||||
|
services.caddy = {
|
||||||
|
package = caddyWithTailscale;
|
||||||
|
enableReload = false;
|
||||||
|
globalConfig = ''
|
||||||
|
tailscale {
|
||||||
|
auth_key {file.${config.sops.secrets."service-tailscale-auth-key".path}}
|
||||||
|
}
|
||||||
|
'';
|
||||||
|
};
|
||||||
|
})
|
||||||
|
]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
@ -1,66 +0,0 @@
|
||||||
{
|
|
||||||
config,
|
|
||||||
pkgs,
|
|
||||||
lib,
|
|
||||||
...
|
|
||||||
}:
|
|
||||||
let
|
|
||||||
nodes = config.custom.services.tailscale.caddyServe |> lib.filterAttrs (_: value: value.enable);
|
|
||||||
|
|
||||||
caddy-tailscale = pkgs.caddy.withPlugins {
|
|
||||||
plugins = [ "github.com/tailscale/caddy-tailscale@v0.0.0-20250207163903-69a970c84556" ];
|
|
||||||
hash = "sha256-wt3+xCsT83RpPySbL7dKVwgqjKw06qzrP2Em+SxEPto=";
|
|
||||||
};
|
|
||||||
in
|
|
||||||
{
|
|
||||||
options.custom.services.tailscale.caddyServe = lib.mkOption {
|
|
||||||
type = lib.types.attrsOf (
|
|
||||||
lib.types.submodule (
|
|
||||||
{ name, ... }:
|
|
||||||
{
|
|
||||||
options = {
|
|
||||||
enable = lib.mkEnableOption "" // {
|
|
||||||
default = true;
|
|
||||||
};
|
|
||||||
subdomain = lib.mkOption {
|
|
||||||
type = lib.types.nonEmptyStr;
|
|
||||||
default = name;
|
|
||||||
};
|
|
||||||
port = lib.mkOption {
|
|
||||||
type = lib.types.nullOr lib.types.port;
|
|
||||||
default = null;
|
|
||||||
};
|
|
||||||
};
|
|
||||||
}
|
|
||||||
)
|
|
||||||
);
|
|
||||||
default = { };
|
|
||||||
};
|
|
||||||
|
|
||||||
config = lib.mkIf (nodes != { }) {
|
|
||||||
sops.secrets."service-tailscale-auth-key".owner = config.services.caddy.user;
|
|
||||||
|
|
||||||
services.caddy = {
|
|
||||||
enable = true;
|
|
||||||
package = caddy-tailscale;
|
|
||||||
enableReload = false;
|
|
||||||
|
|
||||||
globalConfig = ''
|
|
||||||
tailscale {
|
|
||||||
auth_key {file.${config.sops.secrets."service-tailscale-auth-key".path}}
|
|
||||||
}
|
|
||||||
'';
|
|
||||||
|
|
||||||
virtualHosts = lib.mapAttrs' (
|
|
||||||
_: value:
|
|
||||||
lib.nameValuePair "https://${value.subdomain}.${config.custom.services.tailscale.domain}" {
|
|
||||||
extraConfig = ''
|
|
||||||
bind tailscale/${value.subdomain}
|
|
||||||
tailscale_auth
|
|
||||||
reverse_proxy localhost:${toString value.port}
|
|
||||||
'';
|
|
||||||
}
|
|
||||||
) nodes;
|
|
||||||
};
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
@ -1,9 +1,4 @@
|
||||||
{
|
{ config, lib, ... }:
|
||||||
config,
|
|
||||||
pkgs,
|
|
||||||
lib,
|
|
||||||
...
|
|
||||||
}:
|
|
||||||
let
|
let
|
||||||
cfg = config.custom.services.tailscale;
|
cfg = config.custom.services.tailscale;
|
||||||
in
|
in
|
||||||
|
|
@ -16,13 +11,6 @@ in
|
||||||
};
|
};
|
||||||
ssh.enable = lib.mkEnableOption "";
|
ssh.enable = lib.mkEnableOption "";
|
||||||
exitNode.enable = lib.mkEnableOption "";
|
exitNode.enable = lib.mkEnableOption "";
|
||||||
serve = {
|
|
||||||
isFunnel = lib.mkEnableOption "";
|
|
||||||
target = lib.mkOption {
|
|
||||||
type = lib.types.nullOr lib.types.nonEmptyStr;
|
|
||||||
default = null;
|
|
||||||
};
|
|
||||||
};
|
|
||||||
};
|
};
|
||||||
|
|
||||||
config = lib.mkIf cfg.enable {
|
config = lib.mkIf cfg.enable {
|
||||||
|
|
@ -40,30 +28,5 @@ in
|
||||||
"--advertise-exit-node=${lib.boolToString cfg.exitNode.enable}"
|
"--advertise-exit-node=${lib.boolToString cfg.exitNode.enable}"
|
||||||
];
|
];
|
||||||
};
|
};
|
||||||
|
|
||||||
systemd.services =
|
|
||||||
let
|
|
||||||
mode = if cfg.serve.isFunnel then "funnel" else "serve";
|
|
||||||
in
|
|
||||||
{
|
|
||||||
"tailscaled-${mode}" = lib.mkIf (cfg.serve.target != null) {
|
|
||||||
after = [
|
|
||||||
"tailscaled.service"
|
|
||||||
"tailscaled-autoconnect.service"
|
|
||||||
];
|
|
||||||
wants = [ "tailscaled.service" ];
|
|
||||||
wantedBy = [ "multi-user.target" ];
|
|
||||||
serviceConfig = {
|
|
||||||
Type = "oneshot";
|
|
||||||
RemainAfterExit = true;
|
|
||||||
ExecStartPre = "${lib.getExe pkgs.tailscale} cert --min-validity 120h ${config.networking.hostName}.${cfg.domain}";
|
|
||||||
ExecStart = "${lib.getExe pkgs.tailscale} ${mode} --bg ${cfg.serve.target}";
|
|
||||||
ExecStop = "${lib.getExe pkgs.tailscale} ${mode} reset";
|
|
||||||
Restart = "on-failure";
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
||||||
tailscaled-set.after = [ "tailscaled-autoconnect.service" ];
|
|
||||||
};
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
|
||||||
45
modules/system/services/tailscale/serve-funnel.nix
Normal file
45
modules/system/services/tailscale/serve-funnel.nix
Normal file
|
|
@ -0,0 +1,45 @@
|
||||||
|
{
|
||||||
|
config,
|
||||||
|
pkgs,
|
||||||
|
lib,
|
||||||
|
...
|
||||||
|
}:
|
||||||
|
let
|
||||||
|
cfg = config.custom.services.tailscale;
|
||||||
|
in
|
||||||
|
{
|
||||||
|
options.custom.services.tailscale.serve = {
|
||||||
|
isFunnel = lib.mkEnableOption "";
|
||||||
|
target = lib.mkOption {
|
||||||
|
type = lib.types.nullOr lib.types.nonEmptyStr;
|
||||||
|
default = null;
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
config = lib.mkIf cfg.enable {
|
||||||
|
systemd.services =
|
||||||
|
let
|
||||||
|
mode = if cfg.serve.isFunnel then "funnel" else "serve";
|
||||||
|
in
|
||||||
|
{
|
||||||
|
"tailscaled-${mode}" = lib.mkIf (cfg.serve.target != null) {
|
||||||
|
after = [
|
||||||
|
"tailscaled.service"
|
||||||
|
"tailscaled-autoconnect.service"
|
||||||
|
];
|
||||||
|
wants = [ "tailscaled.service" ];
|
||||||
|
wantedBy = [ "multi-user.target" ];
|
||||||
|
serviceConfig = {
|
||||||
|
Type = "oneshot";
|
||||||
|
RemainAfterExit = true;
|
||||||
|
ExecStartPre = "${lib.getExe pkgs.tailscale} cert --min-validity 120h ${config.networking.hostName}.${cfg.domain}";
|
||||||
|
ExecStart = "${lib.getExe pkgs.tailscale} ${mode} --bg ${cfg.serve.target}";
|
||||||
|
ExecStop = "${lib.getExe pkgs.tailscale} ${mode} reset";
|
||||||
|
Restart = "on-failure";
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
tailscaled-set.after = [ "tailscaled-autoconnect.service" ];
|
||||||
|
};
|
||||||
|
};
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue