From 5db3d1ddce62639dc4100dbc79bbdea29bba6efd Mon Sep 17 00:00:00 2001 From: Marco Allegretti Date: Fri, 9 Oct 2026 20:26:05 +0000 Subject: [PATCH 1/3] fix(nixos): start weft-appd and give the session its shell page and app shell The reference session could not show the system UI or open an app, even with every package built: - weft-servo-shell ignores command-line arguments, yet infra/systemd/servo-shell.service passed it the page path, and the binary's packaged fallback named a path no package provides. The Nix package did not install the page and its NixOS service did not name it either, so the shell exited at startup. - The NixOS weft-appd user service was wanted by nothing, so it never started, and it had no WEFT_APP_SHELL_BIN, so every launch would stop before its UI host started. The weft-servo-shell package now installs the page as share/weft/shell/system-ui.html, the binary falls back to that path in the package's active revision, the systemd unit drops the ignored argument, and the NixOS service sets WEFT_SYSTEM_UI_HTML to the package's copy. weft-appd is wanted by the graphical session and is given the app shell. docs/building.md lists what still keeps the image from a working desktop: the Servo source hash, servo-embed in the shell packages and the fixed Wayland socket name. --- crates/weft-servo-shell/src/main.rs | 7 ++++++- docs/building.md | 8 +++++++- infra/nixos/configuration.nix | 3 +++ infra/nixos/weft-packages.nix | 8 ++++++-- infra/systemd/servo-shell.service | 5 +++-- 5 files changed, 25 insertions(+), 6 deletions(-) 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 fca337a..9a4a6d6 100644 --- a/docs/building.md +++ b/docs/building.md @@ -139,7 +139,13 @@ 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. + +The VM's user session starts `weft-compositor`, `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. 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 shell's service names the compositor's socket `wayland-1`, the first name the compositor tries, rather than learning it. ## CI diff --git a/infra/nixos/configuration.nix b/infra/nixos/configuration.nix index 5a6a3bb..17a66c2 100644 --- a/infra/nixos/configuration.nix +++ b/infra/nixos/configuration.nix @@ -96,6 +96,7 @@ wantedBy = [ "graphical-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 +110,7 @@ description = "WEFT Application Daemon"; requires = [ "weft-compositor.service" ]; after = [ "weft-compositor.service" "weft-servo-shell.service" ]; + wantedBy = [ "graphical-session.target" ]; serviceConfig = { Type = "notify"; ExecStart = "${pkgs.weft.weft-appd}/bin/weft-appd"; @@ -116,6 +118,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" ]; 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..e87141e 100644 --- a/infra/systemd/servo-shell.service +++ b/infra/systemd/servo-shell.service @@ -8,8 +8,9 @@ After=weft-compositor.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 From 239afc2d116f484ccf33fdd28cd03d6c88f64c3d Mon Sep 17 00:00:00 2001 From: Marco Allegretti Date: Fri, 9 Oct 2026 20:34:33 +0000 Subject: [PATCH 2/3] 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(); + } +} From a1edac63b845e92f62035cd2d38949eae84709fc Mon Sep 17 00:00:00 2001 From: Marco Allegretti Date: Fri, 9 Oct 2026 20:34:33 +0000 Subject: [PATCH 3/3] fix(session): start the session as user units through its own target The tty1 login hook ran systemctl --user start graphical-session.target, which systemd refuses (RefuseManualStart=yes), so none of the NixOS session's services started. The units in infra/systemd were wanted by graphical.target, a system target, although weft-appd's needs the session user's runtime and home directories and the compositor publishes its socket to the user manager; run as system units the shell could never learn the socket. weft-session.target binds graphical-session.target and wants the compositor, shell and weft-appd, in the NixOS configuration and as a user unit in infra/systemd; the login hook starts it. The services are part of the graphical session, the shell no longer hardcodes the compositor's socket, and the compositor's units unset WAYLAND_DISPLAY and DISPLAY so a restarted compositor does not take them for an enclosing session and pick the nested backend. As a user unit, weft-appd drops ProtectSystem= and PrivateTmp=, which a user manager implements with PrivateUsers=yes, under which the setuid mount helper cannot mount verified images. docs/building.md adds the VM's disabled graphics and its mount helper, which the Nix store cannot hold setuid, to what still keeps the image from a working desktop. Rendered from the NixOS configuration, and as installed from infra/systemd, the units pass systemd-analyze --user verify. --- docs/building.md | 5 +++-- infra/nixos/configuration.nix | 26 ++++++++++++++++++++------ infra/systemd/servo-shell.service | 4 ++-- infra/systemd/weft-appd.service | 12 +++++------- infra/systemd/weft-compositor.service | 12 ++++++++---- infra/systemd/weft-session.target | 10 ++++++++++ 6 files changed, 48 insertions(+), 21 deletions(-) create mode 100644 infra/systemd/weft-session.target diff --git a/docs/building.md b/docs/building.md index 9a4a6d6..9791fb4 100644 --- a/docs/building.md +++ b/docs/building.md @@ -141,11 +141,12 @@ 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 VM's user session starts `weft-compositor`, `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. The image is not yet bootable to a working desktop: +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 shell's service names the compositor's socket `wayland-1`, the first name the compositor tries, rather than learning it. +- 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 17a66c2..271012a 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,9 @@ 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 = { @@ -110,7 +123,8 @@ description = "WEFT Application Daemon"; requires = [ "weft-compositor.service" ]; after = [ "weft-compositor.service" "weft-servo-shell.service" ]; - wantedBy = [ "graphical-session.target" ]; + partOf = [ "graphical-session.target" ]; + wantedBy = [ "weft-session.target" ]; serviceConfig = { Type = "notify"; ExecStart = "${pkgs.weft.weft-appd}/bin/weft-appd"; @@ -128,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/systemd/servo-shell.service b/infra/systemd/servo-shell.service index e87141e..e6ef1cb 100644 --- a/infra/systemd/servo-shell.service +++ b/infra/systemd/servo-shell.service @@ -3,10 +3,10 @@ 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 # The page is the package's share/weft/shell/system-ui.html; set # WEFT_SYSTEM_UI_HTML to use another. @@ -15,4 +15,4 @@ 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