add operation type for nix commands
This commit is contained in:
parent
48430ba3be
commit
ca7f94b36b
|
@ -7,16 +7,16 @@ use ofborg::nix;
|
|||
|
||||
pub struct EvalChecker {
|
||||
name: String,
|
||||
cmd: String,
|
||||
op: nix::Operation,
|
||||
args: Vec<String>,
|
||||
nix: nix::Nix,
|
||||
}
|
||||
|
||||
impl EvalChecker {
|
||||
pub fn new(name: &str, cmd: &str, args: Vec<String>, nix: nix::Nix) -> EvalChecker {
|
||||
pub fn new(name: &str, op: nix::Operation, args: Vec<String>, 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<File, File> {
|
||||
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(" ");
|
||||
}
|
||||
|
|
|
@ -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<String>,
|
||||
keep_stdout: bool,
|
||||
) -> Result<File, File> {
|
||||
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<File, File> {
|
||||
|
@ -113,12 +144,10 @@ impl Nix {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn safe_command(&self, cmd: &str, nixpkgs: &Path, args: Vec<String>) -> 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<String>) -> 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<File, File> =
|
||||
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<File, File> =
|
||||
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<File, File> = 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<File, File> = nix().safely(
|
||||
"nix-instantiate",
|
||||
Operation::Instantiate,
|
||||
passing_eval_path().as_path(),
|
||||
vec![],
|
||||
true,
|
||||
|
|
|
@ -170,7 +170,7 @@ impl OutPaths {
|
|||
}
|
||||
|
||||
self.nix.safely(
|
||||
"nix-env",
|
||||
nix::Operation::new("nix-env"),
|
||||
&self.path,
|
||||
vec![
|
||||
String::from("-f"),
|
||||
|
|
|
@ -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<E> {
|
||||
cloner: checkout::CachedCloner,
|
||||
nix: Nix,
|
||||
nix: nix::Nix,
|
||||
github: hubcaps::Github,
|
||||
acl: ACL,
|
||||
identity: String,
|
||||
|
@ -36,7 +36,7 @@ pub struct MassRebuildWorker<E> {
|
|||
impl<E: stats::SysEvents> MassRebuildWorker<E> {
|
||||
pub fn new(
|
||||
cloner: checkout::CachedCloner,
|
||||
nix: Nix,
|
||||
nix: nix::Nix,
|
||||
github: hubcaps::Github,
|
||||
acl: ACL,
|
||||
identity: String,
|
||||
|
@ -291,7 +291,7 @@ impl<E: stats::SysEvents> worker::SimpleWorker for MassRebuildWorker<E> {
|
|||
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<E: stats::SysEvents> worker::SimpleWorker for MassRebuildWorker<E> {
|
|||
|
||||
EvalChecker::new(
|
||||
"nixos-options",
|
||||
"nix-instantiate",
|
||||
nix::Operation::Instantiate,
|
||||
vec![
|
||||
String::from("./nixos/release.nix"),
|
||||
String::from("-A"),
|
||||
|
@ -315,7 +315,7 @@ impl<E: stats::SysEvents> worker::SimpleWorker for MassRebuildWorker<E> {
|
|||
|
||||
EvalChecker::new(
|
||||
"nixos-manual",
|
||||
"nix-instantiate",
|
||||
nix::Operation::Instantiate,
|
||||
vec![
|
||||
String::from("./nixos/release.nix"),
|
||||
String::from("-A"),
|
||||
|
@ -326,7 +326,7 @@ impl<E: stats::SysEvents> worker::SimpleWorker for MassRebuildWorker<E> {
|
|||
|
||||
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<E: stats::SysEvents> worker::SimpleWorker for MassRebuildWorker<E> {
|
|||
|
||||
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<E: stats::SysEvents> worker::SimpleWorker for MassRebuildWorker<E> {
|
|||
|
||||
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<String>,
|
||||
|
@ -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<String> {
|
||||
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(),
|
||||
|
|
Loading…
Reference in a new issue