On my mission to a leaner computer I’ve been slowly replacing system tray applets with less-bloated Polybar modules. Most of what I need is already covered by Polybar’s official modules or their community-contributed repository but it’s easy and fun to add new ones. Here’s my first one.

“wireguard-polybar”: A simple script/module to display and control WireGuard VPNs.

With no arguments the script displays the currently-connected configuration. It also provides --connect, --disconnect, and --toggle switches that can be mapped to different mouse clicks. When connecting, it uses dmenu so that you can select a WireGuard configuration from the list of available ones.

demo

Some network managers like Netctl and NetworkManager have built-in support for managing WireGuard interfaces. I don’t use either so under the hood the script directly calls networkctl and wg-quick to set things up. Unfortunately wg-quick needs sudo access to run which isn’t great news for scripting. I therefore had to add a specific rule to my /etc/sudoers (using visudo) to make it password-less:

YOUR_USERNAME ALL = (root) NOPASSWD: /usr/bin/wg-quick

(Obviously you don’t want to do this for a user who doesn’t already have sudo access! WireGuard let’s you to add hooks for various events which could let a non-sudo user run arbitrary code.)

Finally, adding a module to Polybar configuration is pretty straightforward . For example:

[module/wireguard]
type = custom/script
exec = /PATH/TO/wireguard_polybar
tail = false
interval = 1
click-left = /PATH/TO/wireguard_polybar --toggle &