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

nix_rs(nix_eval): Don't capture stderr #298

Merged
merged 11 commits into from
Oct 7, 2024
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"]);
srid marked this conversation as resolved.
Show resolved Hide resolved
})
.await?;
let v = serde_json::from_slice::<T>(&stdout)?;
Expand Down