//! Desktop profile installation. //! //! The Phoebus desktop is Mango (wlroots tiling compositor) + DarkMaterial //! (DankMaterialShell), 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_in_stage( exec, "desktop_install", Path::new(&exec.root), &packages, engine, ctx, ) } /// Write `/usr/local/bin/phoebus-session-mango` and the wrapper `/usr/local/bin/phoebus-session`. 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, )?; target.write( "usr/share/applications/phoebus-assistant.desktop", sysgen::PHOEBUS_ASSISTANT_DESKTOP_ENTRY, )?; // 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)?; } DesktopProfile::None => {} } 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, )?; // Convenience wrapper so manual VT invocation launches the canonical session. write_script( target, "usr/local/bin/phoebus-session", "#!/bin/bash\nexec /usr/local/bin/phoebus-session-mango \"$@\"\n", )?; 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() { portage::chroot(exec, root, "systemctl", &["enable", service]).map_err(|e| { ExecError::operation( "desktop", &format!("enable login service {service}"), e.to_string(), ) })?; } exec.logger .info(format!("configured greetd login (default session {cmd})")); Ok(()) } fn ensure_greeter_user(exec: &Exec) -> Result<()> { let passwd_path = Path::new(&exec.root).join("etc/passwd"); if let Ok(passwd) = std::fs::read_to_string(&passwd_path) { if passwd.lines().any(|l| l.starts_with("greeter:")) { exec.logger.info("greeter user already present in target"); return Ok(()); } } let root = Path::new(&exec.root); portage::chroot( exec, root, "useradd", &[ "--system", "--no-create-home", "--home-dir", "/var/lib/greetd", "--shell", "/sbin/nologin", "--comment", "Phoebus greeter", "greeter", ], ) .map_err(|e| ExecError::operation("desktop", "create greeter user", e.to_string()))?; exec.logger.info("created the greeter user"); Ok(()) } 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(()); } portage::chroot( exec, Path::new(&exec.root), "flatpak", &[ "remote-add", "--if-not-exists", "flathub", "https://flathub.org/repo/flathub.flatpakrepo", ], ) .map_err(|e| ExecError::operation("desktop", "add flathub remote", e.to_string()))?; 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 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()); assert!(!exec .root .join("usr/local/bin/phoebus-session-mango") .exists()); // An explicit `none` also writes no assistant launcher. assert!(!exec.root.join("usr/local/bin/phoebus-assistant").exists()); } #[test] fn mango_profile_installs_canonical_session_and_no_dwl_artifacts() { 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", ] { assert!(exec.root.join(rel).is_file(), "{rel} was not installed"); } // Standalone dwl session entry and helpers must NOT be installed. for rel in [ "usr/share/wayland-sessions/phoebus-dwl.desktop", "usr/local/bin/phoebus-menu", "usr/local/bin/phoebus-lock", "usr/local/bin/phoebus-idle", "etc/xdg/waybar/config", "etc/xdg/waybar/style.css", ] { assert!(!exec.root.join(rel).exists(), "{rel} must not be 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 session = std::fs::read_to_string(exec.root.join("usr/local/bin/phoebus-session")).unwrap(); assert!( session.contains("phoebus-session-mango"), "phoebus-session wrapper must invoke phoebus-session-mango" ); assert!( !session.contains("dwl &"), "standalone dwl must not be in phoebus-session" ); } #[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()); } }