From 1555b7200fd1c78a0545de1880411afb8cc3fae2 Mon Sep 17 00:00:00 2001 From: Shivaraj B H Date: Tue, 8 Oct 2024 01:54:03 +0530 Subject: [PATCH] nix_rs(`nix_eval`): Don't capture stderr (#298) Resolves #209 PRs filed tangential to this: - https://github.com/NixOS/nix/pull/11652 --------- Co-authored-by: Sridhar Ratnakumar --- crates/nix_rs/CHANGELOG.md | 4 ++++ crates/nix_rs/src/command.rs | 12 +++++++----- crates/nix_rs/src/copy.rs | 3 ++- crates/nix_rs/src/flake/command.rs | 6 ++++-- crates/nix_rs/src/flake/eval.rs | 7 ++++++- 5 files changed, 23 insertions(+), 9 deletions(-) diff --git a/crates/nix_rs/CHANGELOG.md b/crates/nix_rs/CHANGELOG.md index 114dac0a..741f8e3f 100644 --- a/crates/nix_rs/CHANGELOG.md +++ b/crates/nix_rs/CHANGELOG.md @@ -1,6 +1,10 @@ # Changelog ## Unreleased + +- **`eval::nix_eval`** + - Display evaluation progress + - Decrease logging verbosity - **`flake::schema`** - Don't hardcode flake schema types - **`config`** diff --git a/crates/nix_rs/src/command.rs b/crates/nix_rs/src/command.rs index 8d49ec8a..742ce84f 100644 --- a/crates/nix_rs/src/command.rs +++ b/crates/nix_rs/src/command.rs @@ -175,20 +175,22 @@ impl NixCmd { } /// Run Nix with given [Command] customizations, while also tracing the command being run. - pub async fn run_with(&self, f: F) -> Result<(), CommandError> + /// + /// Return the stdout bytes returned by [tokio::process::Child::wait_with_output]. In order to capture stdout, you must call `cmd.stdout(Stdio::piped());` inside the handler. + pub async fn run_with(&self, f: F) -> Result, CommandError> where F: FnOnce(&mut Command), { let mut cmd = self.command(); f(&mut cmd); trace_cmd(&cmd); - let status = cmd.spawn()?.wait().await?; - if status.success() { - Ok(()) + let out = cmd.spawn()?.wait_with_output().await?; + if out.status.success() { + Ok(out.stdout) } else { Err(CommandError::ProcessFailed { stderr: None, - exit_code: status.code(), + exit_code: out.status.code(), }) } } diff --git a/crates/nix_rs/src/copy.rs b/crates/nix_rs/src/copy.rs index 6ec26a17..3ba5e05a 100644 --- a/crates/nix_rs/src/copy.rs +++ b/crates/nix_rs/src/copy.rs @@ -28,5 +28,6 @@ pub async fn nix_copy( cmd.run_with(|cmd| { cmd.args(args); }) - .await + .await?; + Ok(()) } diff --git a/crates/nix_rs/src/flake/command.rs b/crates/nix_rs/src/flake/command.rs index 327e0a86..5c4957fd 100644 --- a/crates/nix_rs/src/flake/command.rs +++ b/crates/nix_rs/src/flake/command.rs @@ -26,7 +26,8 @@ pub async fn run( cmd.args([url.to_string(), "--".to_string()]); cmd.args(args); }) - .await + .await?; + Ok(()) } /// Run `nix develop` on the given flake devshell. @@ -42,7 +43,8 @@ pub async fn develop( cmd.args(["develop".to_string(), url.to_string(), "-c".to_string()]); cmd.args(command); }) - .await + .await?; + Ok(()) } /// Run `nix build` diff --git a/crates/nix_rs/src/flake/eval.rs b/crates/nix_rs/src/flake/eval.rs index d0d3c699..49e61044 100644 --- a/crates/nix_rs/src/flake/eval.rs +++ b/crates/nix_rs/src/flake/eval.rs @@ -1,4 +1,6 @@ //! Work with `nix eval` +use std::process::Stdio; + use crate::command::{CommandError, NixCmd, NixCmdError}; use super::{command::FlakeOptions, url::FlakeUrl}; @@ -13,10 +15,13 @@ where T: serde::de::DeserializeOwned, { let stdout = nixcmd - .run_with_returning_stdout(|cmd| { + .run_with(|cmd| { + cmd.stdout(Stdio::piped()); cmd.args(["eval", "--json"]); opts.use_in_command(cmd); cmd.arg(url.to_string()); + // Avoid Nix from dumping logs related to `--override-input` use. Yes, this requires *double* use of `--quiet`. Also, `-qq` won't work until https://github.com/NixOS/nix/pull/11652 + cmd.args(["--quiet", "--quiet"]); }) .await?; let v = serde_json::from_slice::(&stdout)?;