diff --git a/ofborg/src/evalchecker.rs b/ofborg/src/evalchecker.rs index f485d77..096d188 100644 --- a/ofborg/src/evalchecker.rs +++ b/ofborg/src/evalchecker.rs @@ -7,16 +7,16 @@ use ofborg::nix; pub struct EvalChecker { name: String, - cmd: String, + op: nix::Operation, args: Vec, nix: nix::Nix, } impl EvalChecker { - pub fn new(name: &str, cmd: &str, args: Vec, nix: nix::Nix) -> EvalChecker { + pub fn new(name: &str, op: nix::Operation, args: Vec, nix: nix::Nix) -> EvalChecker { EvalChecker { name: name.to_owned(), - cmd: cmd.to_owned(), + op: op, args: args, nix: nix, } @@ -27,11 +27,11 @@ impl EvalChecker { } pub fn execute(&self, path: &Path) -> Result { - self.nix.safely(&self.cmd, path, self.args.clone(), false) + self.nix.safely(self.op.clone(), path, self.args.clone(), false) } pub fn cli_cmd(&self) -> String { - let mut cli = vec![self.cmd.clone()]; + let mut cli = vec![self.op.to_string()]; cli.append(&mut self.args.clone()); return cli.join(" "); } diff --git a/ofborg/src/nix.rs b/ofborg/src/nix.rs index 6183c47..aaeac78 100644 --- a/ofborg/src/nix.rs +++ b/ofborg/src/nix.rs @@ -1,5 +1,5 @@ use std::env; -use std::ffi::OsString; +use std::fmt; use std::fs::File; use std::io::Seek; use std::io::SeekFrom; @@ -7,6 +7,37 @@ use std::path::Path; use std::process::{Command, Stdio}; use tempfile::tempfile; +#[derive(Clone, Debug)] +pub enum Operation { + Build, + Instantiate, + Unknown { program: String }, +} + +impl Operation { + pub fn new(program: &str) -> Operation { + Operation::Unknown { program: program.to_owned() } + } + + fn command(&self) -> Command { + match *self { + Operation::Build => Command::new("nix-build"), + Operation::Instantiate => Command::new("nix-instantiate"), + Operation::Unknown { ref program } => Command::new(program), + } + } +} + +impl fmt::Display for Operation { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + match *self { + Operation::Build => write!(f, "{}", "nix-build"), + Operation::Instantiate => write!(f, "{}", "nix-instantiate"), + Operation::Unknown { ref program } => write!(f, "{}", program), + } + } +} + #[derive(Clone, Debug, PartialEq)] pub struct Nix { system: String, @@ -71,17 +102,17 @@ impl Nix { attrargs.push(attr); } - return self.safe_command("nix-build", nixpkgs, attrargs); + return self.safe_command(Operation::Build, nixpkgs, attrargs); } pub fn safely( &self, - cmd: &str, + op: Operation, nixpkgs: &Path, args: Vec, keep_stdout: bool, ) -> Result { - return self.run(self.safe_command(cmd, nixpkgs, args), keep_stdout); + return self.run(self.safe_command(op, nixpkgs, args), keep_stdout); } pub fn run(&self, mut cmd: Command, keep_stdout: bool) -> Result { @@ -113,12 +144,10 @@ impl Nix { } } - pub fn safe_command(&self, cmd: &str, nixpkgs: &Path, args: Vec) -> Command { - let mut nixpath = OsString::new(); - nixpath.push("nixpkgs="); - nixpath.push(nixpkgs.as_os_str()); + pub fn safe_command(&self, op: Operation, nixpkgs: &Path, args: Vec) -> Command { + let nixpath = format!("nixpkgs={}", nixpkgs.display()); - let mut command = Command::new(cmd); + let mut command = op.command(); command.env_clear(); command.current_dir(nixpkgs); command.env("HOME", "/homeless-shelter"); @@ -276,7 +305,7 @@ mod tests { let ret: Result = nix.run( - nix.safe_command("./environment.sh", build_path().as_path(), vec![]), + nix.safe_command(Operation::new("./environment.sh"), build_path().as_path(), vec![]), true, ); @@ -298,7 +327,7 @@ mod tests { let ret: Result = nix.run( - nix.safe_command("./environment.sh", build_path().as_path(), vec![]), + nix.safe_command(Operation::new("./environment.sh"), build_path().as_path(), vec![]), true, ); @@ -320,7 +349,7 @@ mod tests { let nix = nix(); let ret: Result = nix.run( - nix.safe_command("echo", build_path().as_path(), vec![]), + nix.safe_command(Operation::new("echo"), build_path().as_path(), vec![]), true, ); @@ -393,7 +422,7 @@ mod tests { #[test] fn instantiation() { let ret: Result = nix().safely( - "nix-instantiate", + Operation::Instantiate, passing_eval_path().as_path(), vec![], true, diff --git a/ofborg/src/outpathdiff.rs b/ofborg/src/outpathdiff.rs index f3e56c7..27c2e3b 100644 --- a/ofborg/src/outpathdiff.rs +++ b/ofborg/src/outpathdiff.rs @@ -170,7 +170,7 @@ impl OutPaths { } self.nix.safely( - "nix-env", + nix::Operation::new("nix-env"), &self.path, vec![ String::from("-f"), diff --git a/ofborg/src/tasks/massrebuilder.rs b/ofborg/src/tasks/massrebuilder.rs index 389f920..8d29875 100644 --- a/ofborg/src/tasks/massrebuilder.rs +++ b/ofborg/src/tasks/massrebuilder.rs @@ -10,7 +10,7 @@ use std::path::Path; use std::path::PathBuf; use ofborg::checkout; use ofborg::message::{massrebuildjob, buildjob}; -use ofborg::nix::Nix; +use ofborg::nix; use ofborg::acl::ACL; use ofborg::stats; @@ -25,7 +25,7 @@ use hubcaps; pub struct MassRebuildWorker { cloner: checkout::CachedCloner, - nix: Nix, + nix: nix::Nix, github: hubcaps::Github, acl: ACL, identity: String, @@ -36,7 +36,7 @@ pub struct MassRebuildWorker { impl MassRebuildWorker { pub fn new( cloner: checkout::CachedCloner, - nix: Nix, + nix: nix::Nix, github: hubcaps::Github, acl: ACL, identity: String, @@ -291,7 +291,7 @@ impl worker::SimpleWorker for MassRebuildWorker { let eval_checks = vec![ EvalChecker::new( "package-list", - "nix-env", + nix::Operation::new("nix-env"), vec![ String::from("--file"), String::from("."), @@ -304,7 +304,7 @@ impl worker::SimpleWorker for MassRebuildWorker { EvalChecker::new( "nixos-options", - "nix-instantiate", + nix::Operation::Instantiate, vec![ String::from("./nixos/release.nix"), String::from("-A"), @@ -315,7 +315,7 @@ impl worker::SimpleWorker for MassRebuildWorker { EvalChecker::new( "nixos-manual", - "nix-instantiate", + nix::Operation::Instantiate, vec![ String::from("./nixos/release.nix"), String::from("-A"), @@ -326,7 +326,7 @@ impl worker::SimpleWorker for MassRebuildWorker { EvalChecker::new( "nixpkgs-manual", - "nix-instantiate", + nix::Operation::Instantiate, vec![ String::from("./pkgs/top-level/release.nix"), String::from("-A"), @@ -337,7 +337,7 @@ impl worker::SimpleWorker for MassRebuildWorker { EvalChecker::new( "nixpkgs-tarball", - "nix-instantiate", + nix::Operation::Instantiate, vec![ String::from("./pkgs/top-level/release.nix"), String::from("-A"), @@ -348,7 +348,7 @@ impl worker::SimpleWorker for MassRebuildWorker { EvalChecker::new( "nixpkgs-unstable-jobset", - "nix-instantiate", + nix::Operation::Instantiate, vec![ String::from("./pkgs/top-level/release.nix"), String::from("-A"), @@ -542,7 +542,7 @@ pub enum System { #[derive(Debug, PartialEq)] struct Stdenvs { - nix: Nix, + nix: nix::Nix, co: PathBuf, linux_stdenv_before: Option, @@ -553,7 +553,7 @@ struct Stdenvs { } impl Stdenvs { - fn new(nix: Nix, co: PathBuf) -> Stdenvs { + fn new(nix: nix::Nix, co: PathBuf) -> Stdenvs { return Stdenvs { nix: nix, co: co, @@ -615,7 +615,7 @@ impl Stdenvs { fn evalstdenv(&self, system: &str) -> Option { let result = self.nix.with_system(system.to_owned()).safely( - "nix-instantiate", + nix::Operation::Instantiate, &self.co, vec![ String::from("."), @@ -768,8 +768,7 @@ mod tests { let nixpkgs = String::from_utf8(output.stdout) .expect("nixpkgs required"); - let nix = Nix::new(String::from("x86_64-linux"), String::from("daemon"), 1200, None); - + let nix = nix::Nix::new(String::from("x86_64-linux"), String::from("daemon"), 1200, None); let mut stdenv = Stdenvs::new( nix.clone(),