phoebus_os/installer/lib/pkgpolicy.sh
Phoebus 38214ea9e7 chore: import Phoebus OS installer and Rust migration
Initial public import of the proven Bash installer (installer/) and the production Rust workspace (installer-rs/). The reference ISO, build output and local agent transcripts are gitignored.

Verified: installer/tests/run_tests.sh and cargo test --workspace both pass.
2026-09-20 02:07:33 -07:00

156 lines
4.8 KiB
Bash

#!/usr/bin/env bash
# shellcheck shell=bash
#
# Package compilation policy.
#
# The rule is deliberately deterministic:
#
# compile when compilation is cheap OR gives a meaningful benefit;
# use a binary when compilation is expensive AND the benefit is negligible.
#
# Decisions are driven by a small curated table plus optional historical
# compile-time data. An LLM is never consulted to guess performance.
#
# Historical data format (tab separated), one package per line:
# category/package<TAB>seconds<TAB>updates-per-year
# Overrides the built-in estimate.
declare -gA PKGPOLICY_COMPILE_SECONDS=(
[app-editors/vim]=45
[app-misc/tmux]=20
[app-shells/zsh]=60
[dev-lang/python]=900
[dev-lang/rust]=3600
[dev-libs/glib]=120
[dev-util/cmake]=300
[dev-vcs/git]=120
[media-video/ffmpeg]=900
[net-misc/curl]=90
[sys-apps/busybox]=60
[sys-apps/coreutils]=120
[sys-apps/systemd]=420
[sys-devel/gcc]=3600
[sys-kernel/gentoo-kernel]=1800
[sys-kernel/gentoo-sources]=120
[sys-libs/glibc]=900
[www-client/firefox]=4200
[www-client/chromium]=7200
[app-office/libreoffice]=5400
[dev-qt/qtbase]=900
[kde-plasma/plasma-desktop]=600
[net-im/discord-bin]=0
[games-util/steam-launcher]=0
)
# Packages where a binary package is strongly preferred (huge + frequent
# updates + negligible local optimisation benefit + runtime CPU dispatch).
declare -gA PKGPOLICY_BINARY_PREFERRED=(
[www-client/firefox]=1
[www-client/chromium]=1
[app-office/libreoffice]=1
[mail-client/thunderbird]=1
[dev-lang/rust]=1
[dev-lang/llvm]=1
[dev-qt/qtwebengine]=1
[sys-devel/gcc]=1
[sys-libs/glibc]=1
[net-im/discord-bin]=1
[games-util/steam-launcher]=1
)
# Packages that genuinely benefit from local optimisation or USE customisation.
declare -gA PKGPOLICY_SOURCE_PREFERRED=(
[sys-kernel/gentoo-kernel]=1
[sys-kernel/gentoo-sources]=1
[sys-apps/systemd]=1
[app-shells/zsh]=1
[sys-apps/busybox]=1
[sys-fs/btrfs-progs]=1
[sys-fs/cryptsetup]=1
)
PKGPOLICY_HISTORY_FILE=""
pkgpolicy_history_file() {
printf '%s' "${PKGPOLICY_HISTORY_FILE:-${STATE_DIR:-/tmp/ai-installer}/compile-times.tsv}"
}
pkgpolicy_compile_seconds() {
local pkg="$1" hist line
hist="$(pkgpolicy_history_file)"
if [[ -r "$hist" ]]; then
line="$(awk -F'\t' -v p="$pkg" '$1==p{print $2; exit}' "$hist")"
if [[ "$line" =~ ^[0-9]+$ ]]; then printf '%s' "$line"; return; fi
fi
printf '%s' "${PKGPOLICY_COMPILE_SECONDS[$pkg]:-300}"
}
pkgpolicy_updates_per_year() {
local pkg="$1" hist line
hist="$(pkgpolicy_history_file)"
if [[ -r "$hist" ]]; then
line="$(awk -F'\t' -v p="$pkg" '$1==p{print $3; exit}' "$hist")"
if [[ "$line" =~ ^[0-9]+$ ]]; then printf '%s' "$line"; return; fi
fi
printf '6'
}
# pkgpolicy_binary_available PKG - a binary can plausibly be fetched
pkgpolicy_binary_available() {
[[ -n "$(config_get BINHOST_URI)" ]] || return 1
config_bool FEATURES_BINPKG
}
# pkgpolicy_decide PKG - prints "source" or "binary"
pkgpolicy_decide() {
local pkg="$1" policy seconds mem cores
policy="$(config_get PACKAGE_POLICY)"
seconds="$(pkgpolicy_compile_seconds "$pkg")"
mem="$(hw_mem_total_mib)"; [[ -n "$mem" ]] || mem=4096
cores="$(hw_cpu_cores)"; [[ -n "$cores" ]] || cores=2
case "$policy" in
source_first)
printf 'source'; return ;;
binary_first)
if pkgpolicy_binary_available; then printf 'binary'; else printf 'source'; fi
return ;;
esac
# balanced:
# - explicit preferences win;
# - cheap builds are compiled locally;
# - everything else uses a signed Gentoo binary when one is available.
# This is deterministic and avoids guessing performance where no data
# exists. Portage still builds from source whenever USE does not match the
# binary (--binpkg-respect-use=y).
if [[ -n "${PKGPOLICY_BINARY_PREFERRED[$pkg]:-}" ]]; then
pkgpolicy_binary_available && { printf 'binary'; return; }
fi
if [[ -n "${PKGPOLICY_SOURCE_PREFERRED[$pkg]:-}" ]]; then
printf 'source'; return
fi
if ((seconds <= 120)); then printf 'source'; return; fi
if pkgpolicy_binary_available; then printf 'binary'; return; fi
printf 'source'
}
# pkgpolicy_emerge_flags PKG - extra emerge flags for a package
pkgpolicy_emerge_flags() {
local pkg="$1" decision
decision="$(pkgpolicy_decide "$pkg")"
if [[ "$decision" == "binary" ]]; then
printf '%s\n' --getbinpkg --binpkg-respect-use=y
fi
}
# pkgpolicy_report [PKGS...] - human readable policy decisions
pkgpolicy_report() {
local -a pkgs=("$@") pkg
((${#pkgs[@]})) || pkgs=(sys-apps/systemd sys-devel/gcc www-client/firefox sys-kernel/gentoo-kernel app-editors/vim)
printf 'Policy: %s\n\n' "$(config_get PACKAGE_POLICY)"
printf '%-32s %8s %s\n' "package" "est(s)" "decision"
for pkg in "${pkgs[@]}"; do
printf '%-32s %8s %s\n' "$pkg" "$(pkgpolicy_compile_seconds "$pkg")" "$(pkgpolicy_decide "$pkg")"
done
}