Running a script based on udev rules

linux dev cli nix

published 2022-02-26 00:07

updated 2022-02-26 00:07

If you plug in your monitor you should be able to automatically get the config you want First write a script that does that for 'xrandr'

! /usr/bin/env bash

export DISPLAY=:0 export XAUTHORITY=/home/dan/.Xauthority

function connect(){ xrandr --output HDMI-2 --same-as eDP-1 }

function disconnect(){ xrandr --output HDMI-2 --off }

xrandr | grep "HDMI1 connected" &> /dev/null && connect || disconnect

And now create a udev rule to listen to it For nixOS this would be services.udev.extraRules

KERNEL=="card0", SUBSYSTEM=="drm", ENV{DISPLAY}=":0", ENV{XAUTHORITY}="/home/anish/.Xauthorit y", RUN+="/home/anish/bin/"