From 239afc2d116f484ccf33fdd28cd03d6c88f64c3d Mon Sep 17 00:00:00 2001 From: Marco Allegretti Date: Fri, 9 Oct 2026 20:34:33 +0000 Subject: [PATCH] fix(compositor): publish the Wayland socket to the systemd user manager The services started after the compositor had no way to learn its socket: the NixOS shell service hardcoded wayland-1, weft-appd's service had no WAYLAND_DISPLAY at all, so the app shells it starts could not connect, and the plain unit's comment claimed the compositor posted the socket to the environment, which it did not. Started by systemd, the compositor now sets WAYLAND_DISPLAY in the user manager with systemctl --user set-environment right after creating its socket and before reporting readiness, so the units ordered after it inherit it. The winit backend now reports readiness too, as a Type=notify unit expects. --- crates/weft-compositor/src/backend/drm.rs | 2 + crates/weft-compositor/src/backend/winit.rs | 5 ++ crates/weft-compositor/src/main.rs | 1 + crates/weft-compositor/src/session_env.rs | 85 +++++++++++++++++++++ 4 files changed, 93 insertions(+) create mode 100644 crates/weft-compositor/src/session_env.rs diff --git a/crates/weft-compositor/src/backend/drm.rs b/crates/weft-compositor/src/backend/drm.rs index 93bb258..4da4c0b 100644 --- a/crates/weft-compositor/src/backend/drm.rs +++ b/crates/weft-compositor/src/backend/drm.rs @@ -134,6 +134,8 @@ pub fn run() -> anyhow::Result<()> { let socket_name = listening_socket.socket_name().to_os_string(); unsafe { std::env::set_var("WAYLAND_DISPLAY", &socket_name) }; tracing::info!(?socket_name, "Wayland socket open"); + // Before readiness, so the units ordered after this one see it. + crate::session_env::publish_wayland_display(&socket_name); loop_handle .insert_source(listening_socket, |stream, _, state| { diff --git a/crates/weft-compositor/src/backend/winit.rs b/crates/weft-compositor/src/backend/winit.rs index 7575211..1e08b14 100644 --- a/crates/weft-compositor/src/backend/winit.rs +++ b/crates/weft-compositor/src/backend/winit.rs @@ -60,6 +60,7 @@ pub fn run() -> anyhow::Result<()> { let socket_name = listening_socket.socket_name().to_os_string(); unsafe { std::env::set_var("WAYLAND_DISPLAY", &socket_name) }; tracing::info!(?socket_name, "Wayland compositor socket open"); + crate::session_env::publish_wayland_display(&socket_name); loop_handle .insert_source(listening_socket, |client_stream, _, state| { @@ -185,6 +186,10 @@ pub fn run() -> anyhow::Result<()> { }) .map_err(|e| anyhow::anyhow!("winit source insertion failed: {e}"))?; + // A Type=notify unit waits for this, as with the DRM backend. + #[cfg(target_os = "linux")] + let _ = sd_notify::notify(false, &[sd_notify::NotifyState::Ready]); + event_loop.run(None, &mut state, |_| {})?; Ok(()) diff --git a/crates/weft-compositor/src/main.rs b/crates/weft-compositor/src/main.rs index 84334c4..8e80795 100644 --- a/crates/weft-compositor/src/main.rs +++ b/crates/weft-compositor/src/main.rs @@ -4,6 +4,7 @@ mod appd_ipc; mod backend; mod input; mod protocols; +mod session_env; mod state; fn main() -> anyhow::Result<()> { diff --git a/crates/weft-compositor/src/session_env.rs b/crates/weft-compositor/src/session_env.rs new file mode 100644 index 0000000..69a3003 --- /dev/null +++ b/crates/weft-compositor/src/session_env.rs @@ -0,0 +1,85 @@ +//! Tells the systemd user manager which Wayland socket the compositor +//! serves, so the services started after it, the shell and weft-appd and +//! through it the app shells, connect to this compositor. Only done when +//! systemd started the compositor; its own unit unsets the variable, so a +//! restarted compositor does not mistake the value for an enclosing +//! Wayland session. + +use std::ffi::{OsStr, OsString}; +use std::process::{Command, Stdio}; + +/// Publishes `socket_name` as `WAYLAND_DISPLAY` for the units the user +/// manager starts from now on, when running under systemd. +pub fn publish_wayland_display(socket_name: &OsStr) { + if std::env::var_os("NOTIFY_SOCKET").is_none() { + return; + } + publish_with(OsStr::new("systemctl"), socket_name); +} + +fn publish_with(systemctl: &OsStr, socket_name: &OsStr) -> bool { + let mut assignment = OsString::from("WAYLAND_DISPLAY="); + assignment.push(socket_name); + let status = Command::new(systemctl) + .args(["--user", "set-environment"]) + .arg(&assignment) + .stdin(Stdio::null()) + .status(); + match status { + Ok(status) if status.success() => { + tracing::info!( + ?socket_name, + "WAYLAND_DISPLAY published to the user manager" + ); + true + } + Ok(status) => { + tracing::warn!(%status, "cannot publish WAYLAND_DISPLAY to the user manager"); + false + } + Err(e) => { + tracing::warn!(error = %e, "cannot publish WAYLAND_DISPLAY to the user manager"); + false + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + use std::os::unix::fs::PermissionsExt; + + #[test] + fn the_socket_name_is_set_in_the_user_manager() { + let dir = std::env::temp_dir().join(format!("weft_session_env_{}", std::process::id())); + let _ = std::fs::remove_dir_all(&dir); + std::fs::create_dir_all(&dir).unwrap(); + let record = dir.join("args"); + let fake = dir.join("systemctl"); + std::fs::write( + &fake, + format!( + "#!/bin/sh\nprintf '%s\\n' \"$@\" > '{}'\n", + record.display() + ), + ) + .unwrap(); + std::fs::set_permissions(&fake, std::fs::Permissions::from_mode(0o755)).unwrap(); + + assert!(publish_with(fake.as_os_str(), OsStr::new("wayland-3"))); + assert_eq!( + std::fs::read_to_string(&record).unwrap(), + "--user\nset-environment\nWAYLAND_DISPLAY=wayland-3\n" + ); + + let failing = dir.join("failing"); + std::fs::write(&failing, "#!/bin/sh\nexit 1\n").unwrap(); + std::fs::set_permissions(&failing, std::fs::Permissions::from_mode(0o755)).unwrap(); + assert!(!publish_with(failing.as_os_str(), OsStr::new("wayland-3"))); + assert!(!publish_with( + dir.join("missing").as_os_str(), + OsStr::new("wayland-3") + )); + std::fs::remove_dir_all(&dir).unwrap(); + } +}