# \[SOLVED\] Minimal firewall setup for Wireguard client?

**URL:** https://discourse.nixos.org/t/solved-minimal-firewall-setup-for-wireguard-client/7577
**Category:** Help
**Created:** [June 8, 2020, 10:25pm UTC](https://discourse.nixos.org/t/solved-minimal-firewall-setup-for-wireguard-client/7577 "2020-06-08T22:25:03Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![errnoh](https://discourse.nixos.org/user_avatar/discourse.nixos.org/errnoh/32/2608_2.png) [@errnoh](https://discourse.nixos.org/u/errnoh)
#### Post date: [June 8, 2020, 10:25pm UTC](https://discourse.nixos.org/t/solved-minimal-firewall-setup-for-wireguard-client/7577/1 "2020-06-08T22:25:03Z")

</div>

Edit: Solved for `wg-quick`. See first reply.

I can’t seem to get Wireguard traffic through NixOS default firewall rules without doing full `networking.firewall.enable = false;`.

Currently I’ve tested the same Wireguard configuration with:

- fresh NixOS install, firewall enabled: doesn’t work
- fresh NixOS isntall, firewall disabled: works
- fresh Ubuntu install: works
- OSX: works

I do have the wg ListenPort 51820 in my `allowedUDPPorts` but nothing seems to be hitting the rule:

` 0 0 nixos-fw-accept udp -- any any anywhere anywhere udp dpt:51820`

also when Wireguard interface is up almost no traffic is hitting the `nixos-fw` table.

FWIW I’m doing my testing using manual `wg-quick up` in order to have identical setup on every test machine, but didn’t have luck earlier with `networking.wireguard.interfaces` either.

So my question is: starting from out-of-the-box NixOS (20.03) installation, what’s the minimal setup needed to route all traffic through a Wireguard interface without disabling the firewall?

---

<div class="post-metadata">

### Author: ![errnoh](https://discourse.nixos.org/user_avatar/discourse.nixos.org/errnoh/32/2608_2.png) [@errnoh](https://discourse.nixos.org/u/errnoh)
#### Post date: [June 9, 2020, 11:28am UTC](https://discourse.nixos.org/t/solved-minimal-firewall-setup-for-wireguard-client/7577/2 "2020-06-09T11:28:38Z")

</div>

After debugging this a bit more I’ve found the culcript but I’m still unsure what would be the idiomatic way to fix this.

I noticed that the packets are actually being dropped in the `raw` table and the rule that’s dropping them is `-A nixos-fw-rpfilter -j DROP`. Once that’s clear it’s easy enough to create a rule that fixes the problem:

`iptables -I nixos-fw-rpfilter 2 -t raw -p udp -m udp --dport 51820 -j RETURN`

And indeed it works. Traffic is now allowed to go through and Wireguard works as intended. I’m quite sure I can just automate this rather easily by adding pre/post hooks to my Wireguard configuration, but that doesn’t seem intended. Looking at it more closely it seems that before enabling Wireguard traffic is going through this rule:

`-A nixos-fw-rpfilter -m rpfilter --validmark -j RETURN`

But Wireguard traffic seems to bypass that rule? Looking at `firewall.nix` it seems that there’s this: `${optionalString (cfg.checkReversePath == "loose") "--loose"}`.

Turns out enabling that actually does the trick. I wouldn’t mind an explanation why, but for anyone looking for a solution to the same issue, here it is. (I’ll probably try to add it to the wiki as well)

---

<div class="post-metadata">

### Author: ![errnoh](https://discourse.nixos.org/user_avatar/discourse.nixos.org/errnoh/32/2608_2.png) [@errnoh](https://discourse.nixos.org/u/errnoh)
#### Post date: [June 9, 2020, 1:19pm UTC](https://discourse.nixos.org/t/solved-minimal-firewall-setup-for-wireguard-client/7577/3 "2020-06-09T13:19:59Z")

</div>

Still not completely there though. I’d like to get this working for interfaces created with `networking.wireguard.interfaces`.

Those seem to create identical interface to what `wg-quick` created, but generate different routes. `wg-quick` does the fwmark trick from [Routing & Network Namespaces - WireGuard](https://www.wireguard.com/netns/) but interfaces created with `networking.wireguard.interfaces` just seem to add new default route as well as route for the assigned ip:

```auto
1d0
< default dev wg0 scope link 
3d1
< 10.0.0.0/24 dev wg0 proto kernel scope link src 10.0.0.11

```

While this is active I’m unable to get any packets through. By removing the default route that was added traffic bypasses Wireguard and everything works normally. If I add a rule `ip route add 1.1.1.1 dev wg0` traffic does indeed successfully go through the tunnel when connecting to that ip.

So why is the default route not working?

---

<div class="post-metadata">

### Author: ![errnoh](https://discourse.nixos.org/user_avatar/discourse.nixos.org/errnoh/32/2608_2.png) [@errnoh](https://discourse.nixos.org/u/errnoh)
#### Post date: [June 9, 2020, 7:57pm UTC](https://discourse.nixos.org/t/solved-minimal-firewall-setup-for-wireguard-client/7577/4 "2020-06-09T19:57:02Z")

</div>

Found the final missing piece.

[https://wiki.archlinux.org/index.php/WireGuard#Loop\_routing](https://wiki.archlinux.org/index.php/WireGuard#Loop_routing)

Adding proper `ip route add <endpoint ip> via <gateway> dev <network interface>` for the endpoint allows the default route to work as intended. Now both the `networking.wireguard.interfaces` and manual `wg-quick` work as intended.

---

<div class="post-metadata">

### Author: ![Kranzes](https://discourse.nixos.org/user_avatar/discourse.nixos.org/kranzes/32/7577_2.png) [@Kranzes](https://discourse.nixos.org/u/Kranzes)
#### Post date: [January 8, 2021, 11:07pm UTC](https://discourse.nixos.org/t/solved-minimal-firewall-setup-for-wireguard-client/7577/5 "2021-01-08T23:07:42Z")

</div>

“this only works for a static network setup and fails if gateways or devices change (e.g. using ethernet or wifi on a laptop).”

---

<div class="post-metadata">

### Author: ![chrissound](https://discourse.nixos.org/user_avatar/discourse.nixos.org/chrissound/32/2388_2.png) [@chrissound](https://discourse.nixos.org/u/chrissound)
#### Post date: [March 7, 2021, 12:04pm UTC](https://discourse.nixos.org/t/solved-minimal-firewall-setup-for-wireguard-client/7577/6 "2021-03-07T12:04:22Z")

</div>

Hmm potentially seem to be running into the same issue.

What does `endpoint ip` and `gateway` get set to? I’ve tried the public ip of the wireguard server, and the ‘usual’ gateway ip with seemingly no change in behavior.

Strangely if I set `allowed_ips` to a set of ips that is not `0.0.0.0`, then it works correctly.

---

<div class="post-metadata">

### Author: ![supermarin](https://discourse.nixos.org/user_avatar/discourse.nixos.org/supermarin/32/9149_2.png) [@supermarin](https://discourse.nixos.org/u/supermarin)
#### Post date: [April 5, 2022, 3:16am UTC](https://discourse.nixos.org/t/solved-minimal-firewall-setup-for-wireguard-client/7577/7 "2022-04-05T03:16:43Z")

</div>

Same here. The `ip route add` way doesn’t make sense for a non-static IP setup, like on a laptop. Haven’t figured out how to resolve this yet

---

<div class="post-metadata">

### Author: ![leana8959](https://discourse.nixos.org/user_avatar/discourse.nixos.org/leana8959/32/33046_2.png) [@leana8959](https://discourse.nixos.org/u/leana8959)
#### Post date: [January 1, 2024, 9:02pm UTC](https://discourse.nixos.org/t/solved-minimal-firewall-setup-for-wireguard-client/7577/8 "2024-01-01T21:02:17Z")

</div>

Hello, I would like to set up wireguard on my computer, are there new methods to do this without creating an `ip route` ? Thanks 🙂

Update: I noticed that I don’t actually need to set up an ip route for this, it wouldn’t work before due to another misconfiguration. Have a great day 🙂

---

<div class="post-metadata">

### Author: ![Salvage-This](https://discourse.nixos.org/letter_avatar_proxy/v4/letter/s/c5a1d2/32.png) [@Salvage-This](https://discourse.nixos.org/u/Salvage-This)
#### Post date: [November 21, 2024, 5:37pm UTC](https://discourse.nixos.org/t/solved-minimal-firewall-setup-for-wireguard-client/7577/9 "2024-11-21T17:37:30Z")

</div>

Sorry for bumping such an old thread, but some relevant documentation still seems to link here. Adding a fix here for those that want it.

I added this script that will auto detect the endpoint, gateway and network device used in your default route and adds a route that will fix the connection issues using the fix described in [https://wiki.archlinux.org/title/WireGuard#Loop\_routing](https://wiki.archlinux.org/title/WireGuard#Loop_routing)

It has one dependency in wg (installed with [wireguard-tools](https://search.nixos.org/packages?channel=24.05&show=wireguard-tools&from=0&size=50&sort=relevance&type=packages&query=wireguard-tools)) and the script also assumes you are using a wireguard interface named wg0. Be sure to update to match your settings.

one troubleshooting note, you will need to activate your wireguard connection first before attempting to run the script. Running it before activating the connection will result in a failure to find the wireguard interface.

```bash
# Setup the basics
ENDPOINT_IP=$(wg show wg0 endpoints | awk '{print $2}' | cut -d':' -f1)
DEFAULT_ROUTE=$(ip route | grep default)
GW="$(echo $DEFAULT_ROUTE | cut -d ' ' -f3)"
DEV="$(echo $DEFAULT_ROUTE | cut -d ' ' -f5)"

# Check if there are other routes to be removed
if [[$(ip route | grep $ENDPOINT_IP)]]; then
    echo "route exists, removing..."
    ip route delete $(ip route | grep $ENDPOINT_IP)
fi

# Setup a new route
echo "Adding a new route for active connection...
executing -> ip route add $ENDPOINT_IP via $GW dev $DEV"
ip route add $ENDPOINT_IP via $GW dev $DEV

```

---

<div class="post-metadata">

### Author: ![nrbray](https://discourse.nixos.org/user_avatar/discourse.nixos.org/nrbray/32/8264_2.png) [@nrbray](https://discourse.nixos.org/u/nrbray)
#### Post date: [October 29, 2025, 4:15pm UTC](https://discourse.nixos.org/t/solved-minimal-firewall-setup-for-wireguard-client/7577/10 "2025-10-29T16:15:37Z")

</div>

> [@Salvage-This](#):
>
> `DEFAULT_ROUTE=$(ip route | grep default)`

This I changed to  
`DEFAULT_ROUTE=$(ip route | grep 'default via')`

and that worked for me, else it picked up the first (instead of the wanted) of two default entries:

```auto
# ip route | grep default
default dev wg1 scope link 
default via 192.168.8.1 dev wlp4s0 proto dhcp src 192.168.8.115 metric 3002 

```
