phoebus_os/installer-rs/crates/phoebus-executor/src/desktop.rs
Phoebus 24b7259e56 fix(desktop): make phoebus-session race-free and reap helpers
The session script started waybar/mako/swaybg before dwl created the
Wayland socket and never started the polkit agent. It now starts dwl first,
waits for wayland-0, establishes a session bus and runtime dir, starts the
polkit authentication agent, and kills helpers when the compositor exits.
2026-09-20 04:40:08 -07:00

136 lines
4.7 KiB
Rust

//! Desktop profile installation.
//!
//! The flagship desktop is a small Wayland tiling environment centred on dwl,
//! with PipeWire/WirePlumber audio. Flatpak is supported out of the box.
use std::path::Path;
use phoebus_core::policy::{PackagePolicyEngine, PolicyContext};
use phoebus_core::system as sysgen;
use phoebus_core::{DesktopProfile, InstallConfig};
use crate::error::{ExecError, Result};
use crate::exec::Exec;
use crate::portage;
pub fn install(
exec: &Exec,
config: &InstallConfig,
engine: &PackagePolicyEngine,
ctx: &PolicyContext,
) -> Result<()> {
if config.desktop.profile == DesktopProfile::None {
exec.logger.info("no desktop profile selected");
return Ok(());
}
let mut packages: Vec<String> = sysgen::desktop_packages(config.desktop.profile)
.into_iter()
.map(str::to_string)
.collect();
packages.extend(
sysgen::desktop_audio_packages()
.into_iter()
.map(str::to_string),
);
if config.desktop.flatpak {
packages.push("sys-apps/flatpak".to_string());
}
exec.logger.info(format!(
"installing desktop profile '{}' ({} packages)",
config.desktop.profile,
packages.len()
));
portage::emerge(exec, Path::new(&exec.root), &packages, engine, ctx)
}
/// Write `/usr/local/bin/phoebus-session`, a clear manual entry point for the
/// Wayland desktop. It starts the supporting daemons and then execs dwl.
///
/// Hotkeys (dwl defaults, Phoebus patches may add more):
/// Mod+Shift+Return terminal Mod+d launcher
/// Mod+Shift+q close window Mod+j / Mod+k focus next/prev
/// Mod+1..9 workspace Mod+Shift+e quit dwl
pub fn write_session_script(exec: &Exec, config: &InstallConfig) -> Result<()> {
if config.desktop.profile == DesktopProfile::None {
return Ok(());
}
let target = exec.target();
let script = sysgen::PHOEBUS_SESSION_SCRIPT;
let path = target.resolve("usr/local/bin/phoebus-session")?;
target.write("usr/local/bin/phoebus-session", script)?;
use std::os::unix::fs::PermissionsExt;
std::fs::set_permissions(&path, std::fs::Permissions::from_mode(0o755))
.map_err(|e| ExecError::io(path.clone(), e))?;
exec.logger.info("wrote /usr/local/bin/phoebus-session");
Ok(())
}
pub fn configure_flatpak(exec: &Exec, config: &InstallConfig) -> Result<()> {
if !config.desktop.flatpak {
return Ok(());
}
if let Err(e) = portage::chroot(
exec,
Path::new(&exec.root),
"flatpak",
&[
"remote-add",
"--if-not-exists",
"flathub",
"https://flathub.org/repo/flathub.flatpakrepo",
],
) {
exec.logger
.warn(format!("could not add flathub remote: {e}"));
return Ok(());
}
exec.logger.info("flatpak configured with flathub");
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
use crate::command::RecordingRunner;
use crate::log::Logger;
use phoebus_core::Redactor;
fn make_exec(runner: RecordingRunner) -> Exec {
let path = tempfile::tempdir().unwrap().keep();
let logger = Logger::open(path.join("l.log"), Redactor::new(), false);
Exec::new(Box::new(runner), logger, false, path.join("root"), path)
}
#[test]
fn session_script_waits_for_the_socket_and_starts_polkit() {
let runner = RecordingRunner::new();
let exec = make_exec(runner);
let mut cfg = InstallConfig::default();
cfg.desktop.profile = DesktopProfile::Dwl;
write_session_script(&exec, &cfg).unwrap();
let path = exec.root.join("usr/local/bin/phoebus-session");
let text = std::fs::read_to_string(&path).unwrap();
// dwl is started first and waited on; helpers wait for the socket.
assert!(text.contains("dwl &"));
assert!(text.contains("wait \"$DWL_PID\""));
assert!(!text.contains("exec dwl"), "exec would skip helper cleanup");
assert!(text.contains("wayland-0"));
assert!(text.contains("polkit-gnome-authentication-agent-1"));
assert!(text.contains("start_helper waybar"));
assert!(text.contains("trap cleanup EXIT INT TERM"));
use std::os::unix::fs::PermissionsExt;
assert_ne!(
std::fs::metadata(&path).unwrap().permissions().mode() & 0o111,
0
);
}
#[test]
fn no_session_script_for_none_profile() {
let runner = RecordingRunner::new();
let exec = make_exec(runner);
let cfg = InstallConfig::default();
write_session_script(&exec, &cfg).unwrap();
assert!(!exec.root.join("usr/local/bin/phoebus-session").exists());
}
}