Skip to content

Commit f69d7d9

Browse files
feat(lab): added wireguard configuration and setup
added note about potentially refactoring the external interface
1 parent 0e328a4 commit f69d7d9

File tree

3 files changed

+115
-1
lines changed

3 files changed

+115
-1
lines changed

configurations/nixos/lab/services/services.nix

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
{ inputs, config, ... }:
1+
{
2+
inputs,
3+
config,
4+
pkgs,
5+
...
6+
}:
27
let
38
inherit (config.networking) hostName;
49
in
@@ -59,5 +64,11 @@ in
5964
};
6065
vscode-server.enable = true;
6166
wastebin.enable = true;
67+
wireguard = {
68+
enable = true;
69+
interfaces = import ./wireguard.nix {
70+
inherit pkgs;
71+
};
72+
};
6273
};
6374
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
{ pkgs, ... }:
2+
{
3+
# "wg0" is the network interface name. You can name the interface arbitrarily.
4+
wg0 = {
5+
# Determines the IP address and subnet of the server's end of the tunnel interface.
6+
ips = [ "10.100.0.1/24" ];
7+
8+
# The port that WireGuard listens to. Must be accessible by the client.
9+
listenPort = 51820;
10+
11+
# This allows the wireguard server to route your traffic to the internet and hence be like a VPN
12+
# For this to work you have to set the dnsserver IP of your router (or dnsserver of choice) in your clients
13+
# ${pkgs.iptables}/bin/iptables -A FORWARD -i wg0 -j ACCEPT;
14+
postSetup =
15+
# bash
16+
''
17+
${pkgs.iptables}/bin/iptables -t nat -A POSTROUTING -s 10.100.0.0/24 -o enp4s0 -j MASQUERADE
18+
'';
19+
20+
# This undoes the above command
21+
# ${pkgs.iptables}/bin/iptables -D FORWARD -i wg0 -j ACCEPT;
22+
postShutdown =
23+
# bash
24+
''
25+
${pkgs.iptables}/bin/iptables -t nat -D POSTROUTING -s 10.100.0.0/24 -o enp4s0 -j MASQUERADE
26+
'';
27+
28+
# https://superuser.com/questions/1716478/wireguard-no-access-to-the-internet
29+
30+
# Path to the private key file.
31+
#
32+
# Note: The private key can also be included inline via the privateKey option,
33+
# but this makes the private key world-readable; thus, using privateKeyFile is
34+
# recommended.
35+
privateKeyFile = "/var/secrets/wg-privatekey";
36+
37+
peers = [
38+
# List of allowed peers.
39+
{
40+
# Feel free to give a meaningful name
41+
name = "mbp3.local";
42+
# Public key of the peer (not a file path).
43+
publicKey = "5U5c73rfEZ6uSJuVZQudKX5Ir5dZHSq1rmsiKsgzJmI=";
44+
# List of IPs assigned to this peer within the tunnel subnet. Used to configure routing.
45+
allowedIPs = [ "10.100.0.2/32" ];
46+
}
47+
{
48+
name = "iphone";
49+
publicKey = "maAlHZyL5YGILhqm2hCCqTZepTLt7VoEGyWzQca2gVk=";
50+
allowedIPs = [ "10.100.0.3/32" ];
51+
}
52+
];
53+
};
54+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
{
2+
config,
3+
options,
4+
lib,
5+
pkgs,
6+
...
7+
}:
8+
let
9+
serviceName = "wireguard";
10+
11+
cfg = config.nixos.services.${serviceName};
12+
in
13+
{
14+
options.nixos.services.${serviceName} = {
15+
enable = lib.mkEnableOption ''
16+
Userspace Go implementation of WireGuard
17+
'';
18+
interfaces = lib.mkOption {
19+
inherit (options.networking.wireguard.interfaces) type;
20+
default = { };
21+
};
22+
};
23+
24+
config = lib.mkIf cfg.enable {
25+
# enable NAT
26+
networking = {
27+
nat = {
28+
enable = true;
29+
# TODO: may need to modularize the external interface name
30+
# for example, many of the examples had `eth0`
31+
externalInterface = "enp4s0";
32+
internalInterfaces = [ "wg0" ];
33+
};
34+
firewall = {
35+
allowedUDPPorts = [ 51820 ];
36+
};
37+
};
38+
39+
networking.wireguard = {
40+
inherit (cfg) interfaces;
41+
42+
enable = true;
43+
};
44+
45+
environment.systemPackages = with pkgs; [
46+
wireguard-tools
47+
];
48+
};
49+
}

0 commit comments

Comments
 (0)