diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index 7f8e206..132463b 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -7,6 +7,13 @@ Format: Keep a Changelog. Dates are UTC. ## Unreleased +### Added +- **Nix bootstrap (gated).** `phoebus-executor::nix` fetches the pinned, + SHA-256-verified official Nix release and bootstraps it into the target with + discrete commands (store copy, `nix-store --load-db`, `nixbld` users, profile + and daemon units). `packages.nix` defaults off until a VM verifies it. A + binary ebuild was ruled out because the upstream installer has no `DESTDIR`. + ### Changed - **Installing Phoebus now requires an AI.** The installer refuses an install with no provider unless the explicit break-glass `--no-ai` (alias `--legacy`, diff --git a/docs/ROADMAP.md b/docs/ROADMAP.md index 0d84210..98f98f9 100644 --- a/docs/ROADMAP.md +++ b/docs/ROADMAP.md @@ -22,7 +22,7 @@ more interesting. Status vocabulary: **Done**, **Partial**, **Next**, **Later**. | 15 | Recovery environment | Later | recovery BLS entry generated; full recovery image not built | | — | Rust TUI | Partial | ratatui UI with plan/ERASE and AI provider selection; not yet the default on all paths | | — | AI-guided install optimization | Partial | DeepSeek/OpenAI/local providers, evidence-gated typed review, mandatory at install with `--no-ai` break-glass; fixture-verified, no live call | -| — | Nix as a built-in second lane | Next | `sys-apps/nix` Portage overlay + `nix-daemon`; installer wiring; not yet packaged | +| — | Nix as a built-in second lane | Partial | pinned-release bootstrap in the target chroot with `nix-daemon`; `packages.nix` gated off pending VM verification | | — | Git workflow + agent guide | Done | this `docs/` set, `.githooks/`, `scripts/` | ## Immediate next steps diff --git a/docs/adr/0006-package-lanes.md b/docs/adr/0006-package-lanes.md index 601b8c2..cd1ee92 100644 --- a/docs/adr/0006-package-lanes.md +++ b/docs/adr/0006-package-lanes.md @@ -55,20 +55,25 @@ Nix is **baked in**, but "baked in" describes availability, not authority: ## How Nix is baked in without surrendering ownership -1. **Ship it as a Portage package, not the upstream installer.** Provide a - Phoebus overlay ebuild for Nix (e.g. `sys-apps/nix`) that installs the - toolchain, creates the store and `nixbld` build users, and installs a - `nix-daemon.service` unit. The installer adds the overlay and emerges it as - part of the base/desktop package set. -2. **Multi-user, system-managed.** Because it is a real package, a root - `nix-daemon` is acceptable: it is enabled like any other service, updated - through `emerge`, and audited with the rest of the system. -3. **Pinned and verified.** The ebuild pins the Nix version and verifies the - upstream release tarball by SHA-256. The upstream `curl | sh` installer is - never used. nixpkgs itself is pinned per environment (flake lock / channel - revision), not "latest". +A normal binary ebuild is **not possible**: the upstream Nix installer hardcodes +`/nix`, calls `sudo`, and runs `nix-store --load-db` against the real root, with +no `DESTDIR`/`--root` support. Repackaging it into a target image would require +re-implementing that bootstrap; building Nix from source is a separate, large +option. + +1. **Bootstrap the pinned, verified official release** with discrete commands in + the target chroot (no `curl | sh`, no `sh -c`), reproducing the installer's + steps: copy the store, `nix-store --load-db`, create the `nixbld` group and + users, wire the profile, and install/enable the `nix-daemon` units. +2. **Multi-user, systemd-managed.** `nix-daemon.service`/`.socket` are installed + and enabled; the store lives at `/nix`. +3. **Pinned and verified.** The Nix version and tarball SHA-256 are embedded and + checked fail-closed; nixpkgs is pinned per environment (flake lock / channel + revision), never "latest". 4. **No NixOS model.** No `configuration.nix`, no system profiles, no Nix-managed services. Nix manages user environments only. +5. **Gated until verified.** `packages.nix` defaults off until a disposable-VM + install confirms the bootstrap; Nix never becomes a hard dependency. ## Rules diff --git a/installer-rs/crates/phoebus-core/src/config.rs b/installer-rs/crates/phoebus-core/src/config.rs index 3e82e93..31bf5ec 100644 --- a/installer-rs/crates/phoebus-core/src/config.rs +++ b/installer-rs/crates/phoebus-core/src/config.rs @@ -309,6 +309,10 @@ pub struct PackagesConfig { /// Extra scoped USE entries appended to `package.use`. #[serde(default)] pub package_use: Vec, + /// Bootstrap Nix into the target (the second, built-in package lane). + /// Gated off until the bootstrap is VM-verified. + #[serde(default)] + pub nix: bool, } impl Default for PackagesConfig { @@ -319,6 +323,7 @@ impl Default for PackagesConfig { accept_license: "@FREE @BINARY-REDISTRIBUTABLE".into(), build_jobs: None, package_use: Vec::new(), + nix: false, } } } @@ -503,6 +508,7 @@ impl InstallConfig { Some(parse_num(key, value)?) } } + "NIX" | "NIX_ENABLED" => self.packages.nix = parse_bool(value), "DESKTOP_PROFILE" => self.desktop.profile = DesktopProfile::from_str(value)?, "FLATPAK" => self.desktop.flatpak = parse_bool(value), diff --git a/installer-rs/crates/phoebus-executor/src/lib.rs b/installer-rs/crates/phoebus-executor/src/lib.rs index 2e866cf..fc8b00b 100644 --- a/installer-rs/crates/phoebus-executor/src/lib.rs +++ b/installer-rs/crates/phoebus-executor/src/lib.rs @@ -16,6 +16,7 @@ pub mod error; pub mod exec; pub mod kernel; pub mod log; +pub mod nix; pub mod opencode; pub mod portage; pub mod probe; diff --git a/installer-rs/crates/phoebus-executor/src/nix.rs b/installer-rs/crates/phoebus-executor/src/nix.rs new file mode 100644 index 0000000..236c4bc --- /dev/null +++ b/installer-rs/crates/phoebus-executor/src/nix.rs @@ -0,0 +1,241 @@ +//! Nix as a built-in second package lane (bootstrap). +//! +//! The upstream Nix installer hardcodes `/nix`, calls `sudo`, and must run +//! `nix-store --load-db` against the real root, so it cannot be repackaged as a +//! normal binary ebuild. Instead we fetch the **pinned, SHA-256-verified** +//! official release and reproduce its bootstrap with discrete commands in the +//! target chroot. There is no `curl | sh` and no `sh -c`. +//! +//! Gated behind `packages.nix` because it is not VM-verified yet. + +use std::path::{Path, PathBuf}; + +use crate::command::CommandSpec; +use crate::error::{ExecError, Result}; +use crate::exec::Exec; + +pub const NIX_VERSION: &str = "2.24.9"; +pub const NIX_URL: &str = + "https://releases.nixos.org/nix/nix-2.24.9/nix-2.24.9-x86_64-linux.tar.xz"; +pub const NIX_SHA256: &str = "3c0779e4878d1289cf3fbb158ec5ea9bdf61dfb9b4efac6b3b0b6bec5ba4cf13"; +const NIX_TARBALL_DIR: &str = "nix-2.24.9-x86_64-linux"; +const NIX_STORE_NAME: &str = "2nhrwv91g6ycpyxvhmvc0xs8p92wp4bk-nix-2.24.9"; +const NIX_BUILD_USERS: u32 = 8; + +fn nix_store_bin() -> String { + format!("/nix/store/{NIX_STORE_NAME}/bin/nix-store") +} + +fn target_path(root: &Path, rel: &str) -> PathBuf { + root.join(rel) +} + +/// Whether the pinned manifest entry looks usable (not a placeholder). +pub fn manifest_is_pinned() -> bool { + NIX_URL.ends_with(".tar.xz") + && NIX_SHA256.len() == 64 + && NIX_SHA256.chars().all(|c| c.is_ascii_hexdigit()) + && NIX_SHA256.chars().any(|c| c != '0') +} + +/// Fetch, verify and bootstrap the pinned Nix release into `root`. +pub fn install(exec: &Exec, root: &Path) -> Result<()> { + exec.require_tools(&["curl", "tar", "sha256sum"])?; + if !manifest_is_pinned() { + return Err(ExecError::operation( + "nix", + "bootstrap", + "the embedded Nix manifest is not pinned", + )); + } + if exec.dry_run { + exec.logger.info(format!( + "[dry-run] would bootstrap Nix {NIX_VERSION} into the target" + )); + return Ok(()); + } + + let tarball = exec.state_dir.join("nix.tar.xz"); + exec.logger.info(format!("Nix: fetching {NIX_URL}")); + exec.run( + CommandSpec::new("nix", "download Nix", "curl") + .args(["-fL", "--retry", "3", "-o"]) + .arg(tarball.to_string_lossy().to_string()) + .arg(NIX_URL), + )?; + if !crate::opencode::checksum_ok(&tarball, NIX_SHA256)? { + return Err(ExecError::operation( + "nix", + "verify Nix", + "SHA-256 mismatch; discarding download", + )); + } + exec.logger.info("Nix: checksum verified"); + + let result = bootstrap(exec, root, &tarball); + let _ = std::fs::remove_file(&tarball); + result +} + +fn bootstrap(exec: &Exec, root: &Path, tarball: &Path) -> Result<()> { + // Stage and extract inside the target so paths are target-relative. + let tmp = target_path(root, "tmp"); + std::fs::create_dir_all(&tmp).map_err(|e| ExecError::io(tmp.clone(), e))?; + let staged = tmp.join("nix.tar.xz"); + std::fs::copy(tarball, &staged).map_err(|e| ExecError::io(staged.clone(), e))?; + exec.run( + CommandSpec::new("nix", "extract Nix", "tar") + .args(["-xJf"]) + .arg(staged.to_string_lossy().to_string()) + .args(["-C"]) + .arg(tmp.to_string_lossy().to_string()), + )?; + let extracted = tmp.join(NIX_TARBALL_DIR); + + // The Nix store. + let nix_dir = target_path(root, "nix"); + std::fs::create_dir_all(&nix_dir).map_err(|e| ExecError::io(nix_dir.clone(), e))?; + exec.run( + CommandSpec::new("nix", "install Nix store", "cp") + .args(["-a"]) + .arg(extracted.join("store").to_string_lossy().to_string()) + .arg(nix_dir.to_string_lossy().to_string()), + )?; + + // Register the store paths from the shipped database. + let reginfo = std::fs::read(extracted.join(".reginfo")) + .map_err(|e| ExecError::io(extracted.join(".reginfo"), e))?; + crate::portage::chroot_stdin(exec, root, &nix_store_bin(), &["--load-db"], ®info)?; + + // Build users (`nixbld` group + N build users). + let _ = crate::portage::chroot(exec, root, "groupadd", &["--system", "nixbld"]); + for i in 1..=NIX_BUILD_USERS { + let name = format!("nixbld{i}"); + let _ = crate::portage::chroot( + exec, + root, + "useradd", + &[ + "--system", + "--gid", + "nixbld", + "--home-dir", + "/var/empty", + "--shell", + "/sbin/nologin", + "--comment", + "Nix build user", + name.as_str(), + ], + ); + } + + // Configuration and profile wiring. + write_file( + root, + "etc/nix/nix.conf", + "# Phoebus: Nix as a second package lane.\n\ + build-users-group = nixbld\n\ + sandbox = false\n\ + experimental-features = nix-command flakes\n\ + substituters = https://cache.nixos.org\n\ + trusted-public-keys = cache.nixos.org-1:6NCHdD59X431o0gWypbMrAURkbJ16ZPMQFGspcDShjY=\n", + )?; + let profile = "nix/var/nix/profiles"; + std::fs::create_dir_all(target_path(root, profile)) + .map_err(|e| ExecError::io(target_path(root, profile), e))?; + let default_profile = target_path(root, "nix/var/nix/profiles/default"); + let store_nix = format!("/nix/store/{NIX_STORE_NAME}"); + relink(&default_profile, Path::new(&store_nix))?; + relink( + &target_path(root, "root/.nix-profile"), + Path::new("/nix/var/nix/profiles/default"), + )?; + write_file( + root, + "etc/profile.d/nix.sh", + "export PATH=/nix/var/nix/profiles/default/bin:$PATH\n", + )?; + + // The daemon units ship inside the store; symlink and enable them. + let unit = format!("{store_nix}/lib/systemd/system"); + for name in ["nix-daemon.service", "nix-daemon.socket"] { + let link = target_path(root, &format!("etc/systemd/system/{name}")); + if let Some(parent) = link.parent() { + std::fs::create_dir_all(parent).map_err(|e| ExecError::io(parent.to_path_buf(), e))?; + } + relink(&link, Path::new(&format!("{unit}/{name}")))?; + } + if let Err(e) = + crate::portage::chroot(exec, root, "systemctl", &["enable", "nix-daemon.service"]) + { + exec.logger + .warn(format!("could not enable nix-daemon.service: {e}")); + } + exec.logger + .info(format!("Nix {NIX_VERSION} bootstrapped into the target")); + + // Clean up the staging files. + let _ = std::fs::remove_file(&staged); + let _ = std::fs::remove_dir_all(&extracted); + Ok(()) +} + +fn write_file(root: &Path, rel: &str, contents: &str) -> Result<()> { + let path = target_path(root, rel); + if let Some(parent) = path.parent() { + std::fs::create_dir_all(parent).map_err(|e| ExecError::io(parent.to_path_buf(), e))?; + } + std::fs::write(&path, contents).map_err(|e| ExecError::io(path, e)) +} + +fn relink(link: &Path, target: &Path) -> Result<()> { + if let Some(parent) = link.parent() { + std::fs::create_dir_all(parent).map_err(|e| ExecError::io(parent.to_path_buf(), e))?; + } + let _ = std::fs::remove_file(link); + std::os::unix::fs::symlink(target, link).map_err(|e| ExecError::io(link.to_path_buf(), e)) +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::command::RecordingRunner; + use crate::log::Logger; + use phoebus_core::Redactor; + + fn make_exec(runner: RecordingRunner, dry_run: bool) -> Exec { + let path = tempfile::tempdir().unwrap().keep(); + let logger = Logger::open(path.join("l.log"), Redactor::new(), false); + Exec::new(Box::new(runner), logger, dry_run, path.join("root"), path) + } + + #[test] + fn manifest_is_pinned_to_a_real_release() { + assert!(manifest_is_pinned()); + assert!(NIX_URL.contains(NIX_VERSION)); + assert_eq!(NIX_SHA256.len(), 64); + } + + #[test] + fn dry_run_bootstraps_nothing() { + let runner = RecordingRunner::new(); + let exec = make_exec(runner.clone(), true); + install(&exec, Path::new(&exec.root)).unwrap(); + assert!( + runner.recorded().is_empty(), + "dry-run must not run commands" + ); + } + + #[test] + fn checksum_policy_reuses_the_shared_verifier() { + let dir = tempfile::tempdir().unwrap(); + let file = dir.path().join("nix.tar.xz"); + std::fs::write(&file, b"fixture").unwrap(); + let good = crate::opencode::sha256_hex(b"fixture"); + assert!(crate::opencode::checksum_ok(&file, &good).unwrap()); + assert!(!crate::opencode::checksum_ok(&file, &"0".repeat(64)).unwrap()); + assert!(!crate::opencode::checksum_ok(&file, "deadbeef").unwrap()); + } +} diff --git a/installer-rs/crates/phoebus-installer/src/orchestrate.rs b/installer-rs/crates/phoebus-installer/src/orchestrate.rs index d838df5..48983ec 100644 --- a/installer-rs/crates/phoebus-installer/src/orchestrate.rs +++ b/installer-rs/crates/phoebus-installer/src/orchestrate.rs @@ -11,7 +11,7 @@ use phoebus_core::validate::ValidationIssue; use phoebus_core::{validate, HardwareReport, InstallConfig, InstallPlan, SecretString}; use phoebus_executor::storage::StorageState; use phoebus_executor::{ - boot, desktop, encryption, kernel, portage, probe, services, stage3, storage, users, Exec, + boot, desktop, encryption, kernel, nix, portage, probe, services, stage3, storage, users, Exec, Result, }; @@ -410,7 +410,13 @@ impl Installer { return Ok(()); } self.exec.mount_chroot()?; - let result = services::configure(&self.exec, &self.config); + let result: Result<()> = (|| { + services::configure(&self.exec, &self.config)?; + if self.config.packages.nix { + nix::install(&self.exec, std::path::Path::new(&self.exec.root))?; + } + Ok(()) + })(); self.exec.umount_chroot()?; result }