diff --git a/crates/weft-compositor/src/backend/drm.rs b/crates/weft-compositor/src/backend/drm.rs index 51a56c2..755b50f 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 cd11fad..d02fcd6 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| { @@ -186,6 +187,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 850116f..2fff1fd 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; mod windows; 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(); + } +} diff --git a/crates/weft-servo-shell/src/main.rs b/crates/weft-servo-shell/src/main.rs index d659cb1..88a997c 100644 --- a/crates/weft-servo-shell/src/main.rs +++ b/crates/weft-servo-shell/src/main.rs @@ -37,12 +37,17 @@ fn run() -> anyhow::Result<()> { embed_servo(&wayland_display, &html_path, ws_port) } +/// Where the weft-servo-shell package installs the system UI page; +/// `infra/systemd/servo-shell.service` relies on it. +const PACKAGED_SYSTEM_UI: &str = + "/packages/system/weft-servo-shell/active/share/weft/shell/system-ui.html"; + fn system_ui_html_path() -> anyhow::Result { if let Ok(p) = std::env::var("WEFT_SYSTEM_UI_HTML") { return Ok(PathBuf::from(p)); } - let packaged = PathBuf::from("/packages/system/servo-shell/active/share/weft/system-ui.html"); + let packaged = PathBuf::from(PACKAGED_SYSTEM_UI); if packaged.exists() { return Ok(packaged); } diff --git a/docs/building.md b/docs/building.md index e91e3c7..e1d5635 100644 --- a/docs/building.md +++ b/docs/building.md @@ -152,7 +152,14 @@ Run with QEMU: bash infra/vm/run.sh ``` -See `infra/nixos/weft-packages.nix` for the package derivations. Its `outputHashes` hold one hash per git source in `Cargo.lock` (the Servo and Stylo forks); every package vendors them, so they must match the pinned revisions for any Nix package to build. The Nix shell packages do not yet enable `servo-embed`. +See `infra/nixos/weft-packages.nix` for the package derivations. Its `outputHashes` hold one hash per git source in `Cargo.lock` (the Servo and Stylo forks); every package vendors them, so they must match the pinned revisions for any Nix package to build. + +Logging in on tty1 starts the user target `weft-session.target`, which binds `graphical-session.target` (that one refuses a manual start) and wants `weft-compositor`, then `weft-servo-shell` with the system UI page its package installs, and `weft-appd` with the runtime, app shell, file portal and mount helper packages. Run as a systemd user service, the compositor publishes its socket as `WAYLAND_DISPLAY` to the user manager before reporting readiness, so the units after it, and the app shells weft-appd starts, connect to it. The units in `infra/systemd/` are the same session for an installation outside Nix, as user units under `weft-session.target`. The image is not yet bootable to a working desktop: + +- the Servo source hash in `outputHashes` is still a placeholder, so no package builds until a Nix build reports it; +- the shell packages do not enable `servo-embed`, so `weft-servo-shell` and `weft-app-shell` exit at startup; +- the VM runs without graphics (`virtualisation.graphics = false` in `infra/nixos/configuration.nix`, for a serial console), so the session's output is not shown; +- `WEFT_MOUNT_HELPER` names the mount helper's store path, which cannot be setuid, so verified images cannot be mounted; a setuid wrapper would be needed. ## CI diff --git a/infra/nixos/configuration.nix b/infra/nixos/configuration.nix index c15f78f..a18d3e5 100644 --- a/infra/nixos/configuration.nix +++ b/infra/nixos/configuration.nix @@ -75,15 +75,28 @@ }) ]; + # graphical-session.target refuses a manual start; the login shell starts + # this target, which binds it, as Wayland compositors' session targets do. + systemd.user.targets.weft-session = { + description = "WEFT OS session"; + bindsTo = [ "graphical-session.target" ]; + wants = [ "graphical-session-pre.target" ]; + after = [ "graphical-session-pre.target" ]; + }; + systemd.user.services = { weft-compositor = { description = "WEFT OS Wayland Compositor"; - after = [ "graphical-session.target" ]; + before = [ "graphical-session.target" ]; partOf = [ "graphical-session.target" ]; - wantedBy = [ "graphical-session.target" ]; + wantedBy = [ "weft-session.target" ]; serviceConfig = { Type = "notify"; ExecStart = "${pkgs.weft.weft-compositor}/bin/weft-compositor"; + # The compositor publishes WAYLAND_DISPLAY to the user manager for the + # units after it; a restarted compositor must not inherit it, or an + # X11 DISPLAY, or it would pick the nested backend. + UnsetEnvironment = "WAYLAND_DISPLAY DISPLAY"; Restart = "on-failure"; RestartSec = "1"; }; @@ -93,9 +106,10 @@ description = "WEFT OS System Shell"; requires = [ "weft-compositor.service" ]; after = [ "weft-compositor.service" ]; - wantedBy = [ "graphical-session.target" ]; + partOf = [ "graphical-session.target" ]; + wantedBy = [ "weft-session.target" ]; environment = { - WAYLAND_DISPLAY = "wayland-1"; + WEFT_SYSTEM_UI_HTML = "${pkgs.weft.weft-servo-shell}/share/weft/shell/system-ui.html"; }; serviceConfig = { Type = "simple"; @@ -109,6 +123,8 @@ description = "WEFT Application Daemon"; requires = [ "weft-compositor.service" ]; after = [ "weft-compositor.service" "weft-servo-shell.service" ]; + partOf = [ "graphical-session.target" ]; + wantedBy = [ "weft-session.target" ]; serviceConfig = { Type = "notify"; ExecStart = "${pkgs.weft.weft-appd}/bin/weft-appd"; @@ -116,6 +132,7 @@ RestartSec = "1s"; Environment = [ "WEFT_RUNTIME_BIN=${pkgs.weft.weft-runtime}/bin/weft-runtime" + "WEFT_APP_SHELL_BIN=${pkgs.weft.weft-app-shell}/bin/weft-app-shell" "WEFT_FILE_PORTAL_BIN=${pkgs.weft.weft-file-portal}/bin/weft-file-portal" "WEFT_MOUNT_HELPER=${pkgs.weft.weft-mount-helper}/bin/weft-mount-helper" ]; @@ -125,7 +142,7 @@ programs.bash.loginShellInit = '' if [ -z "$DISPLAY" ] && [ -z "$WAYLAND_DISPLAY" ] && [ "$(tty)" = "/dev/tty1" ]; then - systemctl --user start graphical-session.target + systemctl --user start weft-session.target fi ''; diff --git a/infra/nixos/weft-packages.nix b/infra/nixos/weft-packages.nix index f3a7ffc..7a86106 100644 --- a/infra/nixos/weft-packages.nix +++ b/infra/nixos/weft-packages.nix @@ -26,8 +26,8 @@ let nativeBuildInputs = with pkgs; [ pkg-config ]; }; - mkWeftPkg = { pname, extraBuildInputs ? [], extraNativeBuildInputs ? [], cargoFlags ? [], extraEnv ? {}, preBuild ? "" }: rustPlatform.buildRustPackage (commonArgs // { - inherit pname preBuild; + mkWeftPkg = { pname, extraBuildInputs ? [], extraNativeBuildInputs ? [], cargoFlags ? [], extraEnv ? {}, preBuild ? "", postInstall ? "" }: rustPlatform.buildRustPackage (commonArgs // { + inherit pname preBuild postInstall; cargoBuildFlags = [ "--package" pname ] ++ cargoFlags; cargoTestFlags = [ "--package" pname ]; buildInputs = extraBuildInputs; @@ -50,6 +50,10 @@ in { weft-servo-shell = mkWeftPkg { pname = "weft-servo-shell"; + # The system UI page, at the path the shell's units name. + postInstall = '' + install -Dm644 infra/shell/system-ui.html $out/share/weft/shell/system-ui.html + ''; }; weft-app-shell = mkWeftPkg { diff --git a/infra/systemd/servo-shell.service b/infra/systemd/servo-shell.service index e44cf9d..e6ef1cb 100644 --- a/infra/systemd/servo-shell.service +++ b/infra/systemd/servo-shell.service @@ -3,15 +3,16 @@ Description=WEFT OS Servo Shell Documentation=https://github.com/weft-os/weft Requires=weft-compositor.service After=weft-compositor.service +PartOf=graphical-session.target [Service] Type=simple -PassEnvironment=WAYLAND_DISPLAY XDG_RUNTIME_DIR DISPLAY Environment=WEFT_APPD_WS_PORT=7410 -ExecStart=/packages/system/weft-servo-shell/active/bin/weft-servo-shell \ - /packages/system/weft-servo-shell/active/share/weft/shell/system-ui.html +# The page is the package's share/weft/shell/system-ui.html; set +# WEFT_SYSTEM_UI_HTML to use another. +ExecStart=/packages/system/weft-servo-shell/active/bin/weft-servo-shell Restart=on-failure RestartSec=2 [Install] -WantedBy=graphical.target +WantedBy=weft-session.target diff --git a/infra/systemd/weft-appd.service b/infra/systemd/weft-appd.service index 45fd526..105acdf 100644 --- a/infra/systemd/weft-appd.service +++ b/infra/systemd/weft-appd.service @@ -3,6 +3,7 @@ Description=WEFT Application Daemon Documentation=https://github.com/weft-os/weft Requires=weft-compositor.service After=weft-compositor.service servo-shell.service +PartOf=graphical-session.target [Service] Type=notify @@ -10,15 +11,12 @@ Environment=WEFT_RUNTIME_BIN=/packages/system/weft-runtime/active/bin/weft-runti Environment=WEFT_APP_SHELL_BIN=/packages/system/weft-app-shell/active/bin/weft-app-shell Environment=WEFT_FILE_PORTAL_BIN=/packages/system/weft-file-portal/active/bin/weft-file-portal Environment=WEFT_MOUNT_HELPER=/packages/system/weft-mount-helper/active/bin/weft-mount-helper -PassEnvironment=WAYLAND_DISPLAY XDG_RUNTIME_DIR DISPLAY ExecStart=/packages/system/weft-appd/active/bin/weft-appd Restart=on-failure RestartSec=1s -ProtectSystem=strict -PrivateTmp=true -# App data and the user package store, where earlier versions kept app data. -# %h is the home of the user the unit runs as; run it as the session user. -ReadWritePaths=%t -%h/.local/share/weft +# No ProtectSystem= or PrivateTmp=: in the user manager they imply +# PrivateUsers=yes, under which the setuid weft-mount-helper cannot mount +# verified images. Each session's runtime runs in its own systemd scope. [Install] -WantedBy=graphical.target +WantedBy=weft-session.target diff --git a/infra/systemd/weft-compositor.service b/infra/systemd/weft-compositor.service index ad7ac7e..52489b0 100644 --- a/infra/systemd/weft-compositor.service +++ b/infra/systemd/weft-compositor.service @@ -1,15 +1,19 @@ [Unit] Description=WEFT OS Wayland Compositor Documentation=https://github.com/weft-os/weft -After=systemd-logind.service +Before=graphical-session.target +PartOf=graphical-session.target [Service] Type=notify ExecStart=/packages/system/weft-compositor/active/bin/weft-compositor Restart=on-failure RestartSec=1 -# The socket path is posted to the environment by the compositor itself. -# Downstream services (servo-shell.service) should declare After=weft-compositor.service. +# A user unit: the compositor publishes its socket as WAYLAND_DISPLAY in the +# user manager before reporting readiness, for the services ordered +# After=weft-compositor.service. It must not inherit that value, or an X11 +# DISPLAY, on restart, or it would pick the nested backend. +UnsetEnvironment=WAYLAND_DISPLAY DISPLAY [Install] -WantedBy=graphical.target +WantedBy=weft-session.target diff --git a/infra/systemd/weft-session.target b/infra/systemd/weft-session.target new file mode 100644 index 0000000..382c3d0 --- /dev/null +++ b/infra/systemd/weft-session.target @@ -0,0 +1,10 @@ +[Unit] +Description=WEFT OS session +Documentation=https://github.com/weft-os/weft +# These are user units: install them in the user manager +# (systemctl --user enable weft-compositor servo-shell weft-appd) and start +# the session with systemctl --user start weft-session.target, as the NixOS +# image's login shell does. graphical-session.target refuses a manual start. +BindsTo=graphical-session.target +Wants=graphical-session-pre.target +After=graphical-session-pre.target