This is a guide to setup the lightdm display-setup-script option using custom script that uses xrandr or autorandr. This is for people who have multi monitor setup.
sudo pacman -Syu
sudo pacman -S xorg-xrandrThis script analyzes your current monitor layout using xrandr and creates a .xrandr.conf file in your home directory. The file will contain an xrandr command that can be used to restore your display setup automatically.
You can run this anytime you change your monitor layout to update the config.
#!/bin/bash
OUT="$HOME/.xrandr.conf"
>"$OUT"
XRANDR_CMD="xrandr"
while IFS= read -r line; do
mon=$(echo "$line" | grep " connected" | awk '{print $1}')
if [[ -n $mon ]]; then
# Check if primary
is_primary=false
if [[ "$line" == *primary* ]]; then
is_primary=true
fi
# Get resolution and position
res_pos=$(echo "$line" | grep -o '[0-9]\+x[0-9]\++[0-9]\++[0-9]\+')
if [[ -n $res_pos ]]; then
# Read the next line (mode list)
IFS= read -r next_line
res=$(echo "$next_line" | awk '{print $1}')
# Determine refresh rate
if [[ "$next_line" == *" + "* ]]; then
rate=$(echo "$next_line" | cut -d'+' -f2 | awk '{print $1}')
else
rate=$(echo "$next_line" | awk -v r="$res" '$1 == r {print $2}')
fi
rate=${rate//\*/}
# Extract X and Y
x_pos=$(echo "$res_pos" | cut -d'+' -f2)
y_pos=$(echo "$res_pos" | cut -d'+' -f3)
# Append monitor to xrandr command
XRANDR_CMD+=" --output $mon --mode $res --rate $rate --pos ${x_pos}x${y_pos}"
$is_primary && XRANDR_CMD+=" --primary"
else
# If disconnected or ignored, turn off
XRANDR_CMD+=" --output $mon --off"
fi
fi
done < <(xrandr -q)
# Save to file
echo "$XRANDR_CMD" >"$OUT"
echo "Saved xrandr command to $OUT"exec_always --no-startup-id "~/.config/i3/scripts/xrandr_command_generator.sh"You can run the .xrandr.conf file automatically when your window manager (WM) starts. This file is created by the script above and contains xrandr commands to set up your display.
To make sure it runs every time you log in, add the following line to your WM's startup file.
For example, if you're using i3wm, add this line to your ~/.config/i3/config:
exec --no-startup-id bash "$HOME/.xrandr.conf"This will run the display setup commands at startup.
To run your display setup script automatically when LightDM starts, follow these steps:
Use this command to locate lightdm.conf:
sudo ls -al /etc/lightdm | grep 'lightdm.conf'Open the file in a text editor like nano:
sudo nano /etc/lightdm/lightdm.confFind the section that looks like this:
[Seat:*]
In this section, look for the line that starts with display-setup-script=,
and replace or add it as follows (remember to replace <your user name>
with your actual username):
display-setup-script=/bin/bash -c '[ -f "/home/<your user name>/.xrandr.conf" ] && bash "/home/<your user name>/.xrandr.conf" || true'✅ This checks if the .xrandr.conf file exists in your home directory and runs it.
If the file doesn't exist or there's an error, it still returns true so LightDM
won't break.
If the display script isn't working, try adjusting this line in the [LightDM]
section of the same file:
logind-check-graphical=trueTry commenting it out (add # at the start of the line) or uncommenting it,
depending on its current state. This can help in some setups.
If LightDM isn't working as expected, check the logs for errors:
sudo cat /var/log/lightdm/lightdm.logOr check the previous log:
sudo cat /var/log/lightdm/lightdm.log.oldsudo cat /var/log/lightdm/x-0.logOr:
sudo cat /var/log/lightdm/x-0.log.oldRun the following commands to install autorandr:
sudo pacman -Syu
sudo pacman -S autorandrTo use autorandr, enable and start it as your regular user:
systemctl --user enable autorandr
systemctl --user start autorandrOr, if needed, enable it as root user (not common):
sudo systemctl enable autorandr
sudo systemctl start autorandrYou can save your current monitor layout into a profile like this:
autorandr -s <profile-name> --forceReplace <profile-name> with something like home, office, etc.
This saves the display setup to:
~/.config/autorandr/<profile-name>/config
This will auto-copy the saved profile to system space for LightDM to use.
Create a new systemd service file:
sudo nano /etc/systemd/system/autorandrlastUsed@.serviceThis version is a better then the last one as it works with
shutdown, reboot and logout. It will also work if you have
separate [/home] and [root] partitions.
Paste the below code:
[Unit]
Description=Copy autorandr lastUsed for %i on logout/shutdown/reboot
DefaultDependencies=no
# Make sure we run before /home unmounts and before shutdown completes
Before=umount.target shutdown.target reboot.target halt.target exit.target
[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=/bin/true
ExecStop=/usr/local/bin/copy-lastused.sh %i
[Install]
WantedBy=multi-user.target
WantedBy=halt.target
WantedBy=reboot.target
WantedBy=shutdown.targetor below is the first version so I have kept it.
only works with shutdown and reboot only if the
[/home] and [root] are under one single partition.
[Unit]
Description=Copy autorandr profile from %i user to LightDM dir
DefaultDependencies=no
Before=shutdown.target reboot.target halt.target
ConditionPathExists=/home/%i/.config/autorandr/<profile-name>
[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=/bin/true
ExecStop=/usr/local/bin/copy-lastused.sh %i
TimeoutStopSec=10
[Install]
WantedBy=multi-user.targetNow enable the service as following
systemctl enable autorandrlastUsed@<your user name>.serviceReplace <profile-name> with your saved profile name.
This script copies your saved profile to /etc/xdg/autorandr/.
Create the script:
sudo nano /usr/local/bin/copy-lastused.shPaste this into the file:
#!/bin/bash
USER="$1"
USER_HOME="/home/$USER"
LASTUSED="$USER_HOME/.config/autorandr/<profile-name>"
DEST_DIR="/etc/xdg/autorandr/"
# For better organization of logs
# LOG_FILE="/var/log/custom/monitors-copy.log"
# else will find logs in default place
LOG_FILE="/var/log/monitors-copy.log"
: >"$LOG_FILE"
log() {
echo "[$(date '+%Y-%m-%d %H:%M:%S')] $1" >>"$LOG_FILE"
# logger -t copy-lastused "$1" # Optional system log
}
log "Running copy for: $USER"
log "Checking at: $LASTUSED"
if [[ -d "$LASTUSED" ]]; then
mkdir -p "$DEST_DIR"
if cp -r "$LASTUSED" "$DEST_DIR"; then
log "Copied: $LASTUSED -> $DEST_DIR"
else
log "ERROR: Failed to copy"
fi
else
log "WARNING: Profile not found at: $LASTUSED"
fiMake sure the folder
/etc/xdg/autorandr/exists, or make one:sudo mkdir -r /etc/xdg/autorandr/
Make sure the folder
/var/log/custom/exists if using the organised version:LOG_FILE="/var/log/custom/monitors-copy.log"
Make the script executable:
chmod +x /usr/local/bin/copy-lastused.shIn your LightDM config file at /etc/lightdm/lightdm.conf,
find the line under [Seat:*] that starts with display-setup-script.
Replace or add this line:
display-setup-script=/bin/bash -c 'autorandr --load <profile-name> || true'🔁 Replace <profile-name> with the name you saved earlier.
See the setup steps for LightDM: Go to LightDM config section️