Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 026564e

Browse files
committedMar 12, 2025·
slice
1 parent 5a28558 commit 026564e

File tree

8 files changed

+28
-30
lines changed

8 files changed

+28
-30
lines changed
 

‎crates/nix_rs/src/command.rs

+14-16
Original file line numberDiff line numberDiff line change
@@ -76,27 +76,29 @@ impl NixCmd {
7676
}
7777

7878
/// Return a [Command] for this [NixCmd] configuration
79-
pub fn command(&self, msubcommand: Option<&str>) -> Command {
79+
///
80+
/// Arguments:
81+
/// - `subcommands`: Optional subcommands to pass. Note that `NixArgs` will
82+
/// be passed *after* these subcommands.
83+
pub fn command(&self, subcommands: &[&str]) -> Command {
8084
let mut cmd = Command::new("nix");
8185
cmd.kill_on_drop(true);
82-
if let Some(subcommand) = msubcommand {
83-
cmd.arg(subcommand);
84-
}
86+
cmd.args(subcommands);
8587
cmd.args(&self.args);
8688
cmd
8789
}
8890

8991
/// Run nix with given args, interpreting stdout as JSON, parsing into `T`
9092
pub async fn run_with_args_expecting_json<T>(
9193
&self,
92-
msubcommand: Option<&str>,
94+
subcommands: &[&str],
9395
args: &[&str],
9496
) -> Result<T, NixCmdError>
9597
where
9698
T: serde::de::DeserializeOwned,
9799
{
98100
let stdout: Vec<u8> = self
99-
.run_with_returning_stdout(msubcommand, |c| {
101+
.run_with_returning_stdout(subcommands, |c| {
100102
c.args(args);
101103
})
102104
.await?;
@@ -107,15 +109,15 @@ impl NixCmd {
107109
/// Run nix with given args, interpreting parsing stdout, via [std::str::FromStr], into `T`
108110
pub async fn run_with_args_expecting_fromstr<T>(
109111
&self,
110-
msubcommand: Option<&str>,
112+
subcommands: &[&str],
111113
args: &[&str],
112114
) -> Result<T, NixCmdError>
113115
where
114116
T: std::str::FromStr,
115117
<T as std::str::FromStr>::Err: std::fmt::Display,
116118
{
117119
let stdout = self
118-
.run_with_returning_stdout(msubcommand, |c| {
120+
.run_with_returning_stdout(subcommands, |c| {
119121
c.args(args);
120122
})
121123
.await?;
@@ -127,13 +129,13 @@ impl NixCmd {
127129
/// Like [Self::run_with] but returns stdout as a [`Vec<u8>`]
128130
pub async fn run_with_returning_stdout<F>(
129131
&self,
130-
msubcommand: Option<&str>,
132+
subcommands: &[&str],
131133
f: F,
132134
) -> Result<Vec<u8>, CommandError>
133135
where
134136
F: FnOnce(&mut Command),
135137
{
136-
let mut cmd = self.command(msubcommand);
138+
let mut cmd = self.command(subcommands);
137139
f(&mut cmd);
138140
trace_cmd(&cmd);
139141

@@ -156,15 +158,11 @@ impl NixCmd {
156158
/// Run Nix with given [Command] customizations, while also tracing the command being run.
157159
///
158160
/// 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.
159-
pub async fn run_with<F>(
160-
&self,
161-
msubcommand: Option<&str>,
162-
f: F,
163-
) -> Result<Vec<u8>, CommandError>
161+
pub async fn run_with<F>(&self, subcommands: &[&str], f: F) -> Result<Vec<u8>, CommandError>
164162
where
165163
F: FnOnce(&mut Command),
166164
{
167-
let mut cmd = self.command(msubcommand);
165+
let mut cmd = self.command(subcommands);
168166
f(&mut cmd);
169167
trace_cmd(&cmd);
170168
let out = cmd.spawn()?.wait_with_output().await?;

‎crates/nix_rs/src/config.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,11 @@ impl NixConfig {
8080
) -> Result<NixConfig, super::command::NixCmdError> {
8181
let v = if nix_version >= &NIX_2_20_0 {
8282
nix_cmd
83-
.run_with_args_expecting_json(Some("config"), &["show", "--json"])
83+
.run_with_args_expecting_json(&["config", "show"], &["--json"])
8484
.await?
8585
} else {
8686
nix_cmd
87-
.run_with_args_expecting_json(Some("show-config"), &["--json"])
87+
.run_with_args_expecting_json(&["show-config"], &["--json"])
8888
.await?
8989
};
9090
Ok(v)

‎crates/nix_rs/src/copy.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ where
3232
I: IntoIterator<Item = P>,
3333
P: AsRef<Path> + AsRef<OsStr>,
3434
{
35-
cmd.run_with(Some("copy"), |cmd| {
35+
cmd.run_with(&["copy"], |cmd| {
3636
cmd.arg("-v");
3737
if let Some(uri) = options.from {
3838
cmd.arg("--from").arg(uri.to_string());

‎crates/nix_rs/src/flake/command.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ pub async fn run(
2020
args: Vec<String>,
2121
) -> Result<(), CommandError> {
2222
nixcmd
23-
.run_with(Some("run"), |cmd| {
23+
.run_with(&["run"], |cmd| {
2424
opts.use_in_command(cmd);
2525
cmd.args([url.to_string(), "--".to_string()]);
2626
cmd.args(args);
@@ -37,7 +37,7 @@ pub async fn develop(
3737
command: NonEmpty<String>,
3838
) -> Result<(), CommandError> {
3939
nixcmd
40-
.run_with(Some("develop"), |cmd| {
40+
.run_with(&["develop"], |cmd| {
4141
opts.use_in_command(cmd);
4242
cmd.args([url.to_string(), "-c".to_string()]);
4343
cmd.args(command);
@@ -53,7 +53,7 @@ pub async fn build(
5353
url: FlakeUrl,
5454
) -> Result<Vec<OutPath>, NixCmdError> {
5555
let stdout: Vec<u8> = cmd
56-
.run_with_returning_stdout(Some("build"), |c| {
56+
.run_with_returning_stdout(&["build"], |c| {
5757
opts.use_in_command(c);
5858
c.args(["--no-link", "--json", &url]);
5959
})
@@ -72,8 +72,8 @@ pub async fn lock(
7272
let mut cmd = cmd.clone();
7373
// Remove --override-input x y arguments, since they don't make sense for flake.lock check
7474
remove_override_inputs(&mut cmd.args.extra_nix_args);
75-
cmd.run_with(Some("flake"), |c| {
76-
c.args(["lock", url]); // XXX
75+
cmd.run_with(&["flake", "lock"], |c| {
76+
c.arg(url.to_string());
7777
opts.use_in_command(c);
7878
c.args(args);
7979
})
@@ -95,8 +95,8 @@ fn remove_override_inputs(vec: &mut Vec<String>) {
9595

9696
/// Run `nix flake check`
9797
pub async fn check(cmd: &NixCmd, opts: &FlakeOptions, url: &FlakeUrl) -> Result<(), NixCmdError> {
98-
cmd.run_with(Some("flake"), |c| {
99-
c.args(["check", url]); // XXX
98+
cmd.run_with(&["flake", "check"], |c| {
99+
c.arg(url.to_string());
100100
opts.use_in_command(c);
101101
})
102102
.await?;

‎crates/nix_rs/src/flake/eval.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ where
4646
T: serde::de::DeserializeOwned,
4747
{
4848
let stdout = nixcmd
49-
.run_with(Some("eval"), |cmd| {
49+
.run_with(&["eval"], |cmd| {
5050
cmd.stdout(Stdio::piped());
5151
if capture_stderr {
5252
cmd.stderr(Stdio::piped());

‎crates/nix_rs/src/flake/functions/core.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ pub trait FlakeFn {
6060
Self::Output: Sync + for<'de> Deserialize<'de>,
6161
{
6262
async move {
63-
let mut cmd = nixcmd.command(Some("build"));
63+
let mut cmd = nixcmd.command(&["build"]);
6464
cmd.args([Self::flake(), "-L", "--print-out-paths"]);
6565

6666
if impure {

‎crates/nix_rs/src/system_list.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ where
8585
T: Default + serde::de::DeserializeOwned,
8686
{
8787
let v = cmd
88-
.run_with_args_expecting_json::<T>(Some("eval"), &["--impure", "--json", "--expr", &expr])
88+
.run_with_args_expecting_json::<T>(&["eval"], &["--impure", "--json", "--expr", &expr])
8989
.await?;
9090
Ok(v)
9191
}

‎crates/nix_rs/src/version.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ impl NixVersion {
7676
#[instrument(name = "version")]
7777
pub async fn from_nix(cmd: &NixCmd) -> Result<NixVersion, super::command::NixCmdError> {
7878
let v = cmd
79-
.run_with_args_expecting_fromstr(None, &["--version"])
79+
.run_with_args_expecting_fromstr(&[], &["--version"])
8080
.await?;
8181
Ok(v)
8282
}

0 commit comments

Comments
 (0)
Please sign in to comment.