From 7c0bd5738f9bae3b26dee7439b65b3a05fa4dbb9 Mon Sep 17 00:00:00 2001
From: Marty Oehme <contact@martyoeh.me>
Date: Mon, 24 Feb 2025 16:59:02 +0100
Subject: [PATCH] river: Change to runit-managed user service

Switching to runit (and turnstiled, which in turn activates user-local
runit service supervision) we can now have river run supervised by our
service manager quite easily.

We make use of this, but have to take care to export the
river-established wayland variables (DISPLAY, WAYLAND_DISPLAY) back into
the user environment for other processes since river will not be the
one responsible for spawning them anymore.

On finishing the service (i.e. exiting the process), we ensure
that the variables get removed from the environment again so it is not
polluted in the future.

Additionally, we load the (default for void runit) 'turnstile-ready'
service which can define core user services that need to exist before
others on login, and the session-local dbus service which river and
other programs will make use of. It is marked as essential with
turnstile-ready.
---
 desktop/.config/river/init       |  8 +++++++-
 services/sv/dbus/log/run         |  2 ++
 services/sv/dbus/run             |  1 +
 services/sv/river/finish         |  6 ++++++
 services/sv/river/log/run        |  3 +++
 services/sv/river/run            | 10 ++++++++++
 services/sv/turnstile-ready/conf |  1 +
 services/sv/turnstile-ready/run  |  5 +++++
 8 files changed, 35 insertions(+), 1 deletion(-)
 create mode 100755 services/sv/dbus/log/run
 create mode 120000 services/sv/dbus/run
 create mode 100755 services/sv/river/finish
 create mode 100755 services/sv/river/log/run
 create mode 100755 services/sv/river/run
 create mode 100644 services/sv/turnstile-ready/conf
 create mode 100755 services/sv/turnstile-ready/run

diff --git a/desktop/.config/river/init b/desktop/.config/river/init
index c219a99..6063cd2 100755
--- a/desktop/.config/river/init
+++ b/desktop/.config/river/init
@@ -1,8 +1,14 @@
 #!/usr/bin/env sh
 
+# turnstiled user service setup
+if [ -d "$TURNSTILE_ENV_DIR" ]; then
+    echo "$DISPLAY" >"$TURNSTILE_ENV_DIR/DISPLAY"
+    echo "$WAYLAND_DISPLAY" >"$TURNSTILE_ENV_DIR/WAYLAND_DISPLAY"
+fi
+
 mod="Mod4"
 modemod="Mod1"
-term=${TERMINAL:-foot}
+term=${TERMINAL:-wezterm}
 time_to_lockscreen=300
 time_to_screendim=600
 time_to_suspend=900
diff --git a/services/sv/dbus/log/run b/services/sv/dbus/log/run
new file mode 100755
index 0000000..3020c45
--- /dev/null
+++ b/services/sv/dbus/log/run
@@ -0,0 +1,2 @@
+#!/bin/sh
+exec vlogger -t dbus_user -p daemon
diff --git a/services/sv/dbus/run b/services/sv/dbus/run
new file mode 120000
index 0000000..b786362
--- /dev/null
+++ b/services/sv/dbus/run
@@ -0,0 +1 @@
+/usr/share/examples/turnstile/dbus.run
\ No newline at end of file
diff --git a/services/sv/river/finish b/services/sv/river/finish
new file mode 100755
index 0000000..a4e7c74
--- /dev/null
+++ b/services/sv/river/finish
@@ -0,0 +1,6 @@
+#!/usr/bin/env sh
+
+if [ -d "$TURNSTILE_ENV_DIR" ]; then
+    [ -r "$TURNSTILE_ENV_DIR/DISPLAY" ] && rm "$TURNSTILE_ENV_DIR/DISPLAY"
+    [ -r "$TURNSTILE_ENV_DIR/WAYLAND_DISPLAY" ] && rm "$TURNSTILE_ENV_DIR/WAYLAND_DISPLAY"
+fi
diff --git a/services/sv/river/log/run b/services/sv/river/log/run
new file mode 100755
index 0000000..596db87
--- /dev/null
+++ b/services/sv/river/log/run
@@ -0,0 +1,3 @@
+#!/usr/bin/env sh
+
+exec vlogger -t river -p daemon
diff --git a/services/sv/river/run b/services/sv/river/run
new file mode 100755
index 0000000..0bc4d0b
--- /dev/null
+++ b/services/sv/river/run
@@ -0,0 +1,10 @@
+#!/bin/sh
+
+[ -r ./conf ] && . ./conf
+
+if [ -d "$TURNSTILE_ENV_DIR" ]; then
+    echo "$HOME/.local/bin:$PATH" > "$TURNSTILE_ENV_DIR/PATH"
+fi
+
+exec 2>&1
+exec chpst -e "$TURNSTILE_ENV_DIR" river -log-level warning
diff --git a/services/sv/turnstile-ready/conf b/services/sv/turnstile-ready/conf
new file mode 100644
index 0000000..68063d2
--- /dev/null
+++ b/services/sv/turnstile-ready/conf
@@ -0,0 +1 @@
+core_services="dbus"
diff --git a/services/sv/turnstile-ready/run b/services/sv/turnstile-ready/run
new file mode 100755
index 0000000..5b6d120
--- /dev/null
+++ b/services/sv/turnstile-ready/run
@@ -0,0 +1,5 @@
+#!/bin/sh
+[ -r ./conf ] && . ./conf
+[ -n "$core_services" ] && SVDIR=".." sv start $core_services
+[ -p "/run/turnstiled/1000/ready" ] && printf "/home/marty/.local/state/service/turnstile-ready" > "/run/turnstiled/1000/ready"
+exec pause