Updating wireguard setup
This commit is contained in:
2 files changed
+73
-67
No files matched your search
+69
-64
@@ -1,84 +1,89 @@
|
||||
{ config, pkgs, ... }:
|
||||
{ config, pkgs, lib, ... }:
|
||||
|
||||
let
|
||||
vpnInterfaces = [ "Home" "Backup" ];
|
||||
lower = lib.strings.toLower;
|
||||
in
|
||||
{
|
||||
sops.secrets.wireguard_home = {
|
||||
sops.secrets = lib.genAttrs vpnInterfaces (name: {
|
||||
sopsFile = ../secrets/wireguard.yaml;
|
||||
key = "wireguard_home_conf";
|
||||
path = "/run/secrets/Home.conf";
|
||||
key = "${lower name}_conf";
|
||||
path = "/run/secrets/${name}.conf";
|
||||
restartUnits = [ "wireguard-${lower name}.service" ];
|
||||
});
|
||||
|
||||
restartUnits = [ "wireguard-home.service" ];
|
||||
};
|
||||
systemd.services = lib.listToAttrs (map (name:
|
||||
let
|
||||
serviceName = "wireguard-${lower name}";
|
||||
in {
|
||||
name = serviceName;
|
||||
value = {
|
||||
description = "Import ${name} WireGuard connection into NetworkManager";
|
||||
|
||||
systemd.services.wireguard-home = {
|
||||
description = "Import Home WireGuard connection into NetworkManager";
|
||||
wantedBy = [ "multi-user.target" ];
|
||||
|
||||
wantedBy = [ "multi-user.target" ];
|
||||
after = [
|
||||
"NetworkManager.service"
|
||||
"sops-nix.service"
|
||||
];
|
||||
|
||||
after = [
|
||||
"NetworkManager.service"
|
||||
"sops-nix.service"
|
||||
];
|
||||
wants = [
|
||||
"NetworkManager.service"
|
||||
"sops-nix.service"
|
||||
];
|
||||
|
||||
wants = [
|
||||
"NetworkManager.service"
|
||||
"sops-nix.service"
|
||||
];
|
||||
serviceConfig = {
|
||||
Type = "oneshot";
|
||||
RemainAfterExit = true;
|
||||
};
|
||||
|
||||
serviceConfig = {
|
||||
Type = "oneshot";
|
||||
RemainAfterExit = true;
|
||||
};
|
||||
script = ''
|
||||
set -euo pipefail
|
||||
|
||||
script = ''
|
||||
set -euo pipefail
|
||||
new_hash=$(
|
||||
${pkgs.coreutils}/bin/sha256sum /run/secrets/${name}.conf |
|
||||
${pkgs.coreutils}/bin/cut -d' ' -f1
|
||||
)
|
||||
|
||||
new_hash=$(
|
||||
${pkgs.coreutils}/bin/sha256sum /run/secrets/Home.conf |
|
||||
${pkgs.coreutils}/bin/cut -d' ' -f1
|
||||
)
|
||||
old_hash=$(
|
||||
${pkgs.coreutils}/bin/cat /var/lib/${serviceName}/hash 2>/dev/null || true
|
||||
)
|
||||
|
||||
old_hash=$(
|
||||
${pkgs.coreutils}/bin/cat /var/lib/wireguard-home/hash 2>/dev/null || true
|
||||
)
|
||||
if [ "$new_hash" = "$old_hash" ]; then
|
||||
echo "${name} WireGuard configuration unchanged."
|
||||
exit 0
|
||||
fi
|
||||
|
||||
if [ "$new_hash" = "$old_hash" ]; then
|
||||
echo "Home WireGuard configuration unchanged."
|
||||
exit 0
|
||||
fi
|
||||
echo "${name} WireGuard configuration changed; updating NetworkManager..."
|
||||
|
||||
echo "Home WireGuard configuration changed; updating NetworkManager..."
|
||||
if ${pkgs.networkmanager}/bin/nmcli -t -f NAME,ACTIVE connection show \
|
||||
| ${pkgs.gnugrep}/bin/grep -q '^${name}:yes$'; then
|
||||
was_active=true
|
||||
else
|
||||
was_active=false
|
||||
fi
|
||||
|
||||
# Remember whether Home was active before replacing it.
|
||||
if ${pkgs.networkmanager}/bin/nmcli -t -f NAME,ACTIVE connection show \
|
||||
| ${pkgs.gnugrep}/bin/grep -q '^Home:yes$'; then
|
||||
home_was_active=true
|
||||
else
|
||||
home_was_active=false
|
||||
fi
|
||||
${pkgs.networkmanager}/bin/nmcli connection delete '${name}' || true
|
||||
|
||||
${pkgs.networkmanager}/bin/nmcli connection delete Home || true
|
||||
${pkgs.networkmanager}/bin/nmcli connection import \
|
||||
type wireguard \
|
||||
file /run/secrets/${name}.conf
|
||||
|
||||
${pkgs.networkmanager}/bin/nmcli connection modify '${name}' connection.autoconnect no
|
||||
|
||||
${pkgs.networkmanager}/bin/nmcli connection import \
|
||||
type wireguard \
|
||||
file /run/secrets/Home.conf
|
||||
|
||||
# Make it not autoconnect on reboot
|
||||
${pkgs.networkmanager}/bin/nmcli connection modify Home connection.autoconnect no
|
||||
if [ "$was_active" = true ]; then
|
||||
${pkgs.networkmanager}/bin/nmcli connection up '${name}'
|
||||
else
|
||||
${pkgs.networkmanager}/bin/nmcli connection down '${name}' || true
|
||||
echo "${name} was not active; disabling just to be sure."
|
||||
fi
|
||||
|
||||
# Only re-enable it if it was active before the update.
|
||||
if [ "$home_was_active" = true ]; then
|
||||
${pkgs.networkmanager}/bin/nmcli connection up Home
|
||||
else
|
||||
${pkgs.networkmanager}/bin/nmcli connection down Home
|
||||
echo "Home was not active; disabling just to be sure."
|
||||
fi
|
||||
${pkgs.coreutils}/bin/mkdir -p /var/lib/${serviceName}
|
||||
echo "$new_hash" > /var/lib/${serviceName}/hash
|
||||
'';
|
||||
};
|
||||
}
|
||||
) vpnInterfaces);
|
||||
|
||||
${pkgs.coreutils}/bin/mkdir -p /var/lib/wireguard-home
|
||||
echo "$new_hash" > /var/lib/wireguard-home/hash
|
||||
'';
|
||||
};
|
||||
|
||||
systemd.tmpfiles.rules = [
|
||||
"d /var/lib/wireguard-home 0755 root root -"
|
||||
];
|
||||
systemd.tmpfiles.rules = map (name: "d /var/lib/wireguard-${lower name} 0755 root root -") vpnInterfaces;
|
||||
}
|
||||
Reference in new issue
Block a user