From 831f3a98c46b0dc6493829eaafebceb2dc5fbcbb Mon Sep 17 00:00:00 2001 From: Jack O'Sullivan Date: Sun, 29 Nov 2026 18:24:27 +0000 Subject: [PATCH] progress: Report evaluating apart from building Evaluating a big flake can take minutes before Nix builds anything, which looked like a build stuck at nothing. The build is now two phases: evaluating, until Nix starts its first build, fetch or copy, and building. The builder tells the broker when evaluation ends with `Reply::Evaluated`, or after the build when Nix had nothing to do. Co-Authored-By: Claude Opus 5.5 --- docs/architecture.md | 5 +++-- docs/design.md | 2 +- nix/vm-test.nix | 1 + slop-tools.md | 4 ++-- src/builder.rs | 29 +++++++++++++++++++++++++---- src/progress.rs | 35 +++++++++++++++++++++++++++++++++-- 6 files changed, 65 insertions(+), 11 deletions(-) diff --git a/docs/architecture.md b/docs/architecture.md index 5e1ca7c..8d2d110 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -93,14 +93,15 @@ its answer. It holds nothing but a read-only Git key. ### Progress `src/progress.rs` turns Nix's `--log-format internal-json` into `Progress` messages, one `Phase` at -a time: the build, then a copy, comparison and activation for each node, or the push to the cache. +a time: the evaluation and the build, then a copy, comparison and activation for each node, or the push to the cache. - `NixLog` follows Nix's activities as Nix's own progress bar does, and returns the text Nix would have shown: errors and warnings, plus build logs when the request asks for them. - Totals of derivations and paths come from Nix's `these N derivations will be built` messages when there are some; the totals its activities expect move around while it works out what to do. - The builder sends `Reply::Progress` frames at most once a second, then `Reply::Done` with its - answer. The broker's own copies read Nix's log with `exec::run_nix`. + answer. `Reply::Evaluated` ends the evaluation, when Nix starts its first build, fetch or copy + (or, with nothing to build, when it's finished). The broker's own copies read Nix's log with `exec::run_nix`. - `Reporter` sends a phase's progress to the requester when it starts, at most once a second while it runs, and when it ends. - `runs::Runs` keeps the latest progress of each phase of requests that build, from the build on; diff --git a/docs/design.md b/docs/design.md index 236beb6..64865fe 100644 --- a/docs/design.md +++ b/docs/design.md @@ -229,7 +229,7 @@ on a desktop shows what `slop-approve` can only print. - Requests that need a passkey are marked, with a padlock on their approve buttons. - **Progress.** Builds and deploys show how they're going while they run, to approvers as well as in `slop`. - - A running request's page has a bar for the build (derivations built, paths fetched, what's + - A running request's page shows how long evaluating has taken, then has a bar for the build (derivations built, paths fetched, what's building now) and a row for each node: copying, comparing, ready, activating, done or failed, with its copy's bar and how long it's taken. - The main page lists running requests with one bar each. diff --git a/nix/vm-test.nix b/nix/vm-test.nix index 3f293fd..417afe7 100644 --- a/nix/vm-test.nix +++ b/nix/vm-test.nix @@ -320,6 +320,7 @@ pkgs.testers.runNixOSTest { assert "build line one" not in out and "warning:" not in out, out assert "\x1b" not in out, repr(out) expected = [ + "slop: evaluated in", "slop: built: 1 derivation in", "slop: copied to box: 2 paths (", "slop: copied to local in", diff --git a/slop-tools.md b/slop-tools.md index b62262c..aa08051 100644 --- a/slop-tools.md +++ b/slop-tools.md @@ -61,8 +61,8 @@ applies a policy, and asks a human to approve most requests. and their diff; that may be covered by a lease from an earlier request. Then the build and the copy to the nodes happen, with their progress shown as they go, and the deploy itself waits for approval. Keep commits focused so the diff is quick to review. -- Progress comes as a `slop:` line for each step (the build, and copying, comparing and - activating on each node) every 30 seconds or so while it runs and when it ends, such as +- Progress comes as a `slop:` line for each step (evaluating, building, and copying, comparing + and activating on each node) every 30 seconds or so while it runs and when it ends, such as `slop: building: 37/120 derivations (firefox-147.0), 4m 10s`. - Build logs aren't shown, only progress; a failed build shows the end of its log. `--logs` (on `slop deploy` and `slop build`) shows them all as they happen, which is a lot of output. diff --git a/src/builder.rs b/src/builder.rs index 3ccad57..506866c 100644 --- a/src/builder.rs +++ b/src/builder.rs @@ -147,6 +147,8 @@ pub struct BuildRequest { pub enum Reply { /// How a build is going, at most every [`progress::INTERVAL`]. Progress(Counts), + /// Evaluation is over; the progress that follows is building. + Evaluated, /// The result, encoded; the last frame. Done(#[serde(with = "serde_bytes")] Vec), } @@ -231,7 +233,7 @@ pub async fn build( out: &Output, cancel: &CancellationToken, ) -> Result { - let mut reporter = Reporter::start(out, Phase::Build).await; + let mut reporter = Reporter::start(out, Phase::Evaluate).await; let result = call::(ssh, deploy, &BuilderRequest::Build(request.clone()), out, Some(&mut reporter), cancel) .await @@ -297,6 +299,11 @@ async fn call( reporter.update(counts).await; } } + Reply::Evaluated => { + if let Some(reporter) = reporter.as_deref_mut() { + reporter.next(Phase::Build).await; + } + } Reply::Done(data) => { result = Some(data); break; @@ -743,10 +750,14 @@ async fn run( let mut data = Vec::new(); stdout.read_to_end(&mut data).await.map(|_| data) }); - follow(child.stderr.take().unwrap(), NixLog::new(request.logs), replies).await; + let realised = follow(child.stderr.take().unwrap(), NixLog::new(request.logs), replies).await; let status = child.wait().await?; let stdout = result.await??; ensure!(status.success(), "nix build failed"); + // With everything already built, Nix has nothing to start + if !realised { + let _ = replies.send(Reply::Evaluated); + } #[derive(Deserialize)] struct Built { @@ -789,26 +800,36 @@ async fn run( } /// Reads `nix`'s log until it closes: what it would show goes to stderr, and its progress to -/// `replies`, at most every [`progress::INTERVAL`] and once at the end. +/// `replies`, at most every [`progress::INTERVAL`] and once at the end, with +/// [`Reply::Evaluated`] when Nix starts building. Returns whether it did. async fn follow( stderr: impl tokio::io::AsyncRead + Unpin, mut log: NixLog, replies: &tokio::sync::mpsc::UnboundedSender, -) { +) -> bool { use tokio::io::AsyncBufReadExt; let mut lines = tokio::io::BufReader::new(stderr).lines(); let mut sent: Option = None; + let mut realising = false; while let Ok(Some(line)) = lines.next_line().await { if let Some(text) = log.line(&line) { eprintln!("{text}"); } + + // The build's progress starts as soon as Nix starts building + if log.realising() && !realising { + realising = true; + let _ = replies.send(Reply::Evaluated); + sent = None; + } if sent.is_none_or(|s| s.elapsed() >= progress::INTERVAL) { sent = Some(std::time::Instant::now()); let _ = replies.send(Reply::Progress(log.counts())); } } let _ = replies.send(Reply::Progress(log.counts())); + realising } #[cfg(test)] diff --git a/src/progress.rs b/src/progress.rs index 384eb07..bf0cc52 100644 --- a/src/progress.rs +++ b/src/progress.rs @@ -21,7 +21,9 @@ const MAX_CURRENT: usize = 3; #[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)] #[serde(rename_all = "kebab-case", deny_unknown_fields)] pub enum Phase { - /// Evaluating and building, on the builder. + /// Evaluating, on the builder, until Nix starts building and fetching. + Evaluate, + /// Building and fetching, on the builder. Build, /// Copying closures to a node. Copy(String), @@ -38,13 +40,14 @@ impl Phase { pub fn node(&self) -> Option<&str> { match self { Phase::Copy(node) | Phase::Compare(node) | Phase::Activate(node) => Some(node), - Phase::Build | Phase::Push => None, + Phase::Evaluate | Phase::Build | Phase::Push => None, } } /// What it's doing, like `copying to river`. pub fn doing(&self) -> String { match self { + Phase::Evaluate => "evaluating".into(), Phase::Build => "building".into(), Phase::Copy(node) => format!("copying to {node}"), Phase::Compare(node) => format!("comparing on {node}"), @@ -56,6 +59,7 @@ impl Phase { /// What it did, like `copied to river`. pub fn done(&self) -> String { match self { + Phase::Evaluate => "evaluated".into(), Phase::Build => "built".into(), Phase::Copy(node) => format!("copied to {node}"), Phase::Compare(node) => format!("compared on {node}"), @@ -246,6 +250,13 @@ impl Reporter { } } + /// Ends the phase and starts the next. + pub async fn next(&mut self, phase: Phase) { + self.send(State::Done).await; + (self.phase, self.started, self.counts) = (phase, Instant::now(), Counts::default()); + self.send(State::Running).await; + } + pub async fn finish(mut self, ok: bool) { self.send(if ok { State::Done } else { State::Failed }).await; } @@ -264,9 +275,11 @@ impl Reporter { // Nix's activity and result types (`libutil/logging.hh`) const COPY_PATH: u64 = 100; +const REALISE: u64 = 102; const COPY_PATHS: u64 = 103; const BUILDS: u64 = 104; const BUILD: u64 = 105; +const SUBSTITUTE: u64 = 108; const BUILD_LOG_LINE: u64 = 101; const SET_PHASE: u64 = 104; @@ -302,6 +315,8 @@ pub struct NixLog { copied: u64, /// Whether to pass build logs through, like `--print-build-logs`. logs: bool, + /// Whether Nix has started building and fetching, which it does once it's evaluated. + realising: bool, } impl NixLog { @@ -335,6 +350,7 @@ impl NixLog { let name = fields.first().and_then(Value::as_str).and_then(store_name); let text = entry.get("text").and_then(Value::as_str).filter(|t| !t.is_empty()); self.activities.insert(id, Activity { kind, name, ..Activity::default() }); + self.realising |= matches!(kind, REALISE | COPY_PATHS | BUILDS | BUILD | SUBSTITUTE); // Only builds and copies have text worth repeating in logs. (self.logs && matches!(kind, BUILD | COPY_PATH)).then(|| text.map(str::to_owned)).flatten() } @@ -394,6 +410,11 @@ impl NixLog { Count { done, total: expected.max(expected_of).max(done) } } + /// Whether evaluation is over: Nix has started building and fetching. + pub fn realising(&self) -> bool { + self.realising + } + pub fn counts(&self) -> Counts { let mut builds = self.total(BUILDS); if self.announced_builds > 0 { @@ -535,6 +556,16 @@ mod tests { assert!(counts.current.is_empty()); } + #[test] + fn notices_when_evaluation_ends() { + let mut log = NixLog::new(false); + let evaluating = r#"@nix {"action":"start","id":1,"level":4,"type":0,"text":"evaluating derivation 'x'"}"#; + log.line(evaluating); + assert!(!log.realising()); + log.line(r#"@nix {"action":"start","id":2,"level":0,"type":102,"text":""}"#); + assert!(log.realising()); + } + #[test] fn passes_build_logs_through_when_asked() { let mut log = NixLog::new(true);