//! Pure generators and package sets shared by plan, validation and executor. //! //! Keeping these as pure functions makes the exact text written into the //! target unit-testable and keeps parity with the proven Bash output. use crate::config::{DesktopProfile, InstallConfig}; use crate::hardware::{CpuInfo, GpuVendor, HardwareReport}; // --------------------------------------------------------------------------- // Package sets // --------------------------------------------------------------------------- /// Packages every Phoebus system needs beyond the stage3. pub fn base_packages() -> Vec<&'static str> { vec![ "app-admin/sudo", "net-misc/networkmanager", "sys-auth/polkit", "sys-fs/cryptsetup", "sys-fs/btrfs-progs", ] } /// Extra packages implied by the detected CPU. pub fn kernel_extra_packages(cpu: &CpuInfo) -> Vec<&'static str> { let mut out = vec!["sys-kernel/linux-firmware"]; if cpu.has_feature("vmx") { out.push("sys-firmware/intel-microcode"); } if cpu.has_feature("svm") { out.push("sys-firmware/amd-ucode"); } out } pub fn desktop_packages(profile: DesktopProfile) -> Vec<&'static str> { match profile { DesktopProfile::None => vec![], DesktopProfile::Minimal => vec![ "gui-libs/wayland", "gui-libs/wayland-protocols", "x11-base/xwayland", ], DesktopProfile::Dwl | DesktopProfile::Full => vec![ "gui-wm/dwl", "gui-apps/waybar", "gui-apps/wofi", "gui-apps/mako", "gui-apps/swayidle", "gui-apps/swaylock", "gui-apps/swaybg", "gui-apps/wl-clipboard", "gui-apps/grim", "gui-apps/slurp", "gui-libs/gtk-layer-shell", "x11-base/xwayland", "x11-terms/kitty", "net-misc/networkmanager", "sys-auth/polkit", "gnome-extra/polkit-gnome", ], } .into_iter() .chain(if profile == DesktopProfile::Full { vec![ "sys-apps/flatpak", "net-wireless/bluez", "gnome-base/gnome-keyring", ] } else { vec![] }) .collect() } pub fn desktop_audio_packages() -> Vec<&'static str> { vec!["media-video/pipewire", "media-video/wireplumber"] } pub fn network_target_services() -> Vec<&'static str> { vec!["NetworkManager", "systemd-resolved"] } pub fn desktop_services(profile: DesktopProfile) -> Vec<&'static str> { let mut out = vec!["NetworkManager", "systemd-resolved"]; if profile == DesktopProfile::Full { out.push("bluetooth"); } out } /// The services that should be enabled in the target, deduplicated. pub fn target_services(config: &InstallConfig) -> Vec { let mut out: Vec = Vec::new(); for svc in network_target_services() .into_iter() .chain(desktop_services(config.desktop.profile)) { if !out.iter().any(|s| s == svc) { out.push(svc.to_string()); } } out } /// Gentoo profile constants. pub const PROFILE_SYSTEMD_MINIMAL: &str = "default/linux/amd64/23.0/systemd"; pub const PROFILE_SYSTEMD_DESKTOP: &str = "default/linux/amd64/23.0/desktop/systemd"; /// The Gentoo profile to use. /// /// A minimal systemd profile is correct for a core/server install. A desktop /// install should inherit from the supported stable desktop/systemd profile so /// that X/Wayland/dbus/policykit USE defaults are consistent instead of being /// reconstructed by hand. An explicit non-default profile always wins. pub fn selected_profile(config: &InstallConfig) -> String { let configured = &config.stage3.gentoo_profile; if config.desktop.profile != DesktopProfile::None && configured == PROFILE_SYSTEMD_MINIMAL { PROFILE_SYSTEMD_DESKTOP.to_string() } else { configured.clone() } } // --------------------------------------------------------------------------- // Portage configuration // --------------------------------------------------------------------------- pub fn makeopts(cores: u32, memory_mib: u64) -> 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)); format!("-j{jobs} -l{cores}") } pub fn video_cards(hardware: &HardwareReport) -> String { let mut cards: Vec = Vec::new(); for vendor in hardware.gpu_vendors() { match vendor { GpuVendor::Intel => cards.push("intel".into()), GpuVendor::Amd => { cards.push("amdgpu".into()); cards.push("radeonsi".into()); } GpuVendor::Nvidia => cards.push("nvidia".into()), GpuVendor::Unknown => {} } } if cards.is_empty() { cards.push("dummy".into()); } cards.join(" ") } pub fn make_conf(config: &InstallConfig, hardware: &HardwareReport) -> String { let mut o = String::new(); o.push_str(&format!( "# {} Portage configuration\n", config.system.distro_name )); o.push_str("# Generated by the installer. Safe to edit.\n\n"); o.push_str("COMMON_FLAGS=\"-O2 -pipe -march=native\"\n"); o.push_str("CFLAGS=\"${COMMON_FLAGS}\"\n"); o.push_str("CXXFLAGS=\"${COMMON_FLAGS}\"\n"); o.push_str("FCFLAGS=\"${COMMON_FLAGS}\"\n"); o.push_str("FFLAGS=\"${COMMON_FLAGS}\"\n\n"); o.push_str(&format!( "MAKEOPTS=\"{}\"\n", makeopts(hardware.cpu.cores, hardware.memory_mib) )); o.push_str("EMERGE_DEFAULT_OPTS=\"--binpkg-respect-use=y --with-bdeps=y --backtrack=100\"\n\n"); o.push_str("ACCEPT_KEYWORDS=\"amd64\"\n"); o.push_str(&format!( "ACCEPT_LICENSE=\"{}\"\n\n", config.packages.accept_license )); o.push_str("# Keep the global USE set close to the profile/binhost baseline so that\n"); o.push_str("# signed binaries can be reused; per-package USE lives in package.use.\n"); o.push_str("# dbus/policykit are part of the standard systemd desktop baseline and\n"); o.push_str("# are required by NetworkManager/wpa_supplicant and polkit respectively.\n"); o.push_str("USE=\"systemd dbus policykit\"\n"); o.push_str(&format!("VIDEO_CARDS=\"{}\"\n", video_cards(hardware))); o.push_str("INPUT_DEVICES=\"libinput\"\n"); o.push_str("LINGUAS=\"en\"\n"); o } pub fn repos_conf() -> String { let mut o = String::new(); o.push_str("[gentoo]\n"); o.push_str("location = /var/db/repos/gentoo\n"); o.push_str("sync-type = rsync\n"); o.push_str("sync-uri = rsync://rsync.gentoo.org/gentoo-portage\n"); o.push_str("auto-sync = yes\n"); o } 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("location = /var/cache/binhost/gentoo\n"); o.push_str("verify-signature = true\n"); o } pub fn package_use(config: &InstallConfig) -> String { let mut o = String::new(); o.push_str(&format!( "# {} installer policy\n", config.system.distro_name )); o.push_str("# Core system: systemd is the init system.\n"); o.push_str("# policykit is required by sys-auth/polkit[systemd];\n"); 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("sys-apps/dbus systemd\n"); o.push_str("sys-auth/polkit systemd\n"); o.push_str("sys-apps/util-linux systemd\n"); o.push_str("# Avoid the Sphinx/Pillow documentation chain for base storage tools;\n"); o.push_str("# its truetype/introspection edges form a circular dependency.\n"); o.push_str("sys-fs/btrfs-progs -man\n"); o.push_str("sys-fs/cryptsetup -man\n"); o.push_str("# Networking\n"); o.push_str("net-misc/networkmanager systemd\n"); 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"); if config.desktop.profile != DesktopProfile::None { o.push_str("# Desktop (Wayland-first, XWayland compatibility)\n"); o.push_str("x11-libs/cairo X\n"); o.push_str("x11-libs/gtk+ X wayland\n"); 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"); } o } pub fn accept_keywords_policy() -> String { let mut o = String::new(); o.push_str("# Phoebus: scoped testing keywords for hardware/desktop enablement.\n"); o.push_str("# This is NOT a global ~amd64 switch.\n"); o.push_str("sys-kernel/linux-firmware\n"); o.push_str("gui-wm/dwl\n"); o } pub fn install_conf() -> String { let mut o = String::new(); o.push_str("# /etc/kernel/install.conf - Gentoo installkernel\n"); o.push_str("# When /etc/kernel/install.conf exists it is used INSTEAD of the\n"); o.push_str("# package default in /usr/lib/kernel/install.conf, so every required\n"); o.push_str("# generator must be listed here.\n"); o.push_str("# installkernel always requires uki_generator when dracut generates\n"); o.push_str("# the initramfs; \"none\" disables UKI assembly for the BLS layout.\n"); o.push_str("layout=bls\n"); o.push_str("initrd_generator=dracut\n"); o.push_str("uki_generator=none\n"); o } pub fn dracut_conf(config: &InstallConfig) -> String { let mut o = String::new(); o.push_str(&format!( "# {} dracut configuration\n", config.system.distro_name )); o.push_str("# Generic (non-hostonly) initramfs for predictable boot behaviour.\n"); o.push_str("hostonly=\"no\"\n"); o.push_str("compress=\"zstd\"\n"); o.push_str("add_dracutmodules+=\" crypt btrfs \"\n"); o } pub fn kernel_profile_config(config: &InstallConfig) -> String { let profile = config.kernel.profile.as_str(); let mut o = String::new(); o.push_str(&format!( "# {} kernel profile: {profile}\n", config.system.distro_name )); o.push_str("# Applied on top of the Gentoo distribution kernel configuration.\n"); o.push_str("# The base configuration is intentionally kept: it supports common\n"); o.push_str("# peripherals the user may attach later.\n\n"); o.push_str("# Storage / modern filesystems and encryption\n"); for line in [ "CONFIG_BTRFS_FS=y", "CONFIG_BTRFS_FS_POSIX_ACL=y", "CONFIG_DM_CRYPT=y", "CONFIG_DM_INTEGRITY=y", "CONFIG_NVME_CORE=y", "CONFIG_BLK_DEV_NVME=y", ] { o.push_str(line); o.push('\n'); } o.push_str("\n# Input / USB / common buses\n"); for line in [ "CONFIG_HID_GENERIC=y", "CONFIG_USB_HID=y", "CONFIG_USB_XHCI_HCD=y", ] { o.push_str(line); o.push('\n'); } o.push_str("\n# systemd / containers / virtualisation guests\n"); for line in [ "CONFIG_CGROUPS=y", "CONFIG_NAMESPACES=y", "CONFIG_VIRTIO=y", "CONFIG_VIRTIO_PCI=y", "CONFIG_VIRTIO_BLK=y", "CONFIG_VIRTIO_NET=y", ] { o.push_str(line); o.push('\n'); } o.push_str("\n# Compression used by Btrfs\n"); for line in ["CONFIG_ZSTD_COMPRESS=y", "CONFIG_ZSTD_DECOMPRESS=y"] { o.push_str(line); o.push('\n'); } o } /// Btrfs mount options, matching the proven behavior. pub fn btrfs_mount_opts(compress: &str, ssd: bool) -> String { let mut opts = format!("noatime,compress={compress},space_cache=v2"); if ssd { opts.push_str(",ssd"); } opts } // --------------------------------------------------------------------------- // fstab / crypttab / cmdline // --------------------------------------------------------------------------- pub fn fstab( config: &InstallConfig, root_source: &str, efi_uuid: Option<&str>, btrfs_opts: &str, ) -> String { let mut o = String::new(); o.push_str(&format!("# /etc/fstab: {}\n", config.system.distro_name)); o.push_str("# Generated by the installer. Use `findmnt --verify` to check.\n#\n"); o.push_str("# \t\t\t\t\t\n"); if let Some(uuid) = efi_uuid { o.push_str(&format!( "UUID={uuid}\t{}\tvfat\tumask=0077,shortname=winnt\t0\t2\n", config.storage.efi_mount )); } for (subvol, mountpoint, pass) in [ ("@", "/", 1), ("@home", "/home", 2), ("@var", "/var", 2), ("@snapshots", "/.snapshots", 2), ("@swap", "/swap", 2), ] { o.push_str(&format!( "{root_source}\t{mountpoint}\tbtrfs\t{btrfs_opts},subvol={subvol}\t0\t{pass}\n" )); } o.push_str("/swap/swapfile\tnone\tswap\tdefaults\t0\t0\n"); o } pub fn crypttab(name: &str, part_uuid: &str) -> String { let mut o = String::new(); o.push_str("# /etc/crypttab: unlock the root volume at boot.\n"); o.push_str("# \t\t\t\n"); o.push_str(&format!("{name} UUID={part_uuid} none luks,discard\n")); o } /// The kernel command line. /// /// `luks_uuid` is required when encryption is on; `root_source` is used for an /// unencrypted Btrfs root (a UUID= reference). pub fn kernel_cmdline( config: &InstallConfig, luks_uuid: Option<&str>, root_source: &str, ) -> Result { let mut cmdline = String::new(); if config.is_encrypted() { let uuid = luks_uuid.ok_or_else(|| "missing LUKS UUID".to_string())?; let name = &config.encryption.name; // rd.luks.name== makes dracut/systemd create the mapper // under the configured name; rd.luks.uuid alone yields luks-. cmdline.push_str(&format!("rd.luks.name={uuid}={name} ")); cmdline.push_str(&format!("root=/dev/mapper/{name} ")); } else { if root_source.is_empty() { return Err("unencrypted cmdline requires an explicit root source".into()); } cmdline.push_str(&format!("root={root_source} ")); } cmdline.push_str("rootfstype=btrfs rootflags=subvol=@ rw quiet"); if !config.kernel.console.is_empty() { cmdline.push(' '); cmdline.push_str(&config.kernel.console); } Ok(cmdline) } pub fn locale_gen_line(locale: &str) -> String { format!("{locale} UTF-8\n") } #[cfg(test)] mod tests { use super::*; use crate::config::{DesktopProfile, Encryption, InstallConfig}; use crate::hardware::CpuInfo; #[test] fn base_packages_include_storage_tools() { let pkgs = base_packages(); assert!(pkgs.contains(&"sys-fs/cryptsetup")); assert!(pkgs.contains(&"sys-fs/btrfs-progs")); assert!(pkgs.contains(&"net-misc/networkmanager")); } #[test] fn kernel_extras_follow_cpu_features() { let cpu = CpuInfo { features: vec!["vmx".into()], ..Default::default() }; let pkgs = kernel_extra_packages(&cpu); assert!(pkgs.contains(&"sys-kernel/linux-firmware")); assert!(pkgs.contains(&"sys-firmware/intel-microcode")); assert!(!pkgs.contains(&"sys-firmware/amd-ucode")); } #[test] fn fstab_mounts_all_subvolumes_including_swap() { let cfg = InstallConfig::default(); let text = fstab( &cfg, "/dev/mapper/cryptroot", Some("EFI-UUID"), "noatime,compress=zstd:3,space_cache=v2", ); assert!(text.contains("UUID=EFI-UUID\t/efi\tvfat")); assert!(text.contains("\t/\tbtrfs\t")); assert!(text.contains("subvol=@home")); assert!(text.contains("subvol=@var")); assert!(text.contains("subvol=@snapshots")); assert!(text.contains("subvol=@swap")); assert!(text.contains("/swap/swapfile\tnone\tswap")); } #[test] fn crypttab_uses_partition_uuid() { let text = crypttab("cryptroot", "0000-1111"); assert!(text.contains("cryptroot UUID=0000-1111 none luks,discard")); } #[test] fn cmdline_uses_rd_luks_name() { let cfg = InstallConfig::default(); let cmd = kernel_cmdline(&cfg, Some("0000-1111"), "").unwrap(); assert!(cmd.contains("rd.luks.name=0000-1111=cryptroot")); assert!(cmd.contains("root=/dev/mapper/cryptroot")); assert!(!cmd.contains("rd.luks.uuid=")); assert!(cmd.contains("console=tty0")); } #[test] fn install_conf_has_uki_generator_none() { let text = install_conf(); assert!(text.contains("layout=bls")); assert!(text.contains("initrd_generator=dracut")); assert!(text.contains("uki_generator=none")); } #[test] fn desktop_none_has_no_x_use_block() { let mut cfg = InstallConfig::default(); cfg.desktop.profile = DesktopProfile::None; let text = package_use(&cfg); assert!(!text.contains("x11-libs/cairo X")); cfg.desktop.profile = DesktopProfile::Dwl; let text = package_use(&cfg); assert!(text.contains("x11-libs/cairo X")); assert!(text.contains("media-libs/libglvnd X")); assert!(text.contains("sys-fs/btrfs-progs -man")); assert!(text.contains("sys-fs/cryptsetup -man")); } #[test] fn make_conf_does_not_globally_enable_testing() { let cfg = InstallConfig::default(); let hw = crate::hardware::HardwareReport::default(); let text = make_conf(&cfg, &hw); assert!(text.contains("ACCEPT_KEYWORDS=\"amd64\"")); assert!(!text.contains("~amd64")); assert!(text.contains("--backtrack=100")); } #[test] fn desktop_selects_desktop_systemd_profile() { let mut cfg = InstallConfig::default(); assert_eq!(selected_profile(&cfg), PROFILE_SYSTEMD_MINIMAL); cfg.desktop.profile = DesktopProfile::Dwl; assert_eq!(selected_profile(&cfg), PROFILE_SYSTEMD_DESKTOP); // An explicit non-default profile is respected. cfg.stage3.gentoo_profile = "default/linux/amd64/23.0/hardened/systemd".into(); assert_eq!( selected_profile(&cfg), "default/linux/amd64/23.0/hardened/systemd" ); } #[test] fn services_deduplicated() { let mut cfg = InstallConfig::default(); cfg.desktop.profile = DesktopProfile::Full; let svcs = target_services(&cfg); assert!(svcs.contains(&"NetworkManager".to_string())); assert!(svcs.contains(&"bluetooth".to_string())); let count = svcs.iter().filter(|s| *s == "NetworkManager").count(); assert_eq!(count, 1); } #[test] fn encrypted_cmdline_requires_uuid() { let cfg = InstallConfig::default(); assert_eq!(cfg.encryption.mode, Encryption::Luks2); assert!(kernel_cmdline(&cfg, None, "").is_err()); } }