Skip to content

Commit 5dc2221

Browse files
committed
skip stdenv rebuilds
Before attempting builds check if the stdenv for this branch is available in the cache. If this is not the case then either the merge request is a mass rebuild or it's targeting a branch like staging.
1 parent 2228fa6 commit 5dc2221

File tree

2 files changed

+64
-4
lines changed

2 files changed

+64
-4
lines changed

ofborg/src/nix.rs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,15 @@ use crate::ofborg::partition_result;
44

55
use std::collections::HashMap;
66
use std::env;
7+
use std::error::Error;
78
use std::ffi::OsStr;
89
use std::fmt;
910
use std::fs;
11+
use std::io;
1012
use std::io::{BufRead, BufReader, Seek, SeekFrom};
1113
use std::path::Path;
1214
use std::process::{Command, Stdio};
15+
use tracing::{debug, info};
1316

1417
use tempfile::tempfile;
1518

@@ -137,6 +140,47 @@ impl Nix {
137140
n
138141
}
139142

143+
pub fn safely_query_cache_for_attr(
144+
&self,
145+
nixpkgs: &Path,
146+
file: File,
147+
attr: String,
148+
) -> Result<bool, Box<dyn Error>> {
149+
let mut command = self.safe_command::<&OsStr>(&Operation::Instantiate, nixpkgs, &[], &[]);
150+
self.set_attrs_command(&mut command, file, vec![attr]);
151+
let output = command
152+
.stderr(Stdio::piped())
153+
.stdout(Stdio::piped())
154+
.output()?;
155+
debug!("{}", String::from_utf8(output.stderr)?.trim());
156+
157+
let drv = String::from_utf8(output.stdout)?;
158+
let output = Command::new("nix-store")
159+
.args(&["-q", "--binding", "out"])
160+
.arg(drv.trim())
161+
.stderr(Stdio::piped())
162+
.stdout(Stdio::piped())
163+
.output()?;
164+
debug!("{}", String::from_utf8(output.stderr)?.trim());
165+
if !output.status.success() {
166+
let err = io::Error::new(io::ErrorKind::Other, "Could not evaluate stdenv");
167+
return Err(Box::new(err));
168+
}
169+
170+
let out = String::from_utf8(output.stdout)?;
171+
info!("stdenv {}", out);
172+
let output = Command::new("nix-store")
173+
.args(&["--option", "store", "https://cache.nixos.org"])
174+
.args(&["-q", "--size"])
175+
.arg(out.trim())
176+
.stderr(Stdio::piped())
177+
.stdout(Stdio::null())
178+
.output()?;
179+
debug!("{}", String::from_utf8(output.stderr)?.trim());
180+
181+
Ok(output.status.success())
182+
}
183+
140184
pub fn safely_partition_instantiable_attrs(
141185
&self,
142186
nixpkgs: &Path,

ofborg/src/tasks/build.rs

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -325,10 +325,7 @@ impl notifyworker::SimpleNotifyWorker for BuildWorker {
325325
return;
326326
}
327327

328-
info!(
329-
"Got path: {:?}, determining which ones we can build ",
330-
refpath
331-
);
328+
info!("Determining which attributes we can build");
332329
let (can_build, cannot_build) = self.nix.safely_partition_instantiable_attrs(
333330
refpath.as_ref(),
334331
buildfile,
@@ -341,6 +338,25 @@ impl notifyworker::SimpleNotifyWorker for BuildWorker {
341338
.map(|(attr, _)| attr)
342339
.collect();
343340

341+
info!("Checking for stdenv rebuild");
342+
match self.nix.safely_query_cache_for_attr(
343+
refpath.as_ref(),
344+
buildfile,
345+
String::from("stdenv"),
346+
) {
347+
Ok(false) => {
348+
info!(
349+
"Skip build: '{}', Cannot build: '{}'",
350+
can_build.join(", "),
351+
cannot_build_attrs.join(", ")
352+
);
353+
actions.build_not_attempted(cannot_build_attrs);
354+
return;
355+
}
356+
Ok(true) => (),
357+
Err(err) => error!("Failed to detect stdenv rebuild: {:?}", err),
358+
}
359+
344360
info!(
345361
"Can build: '{}', Cannot build: '{}'",
346362
can_build.join(", "),

0 commit comments

Comments
 (0)