Use persistent systemd units for autostart

This commit is contained in:
2020-12-20 20:36:27 +01:00
parent f4fbae6683
commit 0f36baf30c
31 changed files with 194 additions and 182 deletions

View File

@@ -1,166 +0,0 @@
#!/usr/bin/env bash
set -o nounset
# set -x
selective=0
if (( $# > 0 )) ; then
selective=1
services=("${@}")
fi
do_run() {
name="$1"
shift
run=0
if (( $selective )) ; then
for s in "${services[@]}" ; do
if [[ "$s" == "$name" ]] ; then
run=1
fi
done
else
run=1
fi
return $(( ! $run ))
}
get_unit_name() {
name="$1" ; shift
printf 'user:%s' "${name}"
}
run_raw() {
name="$1"
shift
do_run "$name" || return
systemd-run \
--user \
--unit "$(get_unit_name "${name}")" \
--no-block \
--remain-after-exit \
--setenv=DISPLAY=${DISPLAY} \
"${@}"
}
run() {
name="$1"
shift
run_raw "$name" --property=Restart=always "${@}"
}
run_oneshot_multiple() {
name="$1"
shift
cmd=()
# hacky way to start two processes in oneshot mode
for c in "${@}" ; do
cmd+=(--property "ExecStart=/usr/bin/env ${c}")
done
cmd+=(true)
run_oneshot "$name" "${cmd[@]}"
}
run_oneshot() {
name="$1"
shift
run_raw "$name" --service-type=oneshot "${@}"
}
schedule() {
name="$1"; shift
do_run "$name" || return
systemd-run \
--user \
--unit "$(get_unit_name "${name}")" \
--no-block \
--setenv=DISPLAY=${DISPLAY} \
"${@}"
}
# wallpaper config
wallpaper="$LIBDIR/wallpaper/current"
# keyboard settings
keyboard_layout=de
keyboard_variant=nodeadkeys
keyboard_repeat_delay=150
keyboard_repeat_speed=50
run_oneshot acpi bash -c 'grep "^${ACPI_LID_NAME}.*enabled" /proc/acpi/wakeup && echo " ${ACPI_LID_NAME}" | sudo tee /proc/acpi/wakeup'
run_oneshot xresources xrdb -merge -I${HOME} ~/.Xresources
run_oneshot_multiple keyboard \
"setxkbmap -layout $keyboard_layout -variant $keyboard_variant" \
"xset r rate $keyboard_repeat_delay $keyboard_repeat_speed"
if [[ "${MACHINE_HAS_TOUCHPAD}" == "true" ]] ; then
run_oneshot_multiple touchpad \
"synclient VertEdgeScroll=0" \
"synclient VertTwoFingerScroll=1" \
"synclient MaxSpeed=2.2" \
"synclient AccelFactor=0.08" \
"synclient TapButton1=1" \
"synclient CoastingSpeed=0" \
"synclient PalmDetect=1" \
"synclient PalmMinWidth=20" \
"synclient PalmMinZ=180"
fi
run gpg-agent gpg-agent --homedir "$HOME/.gnupg" --no-detach --daemon
# run gnome-keyring_user gnome-keyring-daemon --start --components pkcs11,secrets --foreground
run kwallet kwalletd5
# a service called dunst already exists and conflicts
run dunst dunst -config ~/.config/dunstrc
# disabled due to firefox flicker
# run compton compton --backend glx --vsync opengl --no-dock-shadow --no-dnd-shadow
schedule wallpaper --on-active=1s --on-calendar hourly \
setrandom --recursive --onlylarge --mode=stretch /usr/share/wallpapers
run blueman blueman-applet
run nm-applet nm-applet
run pasystray pasystray
# redshift unit already exists
run redshift redshift-gtk -c ~/.config/redshift.conf
run yubikey-touch-detector yubikey-touch-detector -libnotify
if [[ "${MACHINE_DEFAULT_SCREENPROFILE}" ]] ; then
run screencfg "$HOME/.screencfg/${MACHINE_DEFAULT_SCREENPROFILE}.sh"
fi
if [[ "${MACHINE_HAS_KEEPASSX}" == "true" ]] ; then
run keepassx keepassx --keyfile ~/.secret/main.key ~/.secret/main.kdbx
fi
if [[ "${MACHINE_HAS_SPOTIFY}" == "true" ]] ; then
run spotify spotify
fi
if [[ "${MACHINE_HAS_NEXTCLOUD}" == "true" ]] ; then
run nextcloud nextcloud --background
fi
if [[ "${MACHINE_HAS_RESTIC_BACKUP}" == "true" ]] ; then
[[ -x ~/bin/restic-backup ]] && schedule restic-backup --on-calendar "Mon..Fri 12:00:00" --on-calendar "Mon..Fri 09:00:00" --on-calendar "Mon..Fri 16:00:00" ~/bin/restic-backup
fi
run firefox firefox -P default

View File

@@ -0,0 +1,19 @@
[Unit]
Wants=blueman.service
Wants=dunst.service
Wants=firefox.service
Wants=gpg-agent.service
Wants=keepassx.service
Wants=keyboard.service
Wants=laptop-lid.service
Wants=nextcloud.service
Wants=nm-applet.service
Wants=pasystray.service
Wants=redshift.service
Wants=restic.timer
Wants=screencfg.service
Wants=spotify.service
Wants=touchpad.service
Wants=wallpaper.timer
Wants=xresources.service
Wants=yubikey-touch-detector.service

View File

@@ -0,0 +1,5 @@
[Unit]
BindsTo=autostart.target
[Service]
ExecStart=/usr/bin/blueman-applet

View File

@@ -0,0 +1,5 @@
[Unit]
BindsTo=autostart.target
[Service]
ExecStart=/usr/bin/dunst -config %h/.config/dunstrc

View File

@@ -0,0 +1,5 @@
[Unit]
BindsTo=autostart.target
[Service]
ExecStart=/usr/bin/firefox --setDefaultBrowser -P default

View File

@@ -0,0 +1,6 @@
[Unit]
BindsTo=autostart.target
[Service]
Type=forking
ExecStart=/usr/bin/gpg-agent --homedir %h/.gnupg --no-detach --daemon

View File

@@ -0,0 +1,6 @@
[Unit]
BindsTo=autostart.target
ConditionEnvironment=MACHINE_HAS_KEEPASSX=true
[Service]
ExecStart=/usr/bin/keepassx --keyfile %h/.secret/main.key %h/.secret/main.kdbx

View File

@@ -0,0 +1,8 @@
[Unit]
BindsTo=autostart.target
[Service]
Type=oneshot
ExecStart=/usr/bin/setxkbmap -layout de -variant nodeadkeys
ExecStart=/usr/bin/xset r rate 150 50
RemainAfterExit=true

View File

@@ -0,0 +1,7 @@
[Unit]
BindsTo=autostart.target
ConditionEnvironment=MACHINE_TYPE=laptop
[Service]
ExecStart=/usr/bin/bash -c 'grep "^${ACPI_LID_NAME}.*enabled" /proc/acpi/wakeup && echo " ${ACPI_LID_NAME}" | sudo tee /proc/acpi/wakeup'

View File

@@ -0,0 +1,7 @@
[Unit]
BindsTo=autostart.target
ConditionEnvironment=MACHINE_HAS_NEXTCLOUD=true
[Service]
ExecStart=/usr/bin/nextcloud --background

View File

@@ -0,0 +1,5 @@
[Unit]
BindsTo=autostart.target
[Service]
ExecStart=/usr/bin/nm-applet

View File

@@ -0,0 +1,5 @@
[Unit]
BindsTo=autostart.target
[Service]
ExecStart=/usr/bin/pasystray

View File

@@ -0,0 +1,5 @@
[Unit]
BindsTo=autostart.target
[Service]
ExecStart=/usr/bin/redshift-gtk -c %h/.config/redshift.conf

View File

@@ -0,0 +1,7 @@
[Unit]
ConditionEnvironment=MACHINE_HAS_RESTIC_BACKUP=true
[Service]
Type=oneshot
ExecStart=%h/bin/restic-backup
RemainAfterExit=true

View File

@@ -0,0 +1,7 @@
[Unit]
BindsTo=autostart.target
[Timer]
OnCalendar=Mon..Fri 09:00:00
OnCalendar=Mon..Fri 12:00:00
OnCalendar=Mon..Fri 16:00:00

View File

@@ -0,0 +1,12 @@
[Unit]
BindsTo=autostart.target
ConditionEnvironment=MACHINE_DEFAULT_SCREENPROFILE
[Service]
Type=oneshot
# systemd.service(5):
# > The command to execute may contain spaces, but control characters are not
# > allowed.
ExecStart=bash -c '%h/.screencfg/${MACHINE_DEFAULT_SCREENPROFILE}.sh'
RemainAfterExit=true

View File

@@ -0,0 +1,5 @@
[Unit]
BindsTo=autostart.target
[Service]
ExecStart=/usr/bin/spotify

View File

@@ -0,0 +1,16 @@
[Unit]
BindsTo=autostart.target
ConditionEnvironment=MACHINE_TYPE=laptop
[Service]
Type=oneshot
ExecStart=/usr/bin/synclient VertEdgeScroll=0
ExecStart=/usr/bin/synclient VertTwoFingerScroll=1
ExecStart=/usr/bin/synclient MaxSpeed=2.2
ExecStart=/usr/bin/synclient AccelFactor=0.08
ExecStart=/usr/bin/synclient TapButton1=1
ExecStart=/usr/bin/synclient CoastingSpeed=0
ExecStart=/usr/bin/synclient PalmDetect=1
ExecStart=/usr/bin/synclient PalmMinWidth=20
ExecStart=/usr/bin/synclient PalmMinZ=1
RemainAfterExit=true

View File

@@ -0,0 +1,4 @@
[Service]
Type=oneshot
ExecStart=/usr/bin/setrandom --recursive --onlylarge --mode=stretch /usr/share/wallpapers
RemainAfterExit=true

View File

@@ -0,0 +1,8 @@
[Unit]
BindsTo=autostart.target
[Timer]
OnActiveSec=0s
OnCalendar=hourly
Unit=wallpaper.service
RemainAfterElapse=true

View File

@@ -0,0 +1,7 @@
[Unit]
BindsTo=autostart.target
[Service]
Type=oneshot
ExecStart=/usr/bin/xrdb -merge -I%h %h/.Xresources
RemainAfterExit=true

View File

@@ -0,0 +1,5 @@
[Unit]
BindsTo=autostart.target
[Service]
ExecStart=/usr/bin/yubikey-touch-detector -libnotify