diff --git a/ofborg/src/nix.rs b/ofborg/src/nix.rs index d0339ce..a231e0a 100644 --- a/ofborg/src/nix.rs +++ b/ofborg/src/nix.rs @@ -6,6 +6,7 @@ use std::fs::File; use std::io::Seek; use std::io::SeekFrom; +#[derive(Clone, Debug, PartialEq)] pub struct Nix { system: String, remote: String @@ -19,35 +20,46 @@ pub fn new(system: String, remote: String) -> Nix { } impl Nix { - pub fn safely_build_attrs(&self, nixpkgs: &Path, attrs: Vec) -> Result { - let mut nixpath = OsString::new(); - nixpath.push("nixpkgs="); - nixpath.push(nixpkgs.as_os_str()); + pub fn with_system(&self, system: String) -> Nix { + return Nix{ + system: system, + remote: self.remote.clone(), + }; + } - let mut attrargs: Vec = Vec::with_capacity(attrs.len() * 2); + pub fn safely_build_attrs(&self, nixpkgs: &Path, attrs: Vec) -> Result { + let mut attrargs: Vec = Vec::with_capacity(2 + (attrs.len() * 2)); + attrargs.push(String::from("--no-out-link")); + attrargs.push(String::from("--keep-going")); for attr in attrs { attrargs.push(String::from("-A")); attrargs.push(attr); } + return self.safely("nix-build", nixpkgs, attrargs); + } + + pub fn safely(&self, cmd: &str, nixpkgs: &Path, args: Vec) -> Result { + let mut nixpath = OsString::new(); + nixpath.push("nixpkgs="); + nixpath.push(nixpkgs.as_os_str()); + let stdout = tempfile().expect("Fetching a stdout tempfile"); let stderr = stdout.try_clone().expect("Cloning stdout for stderr"); let mut reader = stderr.try_clone().expect("Cloning stderr to the reader"); - let status = Command::new("nix-build") + let status = Command::new(cmd) .env_clear() .current_dir(nixpkgs) .stdout(Stdio::from(stdout)) .stderr(Stdio::from(stderr)) .env("NIX_PATH", nixpath) .env("NIX_REMOTE", &self.remote) - .arg("--no-out-link") .args(&["--option", "restrict-eval", "true"]) .args(&["--argstr", "system", &self.system]) - .arg("--keep-going") - .args(attrargs) + .args(args) .status() - .expect("Running nix-build"); + .expect(format!("Running {:?}", cmd).as_ref()); reader.seek(SeekFrom::Start(0)).expect("Seeking to Start(0)");