//! 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 = 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(); // A launcher for the installed assistant. It opens `sys ask`, which keeps // privilege and approval in the deterministic broker. write_script( &target, "usr/local/bin/phoebus-assistant", sysgen::PHOEBUS_ASSISTANT_SCRIPT, )?; // The "Install Phoebus OS" entry point and welcome launcher are live-medium // only: an installed system has no business offering to reinstall itself. // They are installed by the live build, not here. match config.desktop.profile { DesktopProfile::Mango => { write_mango_session(exec, &target)?; // The dwl session stays installed as a fallback. write_dwl_session(exec, &target)?; } DesktopProfile::Dwl | DesktopProfile::Full => { write_dwl_session(exec, &target)?; } // Minimal has no session entry point. _ => {} } Ok(()) } fn write_dwl_session(exec: &Exec, target: &phoebus_core::TargetRoot) -> Result<()> { 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))?; if !path.is_file() { return Err(ExecError::operation( "desktop", "write session script", format!("session script missing after write: {}", path.display()), )); } // Launcher/power menu, screen locker and idle daemon. Installing these (and // the Waybar configuration below) completes the discoverable desktop: a // user can launch apps, control network/audio, lock and suspend without // editing configuration by hand. write_script( &target, "usr/local/bin/phoebus-menu", sysgen::PHOEBUS_MENU_SCRIPT, )?; write_script( &target, "usr/local/bin/phoebus-lock", sysgen::PHOEBUS_LOCK_SCRIPT, )?; write_script( &target, "usr/local/bin/phoebus-idle", sysgen::PHOEBUS_IDLE_SCRIPT, )?; target.write("etc/xdg/waybar/config", sysgen::WAYBAR_CONFIG)?; target.write("etc/xdg/waybar/style.css", sysgen::WAYBAR_STYLE)?; if let Some((name, entry)) = sysgen::wayland_session_entry(DesktopProfile::Dwl) { target.write(&format!("usr/share/wayland-sessions/{name}"), entry)?; } exec.logger.info(format!( "wrote /usr/local/bin/phoebus-session ({})", path.display() )); Ok(()) } fn write_mango_session(exec: &Exec, target: &phoebus_core::TargetRoot) -> Result<()> { write_script( target, "usr/local/bin/phoebus-session-mango", sysgen::PHOEBUS_MANGO_SESSION_SCRIPT, )?; target.write("etc/xdg/mango/config.conf", sysgen::MANGO_CONFIG_DEFAULT)?; target.write( "usr/lib/systemd/user/mango-session.target", sysgen::MANGO_SESSION_TARGET, )?; // Bind dms.service to the session target when the DMS package provides it; // the session script falls back to `dms run` otherwise. let dms_unit = target.resolve("usr/lib/systemd/user/dms.service")?; if dms_unit.exists() { let wants = target.resolve("etc/systemd/user/mango-session.target.wants/dms.service")?; if let Some(parent) = wants.parent() { std::fs::create_dir_all(parent).map_err(|e| ExecError::io(parent.to_path_buf(), e))?; } let _ = std::fs::remove_file(&wants); std::os::unix::fs::symlink(&dms_unit, &wants) .map_err(|e| ExecError::io(wants.clone(), e))?; } if let Some((name, entry)) = sysgen::wayland_session_entry(DesktopProfile::Mango) { target.write(&format!("usr/share/wayland-sessions/{name}"), entry)?; } exec.logger .info("wrote /usr/local/bin/phoebus-session-mango (Mango + DMS)"); Ok(()) } /// Configure authenticated graphical login with greetd. The installed system /// always authenticates; only the live ISO uses automatic login. pub fn configure_login(exec: &Exec, config: &InstallConfig) -> Result<()> { if !config.desktop.profile.is_desktop() { return Ok(()); } let target = exec.target(); let cmd = sysgen::default_session_command(config.desktop.profile); target.write("etc/greetd/config.toml", sysgen::greetd_config(cmd))?; ensure_greeter_user(exec); let root = Path::new(&exec.root); for service in sysgen::desktop_login_services() { if let Err(e) = portage::chroot(exec, root, "systemctl", &["enable", service]) { exec.logger.warn(format!("could not enable {service}: {e}")); } } exec.logger .info(format!("configured greetd login (default session {cmd})")); Ok(()) } fn ensure_greeter_user(exec: &Exec) { let root = Path::new(&exec.root); // The greetd greeter runs as `greeter`. `useradd` fails when the account // already exists (e.g. created by the package), which is not an error. match portage::chroot( exec, root, "useradd", &[ "--system", "--no-create-home", "--home-dir", "/var/lib/greetd", "--shell", "/sbin/nologin", "--comment", "Phoebus greeter", "greeter", ], ) { Ok(()) => exec.logger.info("created the greeter user"), Err(e) => exec .logger .info(format!("greeter user already present or not created: {e}")), } } fn write_script(target: &phoebus_core::TargetRoot, rel: &str, contents: &str) -> Result<()> { use std::os::unix::fs::PermissionsExt; let path = target.resolve(rel)?; target.write(rel, contents)?; std::fs::set_permissions(&path, std::fs::Permissions::from_mode(0o755)) .map_err(|e| ExecError::io(path.clone(), e))?; if !path.is_file() { return Err(ExecError::operation( "desktop", "write session helper", format!("helper missing after write: {}", path.display()), )); } 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 session_helpers_and_waybar_config_are_installed() { 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(); for rel in [ "usr/local/bin/phoebus-menu", "usr/local/bin/phoebus-lock", "usr/local/bin/phoebus-idle", "etc/xdg/waybar/config", "etc/xdg/waybar/style.css", ] { let path = exec.root.join(rel); assert!(path.is_file(), "{rel} was not installed"); } use std::os::unix::fs::PermissionsExt; for rel in [ "usr/local/bin/phoebus-menu", "usr/local/bin/phoebus-lock", "usr/local/bin/phoebus-idle", ] { let mode = std::fs::metadata(exec.root.join(rel)) .unwrap() .permissions() .mode(); assert_ne!(mode & 0o111, 0, "{rel} must be executable"); } } #[test] fn no_session_script_for_none_profile() { let runner = RecordingRunner::new(); let exec = make_exec(runner); let mut cfg = InstallConfig::default(); cfg.desktop.profile = DesktopProfile::None; write_session_script(&exec, &cfg).unwrap(); assert!(!exec.root.join("usr/local/bin/phoebus-session").exists()); // An explicit `none` also writes no assistant launcher. assert!(!exec.root.join("usr/local/bin/phoebus-assistant").exists()); } #[test] fn mango_profile_installs_both_sessions_and_no_dwl_helpers_in_mango() { let runner = RecordingRunner::new(); let exec = make_exec(runner); let mut cfg = InstallConfig::default(); cfg.desktop.profile = DesktopProfile::Mango; write_session_script(&exec, &cfg).unwrap(); for rel in [ "usr/local/bin/phoebus-session-mango", "usr/local/bin/phoebus-session", "usr/local/bin/phoebus-assistant", "etc/xdg/mango/config.conf", "usr/lib/systemd/user/mango-session.target", "usr/share/wayland-sessions/phoebus-mango.desktop", "usr/share/wayland-sessions/phoebus-dwl.desktop", ] { assert!(exec.root.join(rel).is_file(), "{rel} was not installed"); } let mango = std::fs::read_to_string(exec.root.join("usr/local/bin/phoebus-session-mango")).unwrap(); for helper in ["waybar", "mako", "swaybg", "swayidle", "swaylock"] { assert!( !mango.contains(helper), "Mango session must not start {helper}" ); } assert!(mango.contains("dms run") || mango.contains("mango-session.target")); let dwl = std::fs::read_to_string(exec.root.join("usr/local/bin/phoebus-session")).unwrap(); assert!( dwl.contains("dwl &"), "dwl fallback session must be present" ); } #[test] fn mango_profile_configures_greetd_login_and_no_install_shortcut() { let runner = RecordingRunner::new(); let exec = make_exec(runner.clone()); let mut cfg = InstallConfig::default(); cfg.desktop.profile = DesktopProfile::Mango; write_session_script(&exec, &cfg).unwrap(); configure_login(&exec, &cfg).unwrap(); assert!(exec.root.join("etc/greetd/config.toml").is_file()); // The installer shortcut is live-medium only; an installed system must // not advertise a reinstall entry point. assert!(!exec.root.join("usr/local/bin/phoebus-install").exists()); assert!(!exec .root .join("usr/share/applications/phoebus-install.desktop") .exists()); let login = std::fs::read_to_string(exec.root.join("etc/greetd/config.toml")).unwrap(); assert!(login.contains("phoebus-session-mango")); assert!(login.contains("tuigreet")); assert!( !login.contains("[initial_session]"), "installed login must not autologin" ); // greetd is enabled and the greeter account is created. let commands: Vec = runner.recorded().iter().map(|s| s.args.join(" ")).collect(); assert!(commands.iter().any(|a| a.contains("enable greetd"))); assert!(commands.iter().any(|a| a.contains("useradd"))); } #[test] fn live_autostart_is_gated_on_the_live_marker() { let runner = RecordingRunner::new(); let exec = make_exec(runner); let mut cfg = InstallConfig::default(); cfg.desktop.profile = DesktopProfile::Mango; write_session_script(&exec, &cfg).unwrap(); let mango = std::fs::read_to_string(exec.root.join("usr/local/bin/phoebus-session-mango")).unwrap(); assert!( mango.contains("/etc/phoebus/live"), "the welcome autostart must be gated on the live marker" ); assert!(mango.contains("phoebus-welcome")); } #[test] fn explicit_none_writes_no_login_configuration() { let runner = RecordingRunner::new(); let exec = make_exec(runner); let mut cfg = InstallConfig::default(); cfg.desktop.profile = DesktopProfile::None; configure_login(&exec, &cfg).unwrap(); assert!(!exec.root.join("etc/greetd/config.toml").exists()); } }