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

@@ -33,9 +33,8 @@ screen:
workspace:
environment:
MACHINE_TYPE: "workstation"
MACHINE_HAS_KEEPASSX: "false"
MACHINE_HAS_NEXTCLOUD: "true"
MACHINE_HAS_RESTIC_BACKUP: "false"
MACHINE_HAS_SPOTIFY: "true"
MACHINE_HAS_TOUCHPAD: "false"
MACHINE_DEFAULT_SCREENPROFILE: "ares"

View File

@@ -48,7 +48,6 @@ workspace:
10: pim
environment:
MACHINE_TYPE: "laptop"
MACHINE_HAS_KEEPASSX: "true"
MACHINE_HAS_RESTIC_BACKUP: "true"
MACHINE_HAS_SPOTIFY: "true"
MACHINE_HAS_TOUCHPAD: "true"

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

View File

@@ -11,12 +11,12 @@ log() {
case "$1" in
enable)
log "Enabling dunst"
systemctl --user --no-block kill --signal SIGUSR2 user:dunst
systemctl --user --no-block kill --signal SIGUSR2 dunst
;;
disable)
log "Disabling dunst"
systemctl --user --no-block kill --signal SIGUSR1 user:dunst
systemctl --user --no-block kill --signal SIGUSR1 dunst
;;
*)
>&2 printf 'Unknown command\n'

View File

@@ -7,8 +7,6 @@ empty_directories:
- name: .config/gtk-3.0
- name: .config/qt5ct
dotfiles:
- from: autostart/autostart.sh
to: .autostart.sh
- from: git/gitconfig
to: .gitconfig
template: true

View File

@@ -201,8 +201,8 @@ assign [class="^Google-chrome$"] $workspace7
bindsym F2 exec --no-startup-id ~/.i3/scripts/screenmenu
bindsym $mod+F1 exec --no-startup-id ~/.i3/scripts/i3exit lock
bindsym $mod+F2 exec --no-startup-id screencfg ~/.screencfg/all.yml ; exec --no-startup-id ~/.autostart/30_keyboard.sh
bindsym $mod+F3 exec --no-startup-id screencfg ~/.screencfg/laptop.yml ; exec --no-startup-id ~/.autostart/30_keyboard.sh
bindsym $mod+F2 exec --no-startup-id screencfg ~/.screencfg/all.yml ; exec systemctl --user restart keyboard.service
bindsym $mod+F3 exec --no-startup-id screencfg ~/.screencfg/laptop.yml ; exec systemctl --user restart keyboard.service
bindsym $mod+F4 exec --no-startup-id ~/.i3/scripts/i3exit suspend
bindsym $mod+Home exec --no-startup-id ~/.i3/scripts/shutdown-menu

View File

@@ -10,14 +10,15 @@ is_on() {
switch_on() {
touch "${_status_file}"
dunstctl disable &
systemctl --user --no-block stop user:redshift
systemctl --user --no-block stop user:spotify
systemctl --user --no-block stop redshift.service
systemctl --user --no-block stop spotify.service
}
switch_off() {
rm -f "${_status_file}"
dunstctl enable &
"$_autostart" redshift spotify
systemctl --user --no-block start redshift.service
systemctl --user --no-block start spotify.service
}

View File

@@ -447,3 +447,28 @@
tags:
- kubectl
- name: handle autostart units
block:
- name: create systemd user directory
file:
state: directory
path: ~/{{ item }}
loop:
- .config/
- .config/systemd/
- .config/systemd/user/
- name: link autostart service files
file:
state: link
force: true
follow: false
path: "/home/{{ user.name }}/.config/systemd/user/{{ item | basename }}"
src: "{{ item }}"
owner: "{{ user.name }}"
group: "{{ user_group_name }}"
with_fileglob: /var/lib/dotfiles/autostart/services/*
tags:
- autostart

View File

@@ -25,9 +25,8 @@ start_wm() {
}
autostart() {
autostart="$HOME/.autostart.sh"
log "Executing autostart file \"$autostart\""
"$autostart" >>"${LOGFILE}" 2>&1
log "Starting systemd user target \"autostart.target\""
systemctl --user start autostart.target >>"${LOGFILE}" 2>&1
}
exec >> "$LOGFILE" 2>&1

View File

@@ -55,6 +55,9 @@ umask 0022
export {{ k }}="{{ v }}"
{% endfor %}
# Make all environment variables also usable in the systemd user instancee
systemctl --user import-environment
# exec startx breaks some logind fuckery, without exec it works
if [[ -z $DISPLAY ]] ; then
if (( $XDG_VTNR == {{ user.vt }} )) ; then