Skip to content

Commit

Permalink
skip stdenv rebuilds
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
LnL7 committed Dec 10, 2020
1 parent 2228fa6 commit 5dc2221
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 4 deletions.
44 changes: 44 additions & 0 deletions ofborg/src/nix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@ use crate::ofborg::partition_result;

use std::collections::HashMap;
use std::env;
use std::error::Error;
use std::ffi::OsStr;
use std::fmt;
use std::fs;
use std::io;
use std::io::{BufRead, BufReader, Seek, SeekFrom};
use std::path::Path;
use std::process::{Command, Stdio};
use tracing::{debug, info};

use tempfile::tempfile;

Expand Down Expand Up @@ -137,6 +140,47 @@ impl Nix {
n
}

pub fn safely_query_cache_for_attr(
&self,
nixpkgs: &Path,
file: File,
attr: String,
) -> Result<bool, Box<dyn Error>> {
let mut command = self.safe_command::<&OsStr>(&Operation::Instantiate, nixpkgs, &[], &[]);
self.set_attrs_command(&mut command, file, vec![attr]);
let output = command
.stderr(Stdio::piped())
.stdout(Stdio::piped())
.output()?;
debug!("{}", String::from_utf8(output.stderr)?.trim());

let drv = String::from_utf8(output.stdout)?;
let output = Command::new("nix-store")
.args(&["-q", "--binding", "out"])
.arg(drv.trim())
.stderr(Stdio::piped())
.stdout(Stdio::piped())
.output()?;
debug!("{}", String::from_utf8(output.stderr)?.trim());
if !output.status.success() {
let err = io::Error::new(io::ErrorKind::Other, "Could not evaluate stdenv");
return Err(Box::new(err));
}

let out = String::from_utf8(output.stdout)?;
info!("stdenv {}", out);
let output = Command::new("nix-store")
.args(&["--option", "store", "https://cache.nixos.org"])
.args(&["-q", "--size"])
.arg(out.trim())
.stderr(Stdio::piped())
.stdout(Stdio::null())
.output()?;
debug!("{}", String::from_utf8(output.stderr)?.trim());

Ok(output.status.success())
}

pub fn safely_partition_instantiable_attrs(
&self,
nixpkgs: &Path,
Expand Down
24 changes: 20 additions & 4 deletions ofborg/src/tasks/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -325,10 +325,7 @@ impl notifyworker::SimpleNotifyWorker for BuildWorker {
return;
}

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

info!("Checking for stdenv rebuild");
match self.nix.safely_query_cache_for_attr(
refpath.as_ref(),
buildfile,
String::from("stdenv"),
) {
Ok(false) => {
info!(
"Skip build: '{}', Cannot build: '{}'",
can_build.join(", "),
cannot_build_attrs.join(", ")
);
actions.build_not_attempted(cannot_build_attrs);
return;
}
Ok(true) => (),
Err(err) => error!("Failed to detect stdenv rebuild: {:?}", err),
}

info!(
"Can build: '{}', Cannot build: '{}'",
can_build.join(", "),
Expand Down

0 comments on commit 5dc2221

Please sign in to comment.