forge
Explore
Log in

Don't you dare go Hollow

Add --session CLI flag instead of detection based on systemd service

Browse files at this commit

f0a5e9c9332624aa7ea201c8c83f4f20b0a9775fIvan MolodetskikhParent: c4c0784
Allows running without systemd.
resources/niri.servicemodified+1 -1
@@ -11,4 +11,4 @@ Before=xdg-desktop-autostart.target
1111 [Service]
1212 Slice=session.slice
1313 Type=notify
14-ExecStart=/usr/bin/niri
14+ExecStart=/usr/bin/niri --session
src/cli.rsmodified+7 -0
@@ -15,6 +15,13 @@ pub struct Cli {
1515 /// Path to config file (default: `$XDG_CONFIG_HOME/niri/config.kdl`).
1616 #[arg(short, long)]
1717 pub config: Option<PathBuf>,
18+ /// Import environment globally to systemd and D-Bus, run D-Bus services.
19+ ///
20+ /// Set this flag in a systemd service started by your display manager, or when running
21+ /// manually as your main compositor instance. Do not set when running as a nested window, or
22+ /// on a TTY as your non-main compositor instance, to avoid messing up the global environment.
23+ #[arg(long)]
24+ pub session: bool,
1825 /// Command to run upon compositor startup.
1926 #[arg(last = true)]
2027 pub command: Vec<OsString>,
src/main.rsmodified+14 -15
@@ -38,8 +38,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
3838 REMOVE_ENV_RUST_LIB_BACKTRACE.store(true, Ordering::Relaxed);
3939 }
4040
41- let is_systemd_service = env::var_os("NOTIFY_SOCKET").is_some();
42- IS_SYSTEMD_SERVICE.store(is_systemd_service, Ordering::Relaxed);
41+ IS_SYSTEMD_SERVICE.store(env::var_os("NOTIFY_SOCKET").is_some(), Ordering::Relaxed);
4342
4443 let directives = env::var("RUST_LOG").unwrap_or_else(|_| "niri=debug".to_owned());
4544 let env_filter = EnvFilter::builder().parse_lossy(directives);
@@ -48,22 +47,22 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
4847 .with_env_filter(env_filter)
4948 .init();
5049
51- if is_systemd_service {
52- // If we're starting as a systemd service, assume that the intention is to start on a TTY.
53- // Remove DISPLAY or WAYLAND_DISPLAY from our environment if they are set, since they will
54- // cause the winit backend to be selected instead.
50+ let cli = Cli::parse();
51+
52+ if cli.session {
53+ // If we're starting as a session, assume that the intention is to start on a TTY. Remove
54+ // DISPLAY or WAYLAND_DISPLAY from our environment if they are set, since they will cause
55+ // the winit backend to be selected instead.
5556 if env::var_os("DISPLAY").is_some() {
56- debug!("we're running as a systemd service but DISPLAY is set, removing it");
57+ debug!("running as a session but DISPLAY is set, removing it");
5758 env::remove_var("DISPLAY");
5859 }
5960 if env::var_os("WAYLAND_DISPLAY").is_some() {
60- debug!("we're running as a systemd service but WAYLAND_DISPLAY is set, removing it");
61+ debug!("running as a session but WAYLAND_DISPLAY is set, removing it");
6162 env::remove_var("WAYLAND_DISPLAY");
6263 }
6364 }
6465
65- let cli = Cli::parse();
66-
6766 let _client = tracy_client::Client::start();
6867
6968 // Set a better error printer for config loading.
@@ -177,9 +176,9 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
177176 info!("IPC listening on: {}", ipc.socket_path.to_string_lossy());
178177 }
179178
180- if is_systemd_service {
181- // We're starting as a systemd service. Export our variables.
182- import_env_to_systemd();
179+ if cli.session {
180+ // We're starting as a session. Import our variables.
181+ import_environment();
183182
184183 // Inhibit power key handling so we can suspend on it.
185184 #[cfg(feature = "dbus")]
@@ -191,7 +190,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
191190 }
192191
193192 #[cfg(feature = "dbus")]
194- dbus::DBusServers::start(&mut state, is_systemd_service);
193+ dbus::DBusServers::start(&mut state, cli.session);
195194
196195 // Notify systemd we're ready.
197196 if let Err(err) = sd_notify::notify(true, &[NotifyState::Ready]) {
@@ -236,7 +235,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
236235 Ok(())
237236 }
238237
239-fn import_env_to_systemd() {
238+fn import_environment() {
240239 let variables = ["WAYLAND_DISPLAY", niri_ipc::SOCKET_PATH_ENV].join(" ");
241240
242241 let rv = Command::new("/bin/sh")