I want to be able to hotplug my external monitor that I use at home with my laptop and have xrandr respond by simply turning off the laptop display if it detects the external monitor. I have looked at the autorandr module and also the grobi option in home-manager but they both have a weird bug where they would try to reset the screens not just when the external monitor was plugged in/unplugged but also when it went blank (I have it set to go blank after 5 mins with xset).
I thought that I could get around this by simply having a script that did all the xrandr stuff and calling it whenever the external monitor was plugged/unplugged with a udev rule.
The script I’m calling is (pulled straight from the arch wiki with the added touch command for debugging purposes)
#!/usr/bin/env bash
touch /home/tom/Downloads/I_RAN
intern=eDP-1
extern=HDMI-1
if xrandr | grep "$extern disconnected"; then
sleep 1 ; xrandr --output "$extern" --off --output "$intern" --auto
else
sleep 1 ; xrandr --output "$intern" --off --output "$extern" --auto
fi
The udev rule in my configuration.nix is like
services.udev.extraRules = ''
ACTION=="change", SUBSYSTEM=="drm", RUN+="${pkgs.writeShellScriptBin "toggle_monitor" (builtins.readFile /home/tom/.dotfiles/toggle_monitor.sh)}"
'';
But unfortunately it’s not executing when I plug/unplug my external monitor to my laptop. I’m on 23.11. Any help figuring out either the udev rule or the weird bug with autorandr/grobi would be very much appreciated!