Skip to content

Commit

Permalink
nix_rs(nix_eval): Don't capture stderr (#298)
Browse files Browse the repository at this point in the history
Resolves #209 

PRs filed tangential to this:
- NixOS/nix#11652

---------

Co-authored-by: Sridhar Ratnakumar <srid@srid.ca>
  • Loading branch information
shivaraj-bh and srid authored Oct 7, 2024
1 parent e1261bf commit 1555b72
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 9 deletions.
4 changes: 4 additions & 0 deletions crates/nix_rs/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# Changelog

## Unreleased

- **`eval::nix_eval`**
- Display evaluation progress
- Decrease logging verbosity
- **`flake::schema`**
- Don't hardcode flake schema types
- **`config`**
Expand Down
12 changes: 7 additions & 5 deletions crates/nix_rs/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,20 +175,22 @@ impl NixCmd {
}

/// Run Nix with given [Command] customizations, while also tracing the command being run.
pub async fn run_with<F>(&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<F>(&self, f: F) -> Result<Vec<u8>, 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(),
})
}
}
Expand Down
3 changes: 2 additions & 1 deletion crates/nix_rs/src/copy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,6 @@ pub async fn nix_copy(
cmd.run_with(|cmd| {
cmd.args(args);
})
.await
.await?;
Ok(())
}
6 changes: 4 additions & 2 deletions crates/nix_rs/src/flake/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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`
Expand Down
7 changes: 6 additions & 1 deletion crates/nix_rs/src/flake/eval.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
//! Work with `nix eval`
use std::process::Stdio;

use crate::command::{CommandError, NixCmd, NixCmdError};

use super::{command::FlakeOptions, url::FlakeUrl};
Expand All @@ -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::<T>(&stdout)?;
Expand Down

0 comments on commit 1555b72

Please sign in to comment.