Kurtis's answer would normally be correct, but this is WSL2, and there's (IMHO) a better solution on WSL2 that can use fish universal variables.
set -Ux DISPLAY (hostname).local:0
As long as the hostname
matches the Windows Computer Name (which it should and does by default), then that will always use mDNS to return the correct address, even if the IP has changed.
Note that you'll need to remove the global variable definition from ~/.config/fish/config.fish
or else the universal will be shadowed by the global.
Explanation:
You might think that it is the dynamically assigned DHCP address changing that is causing the problem, but that's not actually it. The IP address that you get back from ip route list default | awk '{print $3}'
is not the one that is assigned to Windows by DHCP on your home network.
It's actually the address of a virtual switch provided by WSL2 that allows WSL2 to communicate with the Windows host (and beyond).
When the Windows IP address changes, it doesn't matter to this virtual switch. Side note: I just confirmed this to make sure on WSL2 by changing my Windows IP manually.
The problem here is actually that the switch address changes each time the computer (or WSL2) restarts. This is why most instructions for setting your DISPLAY
on WSL2 tell you to map it to this address, rather than hardcoding it.
As Kurtis said, however, this type of assignment would (typically) be a problem if you were using a fish universal variable, since the virtual switch IP does change each reboot.
But WSL2 provides a convenient mDNS lookup for the address, in the form of <computername>.local
. By hardcoding that into the universal variable, DISPLAY
should always resolve correctly, even after the IP address itself changes.