Files
nix_builder/src/nix_util/high_level.rs
2026-07-28 10:19:23 -04:00

90 lines
2.0 KiB
Rust

use std::ffi::OsStr;
use std::ffi::OsString;
use std::path::Path;
use std::process::Stdio;
use tokio::process::Command;
use crate::Result;
use crate::database::db_handle::DbHandle;
use super::RunningUpdate;
use super::running_build::RunningBuild;
pub(crate) async fn nixos_build_target<B, F, A, TN>(
db_handle: &DbHandle,
build_path: B,
flake_path: F,
attr: A,
target_name: TN,
build_id: i64,
) -> Result<()>
where
B: AsRef<Path>,
F: AsRef<Path>,
A: AsRef<str>,
TN: 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
};
let mut command = Command::new("nix");
command.current_dir(build_path);
command.stdout(Stdio::piped());
command.stderr(Stdio::piped());
command.stdin(Stdio::null());
command.args([
"build",
"--show-trace",
"--max-jobs",
"1",
"--repair",
"--log-format",
"internal-json",
"-vvvvvvvvvvv",
"--keep-going",
]);
command.arg(reference);
command.kill_on_drop(true);
let child = command.spawn()?;
let mut running_build = RunningBuild::new(db_handle, build_id)?;
running_build.run_to_completion(child, target_name).await?;
Ok(())
}
pub(crate) async fn nix_flake_update<F>(flake_path: F) -> Result<()>
where
F: AsRef<Path>,
{
let mut command = Command::new("nix");
command.current_dir(flake_path);
command.stdout(Stdio::piped());
command.stderr(Stdio::piped());
command.stdin(Stdio::null());
command.args([
"flake",
"update",
"--log-format",
"internal-json",
"-vvvvvvvvvvv",
]);
command.kill_on_drop(true);
let child = command.spawn()?;
let mut running_update = RunningUpdate::new()?;
running_update.run_to_completion(child).await?;
Ok(())
}