Files
nix_builder/src/nix_util/high_level.rs

166 lines
4.7 KiB
Rust
Raw Normal View History

2026-02-14 19:20:52 -05:00
use std::ffi::OsStr;
use std::ffi::OsString;
use std::path::Path;
use std::process::Stdio;
2026-02-14 19:20:52 -05:00
use tokio::io::AsyncBufReadExt;
use tokio::io::BufReader;
use tokio::io::Lines;
use tokio::process::Child;
use tokio::process::ChildStderr;
use tokio::process::ChildStdout;
2026-02-14 19:20:52 -05:00
use tokio::process::Command;
use crate::Result;
use crate::database::db_handle::DbHandle;
2026-02-14 19:20:52 -05:00
pub(crate) async fn nixos_build_target<B, F, A>(
db_handle: &DbHandle,
2026-02-14 19:20:52 -05:00
build_path: B,
flake_path: F,
attr: A,
) -> Result<()>
2026-02-14 19:20:52 -05:00
where
B: AsRef<Path>,
F: AsRef<Path>,
A: AsRef<str>,
{
let reference = {
let path = AsRef::<OsStr>::as_ref(flake_path.as_ref());
let attr = attr.as_ref();
let mut reference = OsString::with_capacity(path.len() + attr.len() + 1);
reference.push(path);
reference.push("#");
reference.push(attr);
reference
};
// nixos-rebuild build --show-trace --sudo --max-jobs "$JOBS" --flake "$DIR/../../#odo" --log-format internal-json -v "${@}"
let mut command = Command::new("nixos-rebuild");
command.current_dir(build_path);
command.stdout(Stdio::piped());
command.stderr(Stdio::piped());
command.stdin(Stdio::null());
2026-02-14 22:15:09 -05:00
command.args([
2026-02-14 19:20:52 -05:00
"build",
"--show-trace",
"--sudo",
"--max-jobs",
"1",
"--log-format",
"internal-json",
"-v",
"--keep-going",
2026-02-14 19:20:52 -05:00
]);
command.arg("--flake");
command.arg(reference);
command.kill_on_drop(true);
let mut child = command.spawn()?;
let mut output_stream = OutputSream::from_child(&mut child)?;
let exit_status_handle = tokio::spawn(async move {
let status = child
.wait()
.await
.expect("nixos-rebuild encountered an error");
status
});
loop {
let next_line = output_stream.next_line().await?;
match next_line {
OutputLine::Stdout(line) => {
println!("STDOUT: {line}");
}
OutputLine::Stderr(line) => {
println!("STDERR: {line}");
}
OutputLine::Done => break,
};
2026-02-14 19:20:52 -05:00
}
let exit_status = exit_status_handle.await?;
println!("nixos-rebuild status was: {}", exit_status);
2026-02-14 19:20:52 -05:00
Ok(())
}
struct OutputSream {
stdout: Option<Lines<BufReader<ChildStdout>>>,
stderr: Option<Lines<BufReader<ChildStderr>>>,
}
impl OutputSream {
pub(crate) fn from_child(child: &mut Child) -> Result<Self> {
let stdout = child
.stdout
.take()
.expect("child did not have a handle to stdout");
let stderr = child
.stderr
.take()
.expect("child did not have a handle to stderr");
let stdout = BufReader::new(stdout).lines();
let stderr = BufReader::new(stderr).lines();
Ok(OutputSream {
stdout: Some(stdout),
stderr: Some(stderr),
})
}
pub(crate) async fn next_line(&mut self) -> Result<OutputLine> {
loop {
match (&mut self.stdout, &mut self.stderr) {
(None, None) => {
return Ok(OutputLine::Done);
}
(None, Some(err)) => {
if let Some(line) = err.next_line().await? {
return Ok(OutputLine::Stderr(line));
} else {
return Ok(OutputLine::Done);
}
}
(Some(out), None) => {
if let Some(line) = out.next_line().await? {
return Ok(OutputLine::Stdout(line));
} else {
return Ok(OutputLine::Done);
}
}
(Some(out), Some(err)) => {
tokio::select! {
Ok(line) = out.next_line() => match line {
Some(line) => {
return Ok(OutputLine::Stdout(line));
},
None => {
self.stdout.take();
},
},
Ok(line) = err.next_line() => match line {
Some(line) => {
return Ok(OutputLine::Stderr(line));
},
None => {
self.stderr.take();
},
},
else => {
return Ok(OutputLine::Done);
},
};
}
};
}
}
}
enum OutputLine {
Stdout(String),
Stderr(String),
Done,
}