Set default volume and brightness at login

Add three `systemd` user units that reset
volume, screen brightness, and keyboard backlight,
so that every session starts from a known state.

Values come from ~/.config/environment.d/50-defaults.conf,
and can be overridden per machine via an untracked 90-local.conf
This commit is contained in:
Alexander Hess 2026-08-28 15:06:04 +02:00
commit 67c14ddb25
Signed by: alexander
GPG key ID: B46EDB4DFC26805D
9 changed files with 158 additions and 0 deletions

View file

@ -0,0 +1,19 @@
# Session defaults, read by `systemd --user` at login
#
# Applied by:
# - set-brightness-default.service
# - set-keyboard-brightness-default.service
# - set-volume-default.service
#
# For machine-local overrides create an untracked
# ~/.config/environment.d/90-local.conf instead
# and repeat only the keys you want to change
#
# Apply with: `systemctl --user daemon-reload`,
# then log out and back in
#
# Verify with: `systemctl --user show-environment`
DEFAULT_BRIGHTNESS=30
DEFAULT_KEYBOARD_BRIGHTNESS=50
DEFAULT_VOLUME=30

View file

@ -0,0 +1,6 @@
# Session Environment Defaults
This folder contains environment variables
read by `systemd --user` at login.
Machine-local overrides go into an untracked `90-local.conf`.

View file

@ -0,0 +1,13 @@
[Unit]
Description=Set default screen brightness
After=graphical-session.target
ConditionPathExistsGlob=/sys/class/backlight/*
PartOf=graphical-session.target
[Service]
Type=oneshot
ExecStart=%h/.local/bin/set-brightness-default
TimeoutStartSec=30
[Install]
WantedBy=graphical-session.target

View file

@ -0,0 +1,12 @@
[Unit]
Description=Set default keyboard backlight brightness
After=graphical-session.target
PartOf=graphical-session.target
[Service]
Type=oneshot
ExecStart=%h/.local/bin/set-keyboard-brightness-default
TimeoutStartSec=30
[Install]
WantedBy=graphical-session.target

View file

@ -0,0 +1,13 @@
[Unit]
Description=Set default output volume
After=wireplumber.service
PartOf=graphical-session.target
Wants=wireplumber.service
[Service]
Type=oneshot
ExecStart=%h/.local/bin/set-volume-default
TimeoutStartSec=30
[Install]
WantedBy=graphical-session.target

View file

@ -176,6 +176,9 @@ fi
if systemctl --user daemon-reload 2>/dev/null; then
systemctl --user enable gnome-restore.service
systemctl --user enable night-light-reset.timer
systemctl --user enable set-brightness-default.service
systemctl --user enable set-keyboard-brightness-default.service
systemctl --user enable set-volume-default.service
systemctl --user enable xcape.service
else
echo "WARN: no systemd user session, units not enabled" >&2

View file

@ -0,0 +1,20 @@
#!/bin/sh
set -eu
# Loaded from ~/.config/environment.d/50-defaults.conf or a local override
BRIGHTNESS="${DEFAULT_BRIGHTNESS:?unset, check ~/.config/environment.d/}"
gdbus wait --session --timeout 10 org.gnome.SettingsDaemon.Power
gdbus call --session \
--dest org.gnome.SettingsDaemon.Power \
--object-path /org/gnome/SettingsDaemon/Power \
--method org.freedesktop.DBus.Properties.Set \
org.gnome.SettingsDaemon.Power.Screen Brightness "<int32 $BRIGHTNESS>" \
>/dev/null
echo "Set screen brightness to $BRIGHTNESS"

View file

@ -0,0 +1,47 @@
#!/bin/sh
set -eu
# Loaded from ~/.config/environment.d/50-defaults.conf or a local override
BRIGHTNESS="${DEFAULT_KEYBOARD_BRIGHTNESS:?unset, check ~/.config/environment.d/}"
# Nothing to do on machines without a keyboard backlight
KBD_DEVICE=
for led in /sys/class/leds/*kbd_backlight*; do
[ -e "$led" ] || break
KBD_DEVICE=$led
break
done
if [ -z "$KBD_DEVICE" ]; then
echo "No keyboard backlight, skipping"
exit 0
fi
kbd_set() {
gdbus call --session \
--dest org.gnome.SettingsDaemon.Power \
--object-path /org/gnome/SettingsDaemon/Power \
--method org.freedesktop.DBus.Properties.Set \
org.gnome.SettingsDaemon.Power.Keyboard Brightness "<int32 $BRIGHTNESS>" \
>/dev/null
}
gdbus wait --session --timeout 10 org.gnome.SettingsDaemon.Power
# `gsd-power` owns its bus name before it has probed the keyboard
# backlight, so at login the first write may silently be discarded
kbd_set
if [ "$(cat "$KBD_DEVICE/brightness")" = "0" ]; then
sleep 3
kbd_set
fi
echo "Set keyboard brightness to $BRIGHTNESS"

25
.local/bin/set-volume-default Executable file
View file

@ -0,0 +1,25 @@
#!/bin/sh
set -eu
# Loaded from ~/.config/environment.d/50-defaults.conf or a local override
VOLUME="${DEFAULT_VOLUME:?unset, check ~/.config/environment.d/}"
i=0
while ! wpctl status >/dev/null 2>&1; do
i=$((i + 1))
if [ "$i" -ge 20 ]; then
echo "no PipeWire sink after 10s" >&2
exit 1
fi
sleep 0.5
done
wpctl set-volume @DEFAULT_AUDIO_SINK@ "$VOLUME%"
wpctl set-mute @DEFAULT_AUDIO_SINK@ 0
echo "Set volume to $VOLUME%"