diff --git a/installer-rs/crates/phoebus-core/src/config.rs b/installer-rs/crates/phoebus-core/src/config.rs index cdd08a6..3e82e93 100644 --- a/installer-rs/crates/phoebus-core/src/config.rs +++ b/installer-rs/crates/phoebus-core/src/config.rs @@ -282,12 +282,33 @@ impl KernelConfig { } } +/// A scoped `package.use` entry: one atom and one flag. Used by the installer +/// defaults and by validated AI proposals. +#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] +#[serde(deny_unknown_fields)] +pub struct PackageUse { + pub atom: String, + pub flag: String, + #[serde(default = "default_enabled")] + pub enabled: bool, +} + +fn default_enabled() -> bool { + true +} + #[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] #[serde(default)] pub struct PackagesConfig { pub policy: PackagePolicy, pub binary_packages: bool, pub accept_license: String, + /// Explicit `MAKEOPTS -j` value; `None` derives it from cores and memory. + #[serde(default)] + pub build_jobs: Option, + /// Extra scoped USE entries appended to `package.use`. + #[serde(default)] + pub package_use: Vec, } impl Default for PackagesConfig { @@ -296,6 +317,8 @@ impl Default for PackagesConfig { policy: PackagePolicy::Balanced, binary_packages: true, accept_license: "@FREE @BINARY-REDISTRIBUTABLE".into(), + build_jobs: None, + package_use: Vec::new(), } } } @@ -473,6 +496,13 @@ impl InstallConfig { "PACKAGE_POLICY" => self.packages.policy = PackagePolicy::from_str(value)?, "FEATURES_BINPKG" => self.packages.binary_packages = parse_bool(value), "ACCEPT_LICENSE" => self.packages.accept_license = value.into(), + "BUILD_JOBS" => { + self.packages.build_jobs = if value.trim().is_empty() { + None + } else { + Some(parse_num(key, value)?) + } + } "DESKTOP_PROFILE" => self.desktop.profile = DesktopProfile::from_str(value)?, "FLATPAK" => self.desktop.flatpak = parse_bool(value), diff --git a/installer-rs/crates/phoebus-core/src/system.rs b/installer-rs/crates/phoebus-core/src/system.rs index bfb38a4..2fbc99d 100644 --- a/installer-rs/crates/phoebus-core/src/system.rs +++ b/installer-rs/crates/phoebus-core/src/system.rs @@ -126,10 +126,15 @@ pub fn selected_profile(config: &InstallConfig) -> String { // Portage configuration // --------------------------------------------------------------------------- -pub fn makeopts(cores: u32, memory_mib: u64) -> String { +pub fn makeopts(cores: u32, memory_mib: u64, build_jobs: Option) -> String { let cores = if cores == 0 { 4 } else { cores }; - let mem_jobs = std::cmp::max(1, memory_mib / 2048); - let jobs = std::cmp::max(1, std::cmp::min(cores + 1, mem_jobs as u32)); + let mem_jobs = std::cmp::max(1, memory_mib / 2048) as u32; + let derived = std::cmp::max(1, std::cmp::min(cores + 1, mem_jobs)); + // An explicit request is still capped by available memory so a proposal + // cannot make the build exhaust RAM. + let jobs = build_jobs + .map(|j| std::cmp::max(1, std::cmp::min(j, mem_jobs))) + .unwrap_or(derived); format!("-j{jobs} -l{cores}") } @@ -166,7 +171,11 @@ pub fn make_conf(config: &InstallConfig, hardware: &HardwareReport) -> String { o.push_str("FFLAGS=\"${COMMON_FLAGS}\"\n\n"); o.push_str(&format!( "MAKEOPTS=\"{}\"\n", - makeopts(hardware.cpu.cores, hardware.memory_mib) + makeopts( + hardware.cpu.cores, + hardware.memory_mib, + config.packages.build_jobs + ) )); o.push_str("EMERGE_DEFAULT_OPTS=\"--binpkg-respect-use=y --with-bdeps=y --backtrack=100\"\n\n"); o.push_str("ACCEPT_KEYWORDS=\"amd64\"\n"); @@ -195,11 +204,43 @@ pub fn repos_conf() -> String { o } +pub const BINHOST_OPENRC: &str = + "https://distfiles.gentoo.org/releases/amd64/binpackages/23.0/x86-64"; +pub const BINHOST_HARDENED: &str = + "https://distfiles.gentoo.org/releases/amd64/binpackages/23.0/x86-64_hardened"; + +/// The official signed binhost matching a profile family. +/// +/// Gentoo publishes binary packages for the OpenRC and hardened profiles. There +/// is no official systemd binhost, so a systemd system reuses binaries only for +/// packages whose USE matches the OpenRC/hardened build; systemd, polkit, +/// NetworkManager, dbus and util-linux still compile from source. Enabling +/// `systemd` in the global USE therefore does not force needless rebuilds of +/// packages that lack the flag, and this stays a scoped (non-`~amd64`) change. +pub fn binhost_for_profile(profile: &str) -> &'static str { + if profile.contains("hardened") { + BINHOST_HARDENED + } else { + BINHOST_OPENRC + } +} + +/// The binhost URI actually used: an explicit configuration value, otherwise +/// the official host for the selected profile. +pub fn resolved_binhost_uri(config: &InstallConfig) -> String { + let configured = config.stage3.binhost_uri.trim(); + if configured.is_empty() { + binhost_for_profile(&selected_profile(config)).to_string() + } else { + configured.to_string() + } +} + pub fn binrepos_conf(config: &InstallConfig) -> String { let mut o = String::new(); o.push_str("[gentoo]\n"); o.push_str("priority = 1\n"); - o.push_str(&format!("sync-uri = {}\n", config.stage3.binhost_uri)); + o.push_str(&format!("sync-uri = {}\n", resolved_binhost_uri(config))); o.push_str("location = /var/cache/binhost/gentoo\n"); o.push_str("verify-signature = true\n"); o @@ -216,8 +257,11 @@ pub fn package_use(config: &InstallConfig) -> String { o.push_str("# cryptsetup builds systemd-cryptsetup for the encrypted root initramfs.\n"); o.push_str("sys-apps/systemd boot policykit cryptsetup\n"); o.push_str("# Distribution kernel + systemd-boot generation.\n"); - o.push_str("# installkernel REQUIRED_USE: systemd-boot? ( systemd ), ukify? ( uki )\n"); - o.push_str("sys-kernel/installkernel dracut systemd systemd-boot uki\n"); + o.push_str("# installkernel REQUIRED_USE: systemd-boot? ( systemd );\n"); + o.push_str("# the `uki` flag is NOT set: this system uses the BLS layout with\n"); + o.push_str("# uki_generator=none, so ukify is never invoked and its extra\n"); + o.push_str("# dependencies (and binary-package mismatch) are avoided.\n"); + o.push_str("sys-kernel/installkernel dracut systemd systemd-boot\n"); o.push_str("sys-apps/dbus systemd\n"); o.push_str("sys-auth/polkit systemd\n"); o.push_str("sys-apps/util-linux systemd\n"); @@ -227,9 +271,17 @@ pub fn package_use(config: &InstallConfig) -> String { o.push_str("sys-fs/cryptsetup -man\n"); o.push_str("# Networking\n"); o.push_str("net-misc/networkmanager systemd\n"); + // Audio (PipeWire + WirePlumber are the standard stack). Emit pipewire + // once; the previous duplicate entry is gone. `systemd` is added only for + // the desktop profile that installs PipeWire. o.push_str("# Audio (PipeWire + WirePlumber are the standard stack)\n"); - o.push_str("media-video/pipewire sound-server\n"); - o.push_str("media-video/wireplumber\n"); + let pipewire = if config.desktop.profile == DesktopProfile::None { + "media-video/pipewire sound-server".to_string() + } else { + "media-video/pipewire sound-server systemd".to_string() + }; + o.push_str(&pipewire); + o.push('\n'); if config.desktop.profile != DesktopProfile::None { o.push_str("# Desktop (Wayland-first, XWayland compatibility)\n"); o.push_str("x11-libs/cairo X\n"); @@ -237,7 +289,14 @@ pub fn package_use(config: &InstallConfig) -> String { o.push_str("gui-libs/gtk wayland\n"); o.push_str("media-libs/libglvnd X\n"); o.push_str("x11-terms/kitty wayland\n"); - o.push_str("media-video/pipewire systemd\n"); + } + // Scoped use entries from validated configuration/AI proposals. + if !config.packages.package_use.is_empty() { + o.push_str("# Accepted scoped USE changes\n"); + for entry in &config.packages.package_use { + let prefix = if entry.enabled { "" } else { "-" }; + o.push_str(&format!("{} {}{}\n", entry.atom, prefix, entry.flag)); + } } o } @@ -618,6 +677,61 @@ mod tests { assert!(text.contains("--backtrack=100")); } + #[test] + fn makeopts_caps_explicit_jobs_by_memory() { + assert_eq!(makeopts(8, 8192, None), "-j4 -l8"); + assert_eq!(makeopts(8, 8192, Some(2)), "-j2 -l8"); + assert_eq!(makeopts(8, 8192, Some(99)), "-j4 -l8"); + assert_eq!(makeopts(0, 0, None), "-j1 -l4"); + } + + #[test] + fn package_use_is_clean_and_keeps_required_flags() { + let mut cfg = InstallConfig::default(); + cfg.desktop.profile = DesktopProfile::Dwl; + let text = package_use(&cfg); + // Required boot/encryption/session flags stay. + assert!(text.contains("sys-apps/systemd boot policykit cryptsetup")); + assert!(text.contains("sys-kernel/installkernel dracut systemd systemd-boot")); + // BLS does not need the `uki` flag, and the empty wireplumber line and + // the duplicate pipewire line are gone. + assert!(!text.contains(" systemd-boot uki")); + assert!(!text.contains("media-video/wireplumber\n")); + assert_eq!(text.matches("media-video/pipewire").count(), 1); + assert!(text.contains("media-video/pipewire sound-server systemd")); + } + + #[test] + fn package_use_appends_validated_entries() { + let mut cfg = InstallConfig::default(); + cfg.packages.package_use.push(crate::config::PackageUse { + atom: "www-client/firefox".into(), + flag: "wayland".into(), + enabled: false, + }); + let text = package_use(&cfg); + assert!(text.contains("www-client/firefox -wayland")); + } + + #[test] + fn binhost_selection_follows_profile() { + assert_eq!(binhost_for_profile(PROFILE_SYSTEMD_MINIMAL), BINHOST_OPENRC); + assert_eq!( + binhost_for_profile("default/linux/amd64/23.0/hardened/systemd"), + BINHOST_HARDENED + ); + let mut cfg = InstallConfig::default(); + cfg.stage3.binhost_uri.clear(); + cfg.stage3.gentoo_profile = "default/linux/amd64/23.0/hardened/systemd".into(); + assert_eq!(resolved_binhost_uri(&cfg), BINHOST_HARDENED); + cfg.stage3.binhost_uri = "https://example.invalid/binpkgs".into(); + assert_eq!( + resolved_binhost_uri(&cfg), + "https://example.invalid/binpkgs" + ); + assert!(binrepos_conf(&cfg).contains("verify-signature = true")); + } + #[test] fn desktop_selects_desktop_systemd_profile() { let mut cfg = InstallConfig::default(); diff --git a/installer-rs/crates/phoebus-core/src/validate.rs b/installer-rs/crates/phoebus-core/src/validate.rs index 848f531..e743713 100644 --- a/installer-rs/crates/phoebus-core/src/validate.rs +++ b/installer-rs/crates/phoebus-core/src/validate.rs @@ -139,6 +139,38 @@ pub fn password_strength(password: &str) -> &'static str { } } +/// Validate a package atom: `category/name`, with only safe characters. +pub fn validate_package_atom(atom: &str) -> Result<(), String> { + if atom.split('/').count() != 2 || atom.is_empty() { + return Err(format!("package atom must be category/name: {atom}")); + } + if !atom + .chars() + .all(|c| c.is_ascii_alphanumeric() || "/*._+-".contains(c)) + { + return Err(format!("invalid package atom: {atom}")); + } + Ok(()) +} + +/// Validate a USE flag. A leading `-` disables the flag; wildcards are refused +/// so a proposal cannot change flags across every package. +pub fn validate_use_flag(flag: &str) -> Result<(), String> { + if flag.contains('*') { + return Err("wildcard USE flags are not allowed".into()); + } + let name = flag.strip_prefix('-').unwrap_or(flag); + if name.is_empty() + || name.len() > 64 + || !name + .chars() + .all(|c| c.is_ascii_alphanumeric() || c == '_' || c == '-' || c == '+' || c == '@') + { + return Err(format!("invalid USE flag: {flag}")); + } + Ok(()) +} + /// Validate the whole configuration. Disk protection is checked separately /// against probed hardware at preflight time. pub fn validate_config(config: &InstallConfig) -> Vec { @@ -200,6 +232,22 @@ pub fn validate_config(config: &InstallConfig) -> Vec { "ACCEPT_LICENSE must not be empty", )); } + if let Some(jobs) = config.packages.build_jobs { + if jobs == 0 || jobs > 256 { + issues.push(ValidationIssue::new( + "build_jobs", + "build jobs must be between 1 and 256", + )); + } + } + for entry in &config.packages.package_use { + if let Err(e) = validate_package_atom(&entry.atom) { + issues.push(ValidationIssue::new("package_use", e)); + } + if let Err(e) = validate_use_flag(&entry.flag) { + issues.push(ValidationIssue::new("package_use", e)); + } + } issues } @@ -243,6 +291,29 @@ mod tests { assert!(fields.contains(&"target_disk")); } + #[test] + fn package_use_and_jobs_are_validated() { + assert!(validate_package_atom("www-client/firefox").is_ok()); + assert!(validate_package_atom("firefox").is_err()); + assert!(validate_package_atom("sys-fs/btrfs-progs").is_ok()); + assert!(validate_use_flag("-man").is_ok()); + assert!(validate_use_flag("systemd").is_ok()); + assert!(validate_use_flag("*").is_err()); + assert!(validate_use_flag("").is_err()); + assert!(validate_use_flag("bad flag").is_err()); + + let mut cfg = InstallConfig::default(); + cfg.packages.build_jobs = Some(0); + cfg.packages.package_use.push(crate::config::PackageUse { + atom: "notanatatom".into(), + flag: "*.x".into(), + enabled: true, + }); + let fields: Vec<&str> = validate_config(&cfg).iter().map(|i| i.field).collect(); + assert!(fields.contains(&"build_jobs")); + assert!(fields.contains(&"package_use")); + } + #[test] fn complete_config_passes() { let mut cfg = InstallConfig::default(); diff --git a/installer-rs/crates/phoebus-installer/src/orchestrate.rs b/installer-rs/crates/phoebus-installer/src/orchestrate.rs index d5c21c7..7634f7a 100644 --- a/installer-rs/crates/phoebus-installer/src/orchestrate.rs +++ b/installer-rs/crates/phoebus-installer/src/orchestrate.rs @@ -39,8 +39,8 @@ impl Installer { load_history(&volatile) }; let engine = PackagePolicyEngine::new().with_history(history); - let binhost_available = - config.packages.binary_packages && !config.stage3.binhost_uri.is_empty(); + let binhost_available = config.packages.binary_packages + && !phoebus_core::system::resolved_binhost_uri(&config).is_empty(); let policy_ctx = PolicyContext { policy: config.packages.policy, binhost_available,