use std::process::{Command, ExitStatus};
use blue_lang_pkg::lock::Prefetch;
pub struct NixPrefetch;
#[derive(Debug, thiserror::Error)]
pub enum NixPrefetchError {
#[error("could not start `nix`: {0}")]
Spawn(#[source] std::io::Error),
#[error("`nix flake prefetch --json` exited with {status}: {stderr}")]
Exit { status: ExitStatus, stderr: String },
#[error("`nix flake prefetch --json` printed output that is not UTF-8")]
NotUtf8(#[source] std::string::FromUtf8Error),
}
impl Prefetch for NixPrefetch {
fn prefetch(&self, url: &str) -> Result<String, Box<dyn std::error::Error + Send + Sync>> {
let out = Command::new("nix")
.args([
"--extra-experimental-features",
"nix-command flakes",
"flake",
"prefetch",
"--json",
url,
])
.output()
.map_err(NixPrefetchError::Spawn)?;
if !out.status.success() {
return Err(NixPrefetchError::Exit {
status: out.status,
stderr: String::from_utf8_lossy(&out.stderr).trim().to_string(),
}
.into());
}
Ok(String::from_utf8(out.stdout).map_err(NixPrefetchError::NotUtf8)?)
}
}