From 3bfa4a71ffb4457ce092454dfad6938f70028c28 Mon Sep 17 00:00:00 2001 From: Mark Karlinsky <34838287+markK24@users.noreply.github.com> Date: Sun, 4 Jan 2026 09:04:03 +0200 Subject: [PATCH] Improve dinit service files (#3193) * Improve dinit service files and niri-session Two main changes were made: - After a discussion in davmac314/dinit#496, 2 dinit services are now provided. The first one is 'niri', which runs niri itself, and the second one is 'niri.target' which brings up all the dependences from user configuration. - Made the behaviour of 'niri-session' when running under dinit closer to the behaviour when running under systemd. In particular, now the script wait for service completion, because some login managers shut the session down the moment the startup script completes. * Update paths in docs --- docs/wiki/Getting-Started.md | 2 +- docs/wiki/Packaging-niri.md | 2 +- resources/dinit/niri | 15 +++++++-------- resources/dinit/niri-shutdown | 3 --- resources/dinit/niri.target | 6 ++++++ resources/niri-session | 28 ++++++++++++++++++++++++++-- 6 files changed, 41 insertions(+), 15 deletions(-) delete mode 100644 resources/dinit/niri-shutdown create mode 100644 resources/dinit/niri.target diff --git a/docs/wiki/Getting-Started.md b/docs/wiki/Getting-Started.md index c3531934..71515238 100644 --- a/docs/wiki/Getting-Started.md +++ b/docs/wiki/Getting-Started.md @@ -223,7 +223,7 @@ This defaults to `/usr/bin/niri`. | `resources/niri.service` (systemd) | `/etc/systemd/user/` | | `resources/niri-shutdown.target` (systemd) | `/etc/systemd/user/` | | `resources/dinit/niri` (dinit) | `/etc/dinit.d/user/` | -| `resources/dinit/niri-shutdown` (dinit) | `/etc/dinit.d/user/` | +| `resources/dinit/niri.target` (dinit) | `/etc/dinit.d/user/` | [Alacritty]: https://github.com/alacritty/alacritty [fuzzel]: https://codeberg.org/dnkl/fuzzel diff --git a/docs/wiki/Packaging-niri.md b/docs/wiki/Packaging-niri.md index 4cc3b5f5..abc873f2 100644 --- a/docs/wiki/Packaging-niri.md +++ b/docs/wiki/Packaging-niri.md @@ -24,7 +24,7 @@ To do that, put files into the correct directories according to this table. | `resources/niri.service` (systemd) | `/usr/lib/systemd/user/` | | `resources/niri-shutdown.target` (systemd) | `/usr/lib/systemd/user/` | | `resources/dinit/niri` (dinit) | `/usr/lib/dinit.d/user/` | -| `resources/dinit/niri-shutdown` (dinit) | `/usr/lib/dinit.d/user/` | +| `resources/dinit/niri.target` (dinit) | `/usr/lib/dinit.d/user/` | Doing this will make niri appear in GDM and other display managers. diff --git a/resources/dinit/niri b/resources/dinit/niri index b3807b90..07f292bb 100644 --- a/resources/dinit/niri +++ b/resources/dinit/niri @@ -1,8 +1,7 @@ -type = process -command = niri --session -restart = false -working-dir = $HOME -depends-on = dbus -after = niri-shutdown -chain-to = niri-shutdown -options: always-chain \ No newline at end of file +type = process +command = niri --session +restart = false +working-dir = $HOME +ready-notification = pipevar:NOTIFY_FD +logfile = $HOME/.local/share/niri/niri.log +depends-on: dbus diff --git a/resources/dinit/niri-shutdown b/resources/dinit/niri-shutdown deleted file mode 100644 index d609a933..00000000 --- a/resources/dinit/niri-shutdown +++ /dev/null @@ -1,3 +0,0 @@ -type = scripted -command = dinitctl -u setenv WAYLAND_DISPLAY= XDG_SESSION_TYPE= XDG_CURRENT_DESKTOP= NIRI_SOCKET= -restart = false \ No newline at end of file diff --git a/resources/dinit/niri.target b/resources/dinit/niri.target new file mode 100644 index 00000000..aea7f6cc --- /dev/null +++ b/resources/dinit/niri.target @@ -0,0 +1,6 @@ +type = internal +restart = false +depends-on: niri +waits-for.d: $XDG_CONFIG_HOME/dinit.d/niri.d/ +waits-for.d: $HOME/.config/dinit.d/niri.d/ +waits-for.d: /etc/dinit.d/user/niri.d/ diff --git a/resources/niri-session b/resources/niri-session index 49dc4c94..e0c71ffe 100755 --- a/resources/niri-session +++ b/resources/niri-session @@ -59,13 +59,37 @@ elif hash dinitctl >/dev/null 2>&1; then fi # Make sure there's no already running session. - if dinitctl --user is-started niri >/dev/null 2>&1; then + if dinitctl --quiet --user is-started niri 2>/dev/null; then echo 'A niri session is already running.' exit 1 fi + # Import the login manager environment into dinit + # Might not work correctly for multiline variable names, but + # it is reasonable to assume there are none + awk 'BEGIN{for(v in ENVIRON) if (v != "AWKPATH" && v != "AWKLIBPATH") print v}' 2>/dev/null | xargs dinitctl --quiet --user setenv 2>/dev/null + + # Usually the dbus service would start as niri's dependency and inherit + # environment from dinit, but in case it has already started we need + # to update its environment. + if hash dbus-update-activation-environment >/dev/null 2>&1; then + dbus-update-activation-environment --all >/dev/null 2>&1 + fi + + # Create the directory for the logfile, if doesn't exist + mkdir --parents $HOME/.local/share/niri # Start niri - dinitctl --user start niri + dinitctl --quiet --user start niri.target 2>&1 + + # Wait for termination + dinit-monitor --user --initial -c $'sh -c " + if [ "%s" = "stopped" ] || [ "%s" = "failed" ]; then + ppid=$(ps -o ppid= -p $$) + kill $ppid + fi"' niri >/dev/null 2>&1 + + # Unset environment that we've set. + dinitctl --quiet --user unsetenv WAYLAND_DISPLAY DISPLAY XDG_SESSION_TYPE XDG_CURRENT_DESKTOP NIRI_SOCKET 2>/dev/null else echo "No systemd or dinit detected, please use niri --session instead." fi