2023-03-02 16:00:19 +03:00
|
|
|
{ config, lib, pkgs, ... }:
|
|
|
|
|
|
|
|
let
|
|
|
|
cfg = config.local.wireguard;
|
|
|
|
|
2023-07-28 17:08:13 +03:00
|
|
|
# externalServerData = import ../hosts/istal/data.secret.nix;
|
|
|
|
serverData = import ../hosts/canigou/data.secret.nix;
|
|
|
|
# serverData = import ../hosts/istal/data.secret.nix;
|
2023-03-02 16:00:19 +03:00
|
|
|
|
2023-07-28 17:08:13 +03:00
|
|
|
serverAddr = serverData.addr;
|
|
|
|
serverPort = serverData.wireguard.port;
|
2023-03-02 17:36:05 +03:00
|
|
|
|
|
|
|
# Run `ip route` to show gateway
|
2023-03-02 16:00:19 +03:00
|
|
|
defaultGateway = "192.168.0.1";
|
|
|
|
in
|
|
|
|
{
|
|
|
|
options.local.wireguard = with lib; {
|
2023-03-02 18:57:38 +03:00
|
|
|
enable = mkEnableOption "Enable wireguard vpn";
|
2023-03-02 16:00:19 +03:00
|
|
|
ip = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
description = "10.100.0.<num>/24";
|
|
|
|
example = "10.100.0.1/24";
|
|
|
|
};
|
|
|
|
privateKeyFile = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
config = lib.mkIf cfg.enable {
|
|
|
|
networking.firewall = {
|
2023-03-20 23:32:29 +03:00
|
|
|
allowedUDPPorts = [ serverPort ]; # Clients and peers can use the same port, see listenport
|
2023-03-02 16:00:19 +03:00
|
|
|
};
|
|
|
|
# Enable WireGuard
|
2023-07-28 17:08:13 +03:00
|
|
|
networking.wg-quick.interfaces = {
|
2023-03-02 16:00:19 +03:00
|
|
|
# "wg0" is the network interface name. You can name the interface arbitrarily.
|
|
|
|
wg0 = {
|
|
|
|
# Determines the IP address and subnet of the client's end of the tunnel interface.
|
2023-07-28 17:08:13 +03:00
|
|
|
address = [ cfg.ip ];
|
|
|
|
|
2023-03-20 23:32:29 +03:00
|
|
|
listenPort = serverPort; # to match firewall allowedUDPPorts (without this wg uses random port numbers)
|
2023-03-02 16:00:19 +03:00
|
|
|
|
|
|
|
# Path to the private key file.
|
|
|
|
privateKeyFile = cfg.privateKeyFile;
|
|
|
|
|
2023-07-28 17:08:13 +03:00
|
|
|
# Add a more specific ip route allowing traffic to the VPN via the default gateway
|
2023-03-02 17:36:05 +03:00
|
|
|
# Source: https://discourse.nixos.org/t/route-all-traffic-through-wireguard-interface/1480/18
|
2023-07-28 17:08:13 +03:00
|
|
|
/*
|
|
|
|
postUp = ''
|
|
|
|
${pkgs.iproute}/bin/ip route add ${serverAddr} via ${defaultGateway}
|
|
|
|
'';
|
|
|
|
preDown = ''
|
|
|
|
${pkgs.iproute}/bin/ip route del ${serverAddr} via ${defaultGateway}
|
|
|
|
'';
|
|
|
|
*/
|
2023-03-02 16:00:19 +03:00
|
|
|
|
|
|
|
peers = [
|
|
|
|
# For a client configuration, one peer entry for the server will suffice.
|
|
|
|
|
|
|
|
{
|
|
|
|
# Public key of the server (not a file path).
|
2023-07-28 17:08:13 +03:00
|
|
|
publicKey = serverData.wireguard.publicKey;
|
2023-03-02 16:00:19 +03:00
|
|
|
|
|
|
|
# Forward all the traffic via VPN.
|
|
|
|
allowedIPs = [ "0.0.0.0/0" ];
|
|
|
|
# Or forward only particular subnets
|
|
|
|
# allowedIPs = [ "192.168.0.0/24" ];
|
|
|
|
|
|
|
|
# Set this to the server IP and port.
|
2023-03-20 23:32:29 +03:00
|
|
|
endpoint = "${serverAddr}:${toString serverPort}";
|
2023-03-02 16:00:19 +03:00
|
|
|
|
|
|
|
# Send keepalives every 25 seconds. Important to keep NAT tables alive.
|
2023-03-20 23:32:29 +03:00
|
|
|
persistentKeepalive = 15;
|
2023-03-02 16:00:19 +03:00
|
|
|
}
|
|
|
|
];
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|