From d58a45a96c2fce2602224cd246b3576d0c975cf3 Mon Sep 17 00:00:00 2001 From: Ivan Molodetskikh Date: Tue, 20 Feb 2024 13:54:16 +0400 Subject: [PATCH] Add systemd feature flag for systemd-specific things --- Cargo.toml | 6 ++++-- src/main.rs | 19 ++++++++++++++++--- src/niri.rs | 2 -- src/utils.rs | 4 ++-- 4 files changed, 22 insertions(+), 9 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index cd2e2b3..292dd5f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -97,9 +97,11 @@ proptest-derive = "0.4.0" xshell = "0.2.5" [features] -default = ["dbus", "xdp-gnome-screencast"] -# Enables DBus support (required for xdp-gnome and power button inhibiting). +default = ["dbus", "systemd", "xdp-gnome-screencast"] +# Enables D-Bus support (serve various freedesktop and GNOME interfaces, power button handling). dbus = ["zbus", "async-channel", "async-io", "notify-rust", "url"] +# Enables systemd integration (global environment, apps in transient scopes). +systemd = ["dbus"] # Enables screencasting support through xdg-desktop-portal-gnome. xdp-gnome-screencast = ["dbus", "pipewire"] # Enables the Tracy profiler instrumentation. diff --git a/src/main.rs b/src/main.rs index 275d747..a19abfa 100644 --- a/src/main.rs +++ b/src/main.rs @@ -38,7 +38,15 @@ fn main() -> Result<(), Box> { REMOVE_ENV_RUST_LIB_BACKTRACE.store(true, Ordering::Relaxed); } - IS_SYSTEMD_SERVICE.store(env::var_os("NOTIFY_SOCKET").is_some(), Ordering::Relaxed); + if env::var_os("NOTIFY_SOCKET").is_some() { + IS_SYSTEMD_SERVICE.store(true, Ordering::Relaxed); + + #[cfg(not(feature = "systemd"))] + warn!( + "running as a systemd service, but systemd support is compiled out. \ + Are you sure you did not forget to set `--features systemd`?" + ); + } let directives = env::var("RUST_LOG").unwrap_or_else(|_| "niri=debug".to_owned()); let env_filter = EnvFilter::builder().parse_lossy(directives); @@ -249,11 +257,16 @@ fn import_environment() { ] .join(" "); + #[cfg(feature = "systemd")] + let systemctl = format!("systemctl --user import-environment {variables} && "); + #[cfg(not(feature = "systemd"))] + let systemctl = String::new(); + let rv = Command::new("/bin/sh") .args([ "-c", &format!( - "systemctl --user import-environment {variables} && \ + "{systemctl}\ hash dbus-update-activation-environment 2>/dev/null && \ dbus-update-activation-environment {variables}" ), @@ -273,7 +286,7 @@ fn import_environment() { } }, Err(err) => { - warn!("error spawning shell to import environment into systemd: {err:?}"); + warn!("error spawning shell to import environment: {err:?}"); } } } diff --git a/src/niri.rs b/src/niri.rs index 3533765..d88893a 100644 --- a/src/niri.rs +++ b/src/niri.rs @@ -1063,8 +1063,6 @@ impl Niri { pub fn inhibit_power_key(&mut self) -> anyhow::Result<()> { let conn = zbus::blocking::ConnectionBuilder::system()?.build()?; - // logind-zbus has a wrong signature for this method, so do it manually. - // https://gitlab.com/flukejones/logind-zbus/-/merge_requests/5 let message = conn.call_method( Some("org.freedesktop.login1"), "/org/freedesktop/login1", diff --git a/src/utils.rs b/src/utils.rs index 298dace..5801de9 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -229,7 +229,7 @@ fn spawn_sync(command: impl AsRef, args: impl IntoIterator anyhow::Result<()> { use std::fmt::Write as _; use std::path::Path;