Skip to content

Commit

Permalink
networking/wireguard-home: refactor to use networkd
Browse files Browse the repository at this point in the history
  • Loading branch information
linyinfeng committed Mar 7, 2024
1 parent bcdfc36 commit c0615f5
Showing 1 changed file with 44 additions and 17 deletions.
61 changes: 44 additions & 17 deletions nixos/profiles/networking/wireguard-home/default.nix
Original file line number Diff line number Diff line change
@@ -1,59 +1,86 @@
{
config,
pkgs,
lib,
...
}: let
interfaceName = "wg0";
interfaceName = "wg-home";
hostName = config.networking.hostName;
port = config.ports.wireguard;
hosts = {
framework = {
ip = "192.168.2.2/32";
ip = "192.168.2.2";
inherit port;
};
xps8930 = {
ip = "192.168.2.3/32";
ip = "192.168.2.3";
inherit port;
};
shg0 = {
ip = "192.168.2.4/32";
ip = "192.168.2.4";
inherit port;
};
enchilada = {
ip = "192.168.2.101/32";
ip = "192.168.2.101";
inherit port;
};
};
home = {
allowedIPs = [
AllowedIPs = [
"192.168.0.0/24"
"192.168.1.0/24"
"192.168.2.0/24"
];
endpoint = "home.li7g.com:13231";
publicKey = "2JEjZzJGtd6Om0JN4RooJ68QtYm1WMZRP+qSgv6lBXE=";
persistentKeepalive = 30;
# dynamicEndpointRefreshSeconds = 60;
Endpoint = "home.li7g.com:13231";
PublicKey = "2JEjZzJGtd6Om0JN4RooJ68QtYm1WMZRP+qSgv6lBXE=";
PersistentKeepalive = 30;
};
in {
networking.wireguard.interfaces.${interfaceName} = {
ips = [hosts.${hostName}.ip];
listenPort = hosts.${hostName}.port;
peers = [home];
privateKeyFile = config.sops.secrets."wireguard_private_key".path;
systemd.network.netdevs."80-wg-home" = {
netdevConfig = {
Name = interfaceName;
Kind = "wireguard";
};
wireguardConfig = {
PrivateKeyFile = config.sops.secrets."wireguard_private_key".path;
ListenPort = hosts.${hostName}.port;
};
wireguardPeers = [
{wireguardPeerConfig = home;}
];
};
systemd.network.networks."80-wg-home" = {
matchConfig = {
Name = interfaceName;
};
addresses = [
{
addressConfig = {
Address = "${hosts.${hostName}.ip}/24";
Scope = "link";
};
}
];
routes = lib.lists.map (ip: {
routeConfig = {
Destination = ip;
Scope = "site";
};
}) home.AllowedIPs;
};
sops.secrets."wireguard_private_key" = {
terraformOutput = {
enable = true;
perHost = true;
};
restartUnits = ["wireguard-${interfaceName}.service"];
owner = "systemd-network";
restartUnits = ["sysemd-networkd.service"];
};
environment.systemPackages = with pkgs; [
wireguard-tools
];
networking.firewall.allowedUDPPorts = [
config.networking.wireguard.interfaces.${interfaceName}.listenPort
hosts.${hostName}.port
];
networking.networkmanager.unmanaged = [interfaceName];
}

0 comments on commit c0615f5

Please sign in to comment.