This commit is contained in:
2025-07-20 12:19:41 +02:00
parent de723a9ca7
commit 6cdf8b53cc
2 changed files with 30 additions and 16 deletions

View File

@@ -32,6 +32,7 @@
192.168.88.14 cnst.dev 192.168.88.14 cnst.dev
192.168.88.14 lidarr.cnst.dev 192.168.88.14 lidarr.cnst.dev
192.168.88.14 prowlarr.cnst.dev 192.168.88.14 prowlarr.cnst.dev
192.168.88.14 deluge.cnst.dev
''; '';
interfaces = { interfaces = {
"eno1" = { "eno1" = {

View File

@@ -19,16 +19,12 @@ in {
}; };
}; };
config = lib.mkIf cfg.enable { config = lib.mkIf cfg.enable (
systemd.services."netns@${cfg.namespace}" = { let
description = "WireGuard VPN netns (${cfg.namespace})"; netnsSetup = pkgs.writeShellApplication {
after = ["network-online.target"]; name = "netns-${cfg.namespace}-setup";
wants = ["network-online.target"]; runtimeInputs = with pkgs; [iproute2 wireguard-tools gawk coreutils];
wantedBy = ["multi-user.target"]; text = ''
serviceConfig = {
Type = "oneshot";
RemainAfterExit = true;
ExecStart = pkgs.writeShellScript "netns-${cfg.namespace}-setup" ''
set -eux set -eux
CONFIG=${cfg.configFile} CONFIG=${cfg.configFile}
@@ -36,9 +32,7 @@ in {
ADDR=$(awk -F' *= *' '/^Address/ { print $2 }' "$CONFIG") ADDR=$(awk -F' *= *' '/^Address/ { print $2 }' "$CONFIG")
DNS=$(awk -F' *= *' '/^DNS/ { print $2 }' "$CONFIG") DNS=$(awk -F' *= *' '/^DNS/ { print $2 }' "$CONFIG")
# Clean up any existing netns
ip netns delete "$NS" 2>/dev/null || true ip netns delete "$NS" 2>/dev/null || true
ip netns add "$NS" ip netns add "$NS"
ip link add wg0 type wireguard ip link add wg0 type wireguard
ip link set wg0 netns "$NS" ip link set wg0 netns "$NS"
@@ -48,14 +42,33 @@ in {
ip netns exec "$NS" ip link set lo up ip netns exec "$NS" ip link set lo up
ip netns exec "$NS" ip route add default dev wg0 ip netns exec "$NS" ip route add default dev wg0
# Set DNS
mkdir -p /etc/netns/"$NS" mkdir -p /etc/netns/"$NS"
echo "nameserver $DNS" > /etc/netns/"$NS"/resolv.conf echo "nameserver $DNS" > /etc/netns/"$NS"/resolv.conf
''; '';
ExecStop = pkgs.writeShellScript "netns-${cfg.namespace}-teardown" '' };
netnsTeardown = pkgs.writeShellApplication {
name = "netns-${cfg.namespace}-teardown";
runtimeInputs = with pkgs; [iproute2];
text = ''
set -eu
ip netns delete ${cfg.namespace} || true ip netns delete ${cfg.namespace} || true
''; '';
}; };
}; in {
}; systemd.services."netns@${cfg.namespace}" = {
description = "WireGuard VPN netns (${cfg.namespace})";
after = ["network-online.target"];
wants = ["network-online.target"];
wantedBy = ["multi-user.target"];
serviceConfig = {
Type = "oneshot";
RemainAfterExit = true;
ExecStart = "${netnsSetup}/bin/netns-${cfg.namespace}-setup";
ExecStop = "${netnsTeardown}/bin/netns-${cfg.namespace}-teardown";
};
};
}
);
} }