diff --git a/src/deploy.rs b/src/deploy.rs index fa235e2..03561d3 100644 --- a/src/deploy.rs +++ b/src/deploy.rs @@ -1,10 +1,13 @@ -use color_eyre::{eyre::Result, owo_colors::OwoColorize}; +use color_eyre::{ + eyre::{Context, Result}, + owo_colors::OwoColorize, +}; use futures::future::join_all; use openssh::{KnownHosts, Session}; use std::{ env, path::PathBuf, - process::{Command, Stdio}, + process::{Command, Output, Stdio}, }; fn gen_systems_path(system: &str) -> String { @@ -14,10 +17,7 @@ fn gen_systems_path(system: &str) -> String { ) } -pub fn build(systems: &[String], show_trace: bool) -> Result<()> { - if systems.is_empty() { - return Ok(()); - } +pub fn build(systems: &[String], show_trace: bool) -> Result { let dir = env::var("PRJ_ROOT") .map(PathBuf::from) .or_else(|_| env::current_dir())?; @@ -50,8 +50,7 @@ pub fn build(systems: &[String], show_trace: bool) -> Result<()> { .arg("--print-out-paths") .arg("--no-link") .args(configs); - cmd.spawn()?.wait()?; - Ok(()) + cmd.output().wrap_err("Error building systems") } pub async fn deploy(systems: &[String], show_trace: bool, mode: &str) -> Result<()> { @@ -68,22 +67,24 @@ pub async fn deploy(systems: &[String], show_trace: bool, mode: &str) -> Result< .await .into_iter() .collect::, openssh::Error>>()?; - build( - &systems.iter().map(|x| x.to_string()).collect::>(), - show_trace, + let toplevels = String::from_utf8( + build( + &systems.iter().map(|x| x.to_string()).collect::>(), + show_trace, + )? + .stdout, )?; - for (system, (con, host)) in systems.into_iter().zip(connections.into_iter().zip(hosts)) { - let path = gen_systems_path(system); - let mut cmd = Command::new("nix"); - let cmd = cmd.arg("eval").arg("--read-only").arg("--raw").arg(path); - let toplevel = String::from_utf8(cmd.output()?.stdout)?; + for (toplevel, (system, (con, host))) in toplevels + .lines() + .zip(systems.into_iter().zip(connections.into_iter().zip(hosts))) + { println!("Copyings toplevel for {}", system.blue()); let mut cmd = Command::new("nix"); let cmd = cmd .arg("copy") .arg("--to") .arg(format!("ssh://{}", host)) - .arg(&toplevel); + .arg(toplevel); cmd.spawn()?.wait()?; let prev_system = con .command("readlink") @@ -97,7 +98,7 @@ pub async fn deploy(systems: &[String], show_trace: bool, mode: &str) -> Result< .arg("--profile") .arg("/nix/var/nix/profiles/system") .arg("--set") - .arg(&toplevel) + .arg(toplevel) .spawn() .await?; con.command(format!("{}/bin/switch-to-configuration", toplevel))