xrandr fails when run from udev

Solution 1:

udev does not have access to X, you have to give it the DISPLAY and XAUTHORITY environment variable:

KERNEL=="card0", SUBSYSTEM=="drm", ACTION=="change", ENV{DISPLAY}=":0", ENV{XAUTHORITY}="/home/ben/.Xauthority", RUN+="/bin/bash /path/to/script.sh"

(Taken from https://frdmtoplay.com/i3-udev-xrandr-hotplugging-output-switching/ )

Of course, you have to adjust the path to the .Xauthority file and the script.