forge
Explore
Log in

Don't you dare go Hollow

niri

Clone
★ 0

0 watching0 forksRustGPL-3.012.1 MiB9d8f4f8Ivan Molodetskikh1 month ago2997 commits

niri-session110 lines, 4.1 KiBHistoryRawDownload
1#!/bin/sh
2
3# Detect if being run as a user service, which implies external session management,
4# exec compositor directly
5if [ -n "${MANAGERPID:-}" ] && [ "${SYSTEMD_EXEC_PID:-}" = "$$" ]; then
6 case "$(ps -p "$MANAGERPID" -o cmd=)" in
7 *systemd*--user*)
8 exec niri --session
9 ;;
10 esac
11fi
12
13if [ -n "$SHELL" ] &&
14 grep -q "$SHELL" /etc/shells &&
15 ! (echo "$SHELL" | grep -q "false") &&
16 ! (echo "$SHELL" | grep -q "nologin"); then
17 if [ "$1" != '-l' ]; then
18 exec "$SHELL" -l -c "exec $0 -l $*"
19 else
20 shift
21 fi
22fi
23
24# Try to detect the service manager that is being used
25if hash systemctl >/dev/null 2>&1; then
26 # Make sure there's no already running session.
27 if systemctl --user -q is-active niri.service; then
28 echo 'A niri session is already running.'
29 exit 1
30 fi
31
32 # Reset failed state of all user units.
33 systemctl --user reset-failed
34
35 # Import the login manager environment.
36 #
37 # We intentionally import *all* environment variables.
38 # It's unfortunately commonplace for distros to initialize
39 # things with shell scripts and set environment variables.
40 #
41 # For example, debuginfod is initialized this way, and sets the
42 # $DEBUGINFOD_URLS environment variable. If we don't export it,
43 # then programs started from a systemd service (e.g. from xdg-autostart)
44 # will be unable to use debuginfod.
45 #
46 # We cannot possibly know and list all variables like this, so
47 # we import all of them into the environment. This also covers
48 # any variables the user may have customized, such as $PATH.
49 if hash dbus-update-activation-environment 2>/dev/null; then
50 # The `--systemd` flag also sets the variables for systemd user services,
51 # so we don't need a separate `systemctl --user import-environment` call.
52 #
53 # (This also avoids the deprecation warning printed by
54 # a blanket `systemctl --user import-environment` that
55 # some people find very annoying.)
56 dbus-update-activation-environment --systemd --all
57 else
58 systemctl --user import-environment
59 fi
60
61 # Start niri and wait for it to terminate.
62 systemctl --user --wait start niri.service
63
64 # Force stop of graphical-session.target.
65 systemctl --user start --job-mode=replace-irreversibly niri-shutdown.target
66
67 # Unset environment that we've set.
68 systemctl --user unset-environment WAYLAND_DISPLAY DISPLAY XDG_SESSION_TYPE XDG_CURRENT_DESKTOP NIRI_SOCKET
69elif hash dinitctl >/dev/null 2>&1; then
70 # Check that the user dinit daemon is running
71 if ! pgrep -u "$(id -u)" dinit >/dev/null 2>&1; then
72 echo "dinit user daemon is not running."
73 exit 1
74 fi
75
76 # Make sure there's no already running session.
77 if dinitctl --quiet --user is-started niri 2>/dev/null; then
78 echo 'A niri session is already running.'
79 exit 1
80 fi
81
82 # Import the login manager environment into dinit
83 # Might not work correctly for multiline variable names, but
84 # it is reasonable to assume there are none
85 awk 'BEGIN{for(v in ENVIRON) if (v != "AWKPATH" && v != "AWKLIBPATH") print v}' 2>/dev/null | xargs dinitctl --quiet --user setenv 2>/dev/null
86
87 # Usually the dbus service would start as niri's dependency and inherit
88 # environment from dinit, but in case it has already started we need
89 # to update its environment.
90 if hash dbus-update-activation-environment >/dev/null 2>&1; then
91 dbus-update-activation-environment --all >/dev/null 2>&1
92 fi
93
94 # Create the directory for the logfile, if doesn't exist
95 mkdir --parents "$HOME/.local/share/niri"
96 # Start niri
97 dinitctl --quiet --user start niri.target 2>&1
98
99 # Wait for termination
100 dinit-monitor --user --initial -c 'sh -c "
101 if [ "%s" = "stopped" ] || [ "%s" = "failed" ]; then
102 ppid=$(ps -o ppid= -p $$)
103 kill "$ppid"
104 fi"' niri >/dev/null 2>&1
105
106 # Unset environment that we've set.
107 dinitctl --quiet --user unsetenv WAYLAND_DISPLAY DISPLAY XDG_SESSION_TYPE XDG_CURRENT_DESKTOP NIRI_SOCKET 2>/dev/null
108else
109 echo "No systemd or dinit detected, please use niri --session instead."
110fi