From 1c3bb35bdef032847a38eb554a8fd1146ab8b2c2 Mon Sep 17 00:00:00 2001 From: Alexander Hess Date: Fri, 28 Aug 2026 15:06:04 +0200 Subject: [PATCH] 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 --- .config/environment.d/50-defaults.conf | 19 ++++++++ .config/environment.d/README.md | 6 +++ .../user/set-brightness-default.service | 13 +++++ .../set-keyboard-brightness-default.service | 12 +++++ .../systemd/user/set-volume-default.service | 13 +++++ .local/bin/dotfiles-install | 3 ++ .local/bin/set-brightness-default | 20 ++++++++ .local/bin/set-keyboard-brightness-default | 47 +++++++++++++++++++ .local/bin/set-volume-default | 25 ++++++++++ 9 files changed, 158 insertions(+) create mode 100644 .config/environment.d/50-defaults.conf create mode 100644 .config/environment.d/README.md create mode 100644 .config/systemd/user/set-brightness-default.service create mode 100644 .config/systemd/user/set-keyboard-brightness-default.service create mode 100644 .config/systemd/user/set-volume-default.service create mode 100755 .local/bin/set-brightness-default create mode 100755 .local/bin/set-keyboard-brightness-default create mode 100755 .local/bin/set-volume-default diff --git a/.config/environment.d/50-defaults.conf b/.config/environment.d/50-defaults.conf new file mode 100644 index 0000000..2f8681c --- /dev/null +++ b/.config/environment.d/50-defaults.conf @@ -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 diff --git a/.config/environment.d/README.md b/.config/environment.d/README.md new file mode 100644 index 0000000..2b9fe39 --- /dev/null +++ b/.config/environment.d/README.md @@ -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`. diff --git a/.config/systemd/user/set-brightness-default.service b/.config/systemd/user/set-brightness-default.service new file mode 100644 index 0000000..3e0027d --- /dev/null +++ b/.config/systemd/user/set-brightness-default.service @@ -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 diff --git a/.config/systemd/user/set-keyboard-brightness-default.service b/.config/systemd/user/set-keyboard-brightness-default.service new file mode 100644 index 0000000..5cfc12d --- /dev/null +++ b/.config/systemd/user/set-keyboard-brightness-default.service @@ -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 diff --git a/.config/systemd/user/set-volume-default.service b/.config/systemd/user/set-volume-default.service new file mode 100644 index 0000000..79ed9b7 --- /dev/null +++ b/.config/systemd/user/set-volume-default.service @@ -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 diff --git a/.local/bin/dotfiles-install b/.local/bin/dotfiles-install index f9ce1ba..b0f6860 100755 --- a/.local/bin/dotfiles-install +++ b/.local/bin/dotfiles-install @@ -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 diff --git a/.local/bin/set-brightness-default b/.local/bin/set-brightness-default new file mode 100755 index 0000000..1fbb568 --- /dev/null +++ b/.local/bin/set-brightness-default @@ -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 "" \ + >/dev/null + + +echo "Set screen brightness to $BRIGHTNESS" diff --git a/.local/bin/set-keyboard-brightness-default b/.local/bin/set-keyboard-brightness-default new file mode 100755 index 0000000..86a0c53 --- /dev/null +++ b/.local/bin/set-keyboard-brightness-default @@ -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 "" \ + >/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" diff --git a/.local/bin/set-volume-default b/.local/bin/set-volume-default new file mode 100755 index 0000000..cc4d748 --- /dev/null +++ b/.local/bin/set-volume-default @@ -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%"