aboutsummaryrefslogtreecommitdiff
path: root/wireguard.nix
diff options
context:
space:
mode:
authorIsabella Bastaja <isabella@sorre.xyz>2026-05-28 17:12:58 +0200
committerIsabella Morgana Bastaja <isabella@sorre.xyz>2026-05-30 11:59:47 +0200
commit65968fb7b1f6072424df1c396c097117e5dd7f63 (patch)
tree9b2b02229be4dff23227366d65747e9470b5b99f /wireguard.nix
parent798b338a7215916d679af2676c322fa3d84c5099 (diff)
common wireguard module
Diffstat (limited to 'wireguard.nix')
-rw-r--r--wireguard.nix78
1 files changed, 78 insertions, 0 deletions
diff --git a/wireguard.nix b/wireguard.nix
new file mode 100644
index 0000000..04ec3bc
--- /dev/null
+++ b/wireguard.nix
@@ -0,0 +1,78 @@
+{ config, pkgs, ... }:
+let
+ hostname = config.networking.hostName;
+ conf = hosts."${hostname}";
+ privateKeyPath = ./secrets/wireguard/${hostname}.txt;
+
+ lib = pkgs.lib;
+ wgPort = 51820; # Common port used on all peers
+ firewallMark = 42;
+
+ hosts = {
+ sagittarius = {
+ address = "2301:db8:acad:a::1/128";
+ publicKey = "GrrN8j9ZlPjTU5sQD4l8RDCGAn2YYszbSIE1zuG4OiI=";
+ endpoint = "95.216.136.85:${toString wgPort}";
+ };
+ nyaan = {
+ address = "2301:db8:acad:a::2/128";
+ publicKey = "u5nh+YNV84IbS7g0ncfs/V+cqEudNh500LQ7pmdS7j0=";
+ };
+ };
+
+ generatePeer =
+ hostname: values:
+ {
+ PublicKey = values.publicKey;
+ AllowedIPs = [ values.address ];
+ }
+ // lib.optionalAttrs (builtins.hasAttr "endpoint" values) {
+ Endpoint = values.endpoint;
+ PersistentKeepalive = 1;
+ };
+ generatePeers =
+ hostname:
+ let
+ peers = lib.attrsets.filterAttrs (k: v: k != hostname) hosts;
+ in
+ lib.attrsets.mapAttrsToList generatePeer peers;
+in
+{
+ sops.secrets."wireguard-${hostname}-pk" = {
+ sopsFile = privateKeyPath;
+ owner = "systemd-network";
+ group = "systemd-network";
+ mode = "0640";
+ format = "binary";
+ };
+
+ networking.firewall.allowedUDPPorts = [ wgPort ];
+
+ systemd.network = {
+ enable = true;
+ networks."50-wg" = {
+ matchConfig.Name = "wg0";
+ address = [ conf.address ];
+ };
+
+ netdevs."50-wg" = {
+ netdevConfig = {
+ Kind = "wireguard";
+ Name = "wg0";
+ };
+
+ wireguardConfig = {
+ ListenPort = wgPort;
+ PrivateKeyFile = config.sops.secrets."wireguard-${hostname}-pk".path;
+ # To automatically create routes for everything in AllowedIPs,
+ # add RouteTable=main
+ RouteTable = "main";
+ # FirewallMark marks all packets send and received by wg0 with the
+ # number, which can be used to define policy rules on these packets.
+ FirewallMark = firewallMark;
+ };
+
+ wireguardPeers = generatePeers hostname;
+ };
+ };
+}