This commit is contained in:
2025-07-20 14:20:59 +02:00
parent ff656dc319
commit 428a2cd615
4 changed files with 69 additions and 69 deletions

View File

@@ -44,23 +44,23 @@ in {
networking = { networking = {
hostName = "sobotka"; hostName = "sobotka";
domain = "cnst.dev"; domain = "cnst.dev";
wireguard = { # wireguard = {
interfaces.wg0 = { # interfaces.wg0 = {
ips = [ # ips = [
"127.0.0.1/8" # "127.0.0.1/8"
]; # ];
peers = [ # peers = [
{ # {
allowedIPs = [ # allowedIPs = [
"192.168.88.13/24" # "192.168.88.13/24"
]; # ];
# endpoint = "demo.wireguard.io:12913"; # # endpoint = "demo.wireguard.io:12913";
publicKey = "cUeRvwTwrL5GRc4dHjea89RJSa1kh4kIA/sHYzmscyQ="; # publicKey = "cUeRvwTwrL5GRc4dHjea89RJSa1kh4kIA/sHYzmscyQ=";
} # }
]; # ];
privateKeyFile = config.age.secrets.wgSobotkaPrivateKey.path; # privateKeyFile = config.age.secrets.wgSobotkaPrivateKey.path;
}; # };
}; # };
}; };
powerManagement.enable = false; powerManagement.enable = false;

View File

@@ -33,6 +33,8 @@
enable = true; enable = true;
namespace = "vpn"; namespace = "vpn";
configFile = config.age.secrets.wgCredentials.path; configFile = config.age.secrets.wgCredentials.path;
privateIP = "10.68.145.193/32";
dnsIP = "10.64.0.1";
}; };
}; };
} }

View File

@@ -66,6 +66,7 @@ in {
}; };
services."delugedproxy" = { services."delugedproxy" = {
enable = true;
description = "Proxy to Deluge in Network Namespace"; description = "Proxy to Deluge in Network Namespace";
requires = [ requires = [
"deluged.service" "deluged.service"
@@ -79,10 +80,10 @@ in {
JoinsNamespaceOf = "deluged.service"; JoinsNamespaceOf = "deluged.service";
}; };
serviceConfig = { serviceConfig = {
Type = "simple"; User = config.services.deluge.user;
Group = config.services.deluge.group;
ExecStart = "${pkgs.systemd}/lib/systemd/systemd-socket-proxyd --exit-idle-time=5min 127.0.0.1:58846"; ExecStart = "${pkgs.systemd}/lib/systemd/systemd-socket-proxyd --exit-idle-time=5min 127.0.0.1:58846";
PrivateNetwork = true; PrivateNetwork = "yes";
NetworkNamespacePath = "/var/run/netns/${ns}";
}; };
}; };
}; };

View File

@@ -17,57 +17,54 @@ in {
default = "vpn"; default = "vpn";
description = "Name of the network namespace"; description = "Name of the network namespace";
}; };
privateIP = lib.mkOption {
type = lib.types.str;
}; };
dnsIP = lib.mkOption {
config = lib.mkIf cfg.enable ( type = lib.types.str;
let
netnsSetup = pkgs.writeShellApplication {
name = "netns-${cfg.namespace}-setup";
runtimeInputs = with pkgs; [iproute2 wireguard-tools gawk coreutils];
text = ''
set -eux
CONFIG=${cfg.configFile}
NS=${cfg.namespace}
ADDR=$(awk -F' *= *' '/^Address/ { print $2 }' "$CONFIG")
DNS=$(awk -F' *= *' '/^DNS/ { print $2 }' "$CONFIG")
ip netns delete "$NS" 2>/dev/null || true
ip netns add "$NS"
ip link add wg0 type wireguard
ip link set wg0 netns "$NS"
IFS=',' read -ra ADDRS <<< "$ADDR"
for ip in "''${ADDRS[@]}"; do
ip -n "$NS" addr add "$ip" dev wg0
done
ip -n "$NS" link set wg0 up
grep -vE '^(Address|DNS) *=' "$CONFIG" | ip netns exec "$NS" wg setconf wg0 /dev/stdin
ip netns exec "$NS" ip link set lo up
ip netns exec "$NS" ip route add default dev wg0
mkdir -p /etc/netns/"$NS"
echo "nameserver $DNS" > /etc/netns/"$NS"/resolv.conf
'';
}; };
netnsTeardown = pkgs.writeShellApplication {
name = "netns-${cfg.namespace}-teardown";
runtimeInputs = with pkgs; [iproute2];
text = ''
set -eu
ip netns delete ${cfg.namespace} || true
'';
}; };
in { config = lib.mkIf cfg.enable {
systemd.services."netns@${cfg.namespace}" = { systemd.services."netns@" = {
description = "WireGuard VPN netns (${cfg.namespace})"; description = "%I network namespace";
requires = ["network-online.target"]; before = ["network.target"];
wantedBy = ["multi-user.target"];
serviceConfig = { serviceConfig = {
Type = "oneshot"; Type = "oneshot";
RemainAfterExit = true; RemainAfterExit = true;
ExecStart = "${netnsSetup}/bin/netns-${cfg.namespace}-setup"; ExecStart = "${pkgs.iproute2}/bin/ip netns add %I";
ExecStop = "${netnsTeardown}/bin/netns-${cfg.namespace}-teardown"; ExecStop = "${pkgs.iproute2}/bin/ip netns del %I";
};
};
environment.etc."netns/${cfg.namespace}/resolv.conf".text = "nameserver ${cfg.dnsIP}";
systemd.services.${cfg.namespace} = {
description = "${cfg.namespace} network interface";
bindsTo = ["netns@${cfg.namespace}.service"];
requires = ["network-online.target"];
after = ["netns@${cfg.namespace}.service"];
wantedBy = ["multi-user.target"];
serviceConfig = {
Type = "oneshot";
RemainAfterExit = true;
ExecStart = with pkgs;
writers.writeBash "wg-up" ''
set -e
${iproute2}/bin/ip link add wg0 type wireguard
${iproute2}/bin/ip link set wg0 netns ${cfg.namespace}
${iproute2}/bin/ip -n ${cfg.namespace} address add ${cfg.privateIP} dev wg0
${iproute2}/bin/ip netns exec ${cfg.namespace} \
${wireguard-tools}/bin/wg setconf wg0 ${cfg.configFile}
${iproute2}/bin/ip -n ${cfg.namespace} link set wg0 up
${iproute2}/bin/ip -n ${cfg.namespace} link set lo up
${iproute2}/bin/ip -n ${cfg.namespace} route add default dev wg0
'';
ExecStop = with pkgs;
writers.writeBash "wg-down" ''
set -e
${iproute2}/bin/ip -n ${cfg.namespace} route del default dev wg0
${iproute2}/bin/ip -n ${cfg.namespace} link del wg0
'';
};
}; };
}; };
} }
);
}