Add --session CLI flag instead of detection based on systemd service
Browse files at this commitf0a5e9c9332624aa7ea201c8c83f4f20b0a9775fParent: c4c07843 changed files +22 -16
- resources/niri.service +1 -1
- src/cli.rs +7 -0
- src/main.rs +14 -15
resources/niri.servicemodified+1 -1
| @@ -11,4 +11,4 @@ Before=xdg-desktop-autostart.target | ||
| 11 | 11 | [Service] |
| 12 | 12 | Slice=session.slice |
| 13 | 13 | 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 { | ||
| 15 | 15 | /// Path to config file (default: `$XDG_CONFIG_HOME/niri/config.kdl`). |
| 16 | 16 | #[arg(short, long)] |
| 17 | 17 | 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, | |
| 18 | 25 | /// Command to run upon compositor startup. |
| 19 | 26 | #[arg(last = true)] |
| 20 | 27 | pub command: Vec<OsString>, |
src/main.rsmodified+14 -15
| @@ -38,8 +38,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> { | ||
| 38 | 38 | REMOVE_ENV_RUST_LIB_BACKTRACE.store(true, Ordering::Relaxed); |
| 39 | 39 | } |
| 40 | 40 | |
| 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); | |
| 43 | 42 | |
| 44 | 43 | let directives = env::var("RUST_LOG").unwrap_or_else(|_| "niri=debug".to_owned()); |
| 45 | 44 | let env_filter = EnvFilter::builder().parse_lossy(directives); |
| @@ -48,22 +47,22 @@ fn main() -> Result<(), Box<dyn std::error::Error>> { | ||
| 48 | 47 | .with_env_filter(env_filter) |
| 49 | 48 | .init(); |
| 50 | 49 | |
| 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. | |
| 55 | 56 | 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"); | |
| 57 | 58 | env::remove_var("DISPLAY"); |
| 58 | 59 | } |
| 59 | 60 | 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"); | |
| 61 | 62 | env::remove_var("WAYLAND_DISPLAY"); |
| 62 | 63 | } |
| 63 | 64 | } |
| 64 | 65 | |
| 65 | - let cli = Cli::parse(); | |
| 66 | - | |
| 67 | 66 | let _client = tracy_client::Client::start(); |
| 68 | 67 | |
| 69 | 68 | // Set a better error printer for config loading. |
| @@ -177,9 +176,9 @@ fn main() -> Result<(), Box<dyn std::error::Error>> { | ||
| 177 | 176 | info!("IPC listening on: {}", ipc.socket_path.to_string_lossy()); |
| 178 | 177 | } |
| 179 | 178 | |
| 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(); | |
| 183 | 182 | |
| 184 | 183 | // Inhibit power key handling so we can suspend on it. |
| 185 | 184 | #[cfg(feature = "dbus")] |
| @@ -191,7 +190,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> { | ||
| 191 | 190 | } |
| 192 | 191 | |
| 193 | 192 | #[cfg(feature = "dbus")] |
| 194 | - dbus::DBusServers::start(&mut state, is_systemd_service); | |
| 193 | + dbus::DBusServers::start(&mut state, cli.session); | |
| 195 | 194 | |
| 196 | 195 | // Notify systemd we're ready. |
| 197 | 196 | if let Err(err) = sd_notify::notify(true, &[NotifyState::Ready]) { |
| @@ -236,7 +235,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> { | ||
| 236 | 235 | Ok(()) |
| 237 | 236 | } |
| 238 | 237 | |
| 239 | -fn import_env_to_systemd() { | |
| 238 | +fn import_environment() { | |
| 240 | 239 | let variables = ["WAYLAND_DISPLAY", niri_ipc::SOCKET_PATH_ENV].join(" "); |
| 241 | 240 | |
| 242 | 241 | let rv = Command::new("/bin/sh") |