Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: --print-all-dependencies should handle unknown-deriver #70

Merged
merged 12 commits into from
Jul 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ crate-type = ["cdylib", "rlib"]

[dependencies]
anyhow = "1.0"
thiserror = "1.0"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
tokio = { version = "1", features = ["full"] }
Expand Down
69 changes: 55 additions & 14 deletions src/nix/nix_store.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
/// Run `nix-store` in Rust
///
/// TODO: Upstream this to nix-rs
use std::{fmt, path::PathBuf};

use anyhow::{bail, Result};
use anyhow::Result;
use nix_rs::command::{CommandError, NixCmdError};
use serde::{Deserialize, Serialize};
use thiserror::Error;
use tokio::process::Command;

/// Nix derivation output path
Expand Down Expand Up @@ -66,7 +71,10 @@ impl NixStoreCmd {
/// This is done by querying the deriver of each output path from [devour_flake::DrvOut] using [nix_store_query_deriver] and
/// then querying all dependencies of each deriver using [nix_store_query_requisites_with_outputs].
/// Finally, all dependencies of each deriver are collected and returned as [Vec<StorePath>].
pub async fn fetch_all_deps(&self, out_paths: Vec<DrvOut>) -> Result<Vec<StorePath>> {
pub async fn fetch_all_deps(
&self,
out_paths: Vec<DrvOut>,
) -> Result<Vec<StorePath>, NixStoreCmdError> {
let mut all_drvs = Vec::new();
for out in out_paths.iter() {
let DrvOut(out_path) = out;
Expand All @@ -84,20 +92,23 @@ impl NixStoreCmd {
}

/// Return the derivation used to build the given build output.
async fn nix_store_query_deriver(&self, out_path: PathBuf) -> Result<DrvOut> {
async fn nix_store_query_deriver(&self, out_path: PathBuf) -> Result<DrvOut, NixStoreCmdError> {
let mut cmd = self.command();
cmd.args(["--query", "--deriver", &out_path.to_string_lossy().as_ref()]);
nix_rs::command::trace_cmd(&cmd);
let out = cmd.output().await?;
if out.status.success() {
let drv_path = String::from_utf8(out.stdout)?.trim().to_string();
if drv_path == "unknown-deriver" {
return Err(NixStoreCmdError::UnknownDeriver);
}
Ok(DrvOut(PathBuf::from(drv_path)))
} else {
let exit_code = out.status.code().unwrap_or(1);
bail!(
"nix-store --query --deriver failed to run (exited: {})",
exit_code
);
// TODO(refactor): When upstreaming this module to nix-rs, create a
// nicer and unified way to create `ProcessFailed`
let stderr = Some(String::from_utf8_lossy(&out.stderr).to_string());
let exit_code = out.status.code();
Err(CommandError::ProcessFailed { stderr, exit_code }.into())
}
}

Expand All @@ -106,7 +117,7 @@ impl NixStoreCmd {
async fn nix_store_query_requisites_with_outputs(
&self,
drv_path: DrvOut,
) -> Result<Vec<StorePath>> {
) -> Result<Vec<StorePath>, NixStoreCmdError> {
let mut cmd = self.command();
cmd.args([
"--query",
Expand All @@ -127,11 +138,41 @@ impl NixStoreCmd {
.collect();
Ok(out)
} else {
let exit_code = out.status.code().unwrap_or(1);
bail!(
"nix-store --query --requisites --include-outputs failed to run (exited: {})",
exit_code
);
// TODO(refactor): see above
let stderr = Some(String::from_utf8_lossy(&out.stderr).to_string());
let exit_code = out.status.code();
Err(CommandError::ProcessFailed { stderr, exit_code }.into())
}
}
}

/// `nix-store` command errors
#[derive(Error, Debug)]
pub enum NixStoreCmdError {
#[error(transparent)]
NixCmdError(#[from] NixCmdError),

#[error("Unknown deriver")]
UnknownDeriver,
}

impl From<std::io::Error> for NixStoreCmdError {
fn from(err: std::io::Error) -> Self {
let cmd_error: CommandError = err.into();
cmd_error.into()
}
}

impl From<std::string::FromUtf8Error> for NixStoreCmdError {
fn from(err: std::string::FromUtf8Error) -> Self {
let cmd_error: CommandError = err.into();
cmd_error.into()
}
}

impl From<CommandError> for NixStoreCmdError {
fn from(err: CommandError) -> Self {
let nixcmd_error: NixCmdError = err.into();
nixcmd_error.into()
}
}
Loading