Skip to content

Commit 9f6da63

Browse files
authored
Merge pull request #1753 from cachix/fix-search
devenv: improve `devenv search` performance and hide warnings
2 parents d811e88 + 08a6f1b commit 9f6da63

File tree

2 files changed

+54
-24
lines changed

2 files changed

+54
-24
lines changed

devenv/src/cnix.rs

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ use std::process;
1414
use std::time::{SystemTime, UNIX_EPOCH};
1515
use tracing::{debug, error, info, warn};
1616

17-
pub struct Nix<'a> {
18-
pub options: Options<'a>,
17+
pub struct Nix {
18+
pub options: Options,
1919
pool: SqlitePool,
2020
// TODO: all these shouldn't be here
2121
config: config::Config,
@@ -29,7 +29,7 @@ pub struct Nix<'a> {
2929
}
3030

3131
#[derive(Clone)]
32-
pub struct Options<'a> {
32+
pub struct Options {
3333
/// Run `exec` to replace the shell with the command.
3434
pub replace_shell: bool,
3535
/// Error out if the command returns a non-zero status code.
@@ -41,10 +41,10 @@ pub struct Options<'a> {
4141
/// Log the stdout of the command.
4242
pub logging_stdout: bool,
4343
/// Extra flags to pass to nix commands.
44-
pub nix_flags: &'a [&'a str],
44+
pub nix_flags: &'static [&'static str],
4545
}
4646

47-
impl Default for Options<'_> {
47+
impl Default for Options {
4848
fn default() -> Self {
4949
Self {
5050
replace_shell: false,
@@ -68,7 +68,7 @@ impl Default for Options<'_> {
6868
}
6969
}
7070

71-
impl<'a> Nix<'a> {
71+
impl Nix {
7272
pub async fn new<P: AsRef<Path>>(
7373
config: config::Config,
7474
global_options: cli::GlobalOptions,
@@ -186,15 +186,20 @@ impl<'a> Nix<'a> {
186186
Ok(())
187187
}
188188

189-
pub async fn build(&self, attributes: &[&str]) -> Result<Vec<PathBuf>> {
189+
pub async fn build(
190+
&self,
191+
attributes: &[&str],
192+
options: Option<Options>,
193+
) -> Result<Vec<PathBuf>> {
190194
if attributes.is_empty() {
191195
return Ok(Vec::new());
192196
}
193197

194-
let options = Options {
198+
let options = options.unwrap_or(Options {
195199
cache_output: true,
196200
..self.options
197-
};
201+
});
202+
198203
// TODO: use eval underneath
199204
let mut args: Vec<String> = vec![
200205
"build".to_string(),
@@ -272,7 +277,18 @@ impl<'a> Nix<'a> {
272277
pub async fn search(&self, name: &str) -> Result<devenv_eval_cache::Output> {
273278
self.run_nix_with_substituters(
274279
"nix",
275-
&["search", "--inputs-from", ".", "--json", "nixpkgs", name],
280+
&[
281+
"search",
282+
"--inputs-from",
283+
".",
284+
"--quiet",
285+
"--option",
286+
"eval-cache",
287+
"true",
288+
"--json",
289+
"nixpkgs",
290+
name,
291+
],
276292
&self.options,
277293
)
278294
.await
@@ -298,7 +314,7 @@ impl<'a> Nix<'a> {
298314
&self,
299315
command: &str,
300316
args: &[&str],
301-
options: &Options<'a>,
317+
options: &Options,
302318
) -> Result<devenv_eval_cache::Output> {
303319
let cmd = self.prepare_command(command, args, options)?;
304320
self.run_nix_command(cmd, options).await
@@ -308,7 +324,7 @@ impl<'a> Nix<'a> {
308324
&self,
309325
command: &str,
310326
args: &[&str],
311-
options: &Options<'a>,
327+
options: &Options,
312328
) -> Result<devenv_eval_cache::Output> {
313329
let cmd = self
314330
.prepare_command_with_substituters(command, args, options)
@@ -319,7 +335,7 @@ impl<'a> Nix<'a> {
319335
async fn run_nix_command(
320336
&self,
321337
mut cmd: std::process::Command,
322-
options: &Options<'a>,
338+
options: &Options,
323339
) -> Result<devenv_eval_cache::Output> {
324340
use devenv_eval_cache::internal_log::Verbosity;
325341
use devenv_eval_cache::{supports_eval_caching, CachedCommand};
@@ -444,7 +460,7 @@ impl<'a> Nix<'a> {
444460
&self,
445461
command: &str,
446462
args: &[&str],
447-
options: &Options<'a>,
463+
options: &Options,
448464
) -> Result<std::process::Command> {
449465
let mut final_args = Vec::new();
450466
let known_keys_str;
@@ -532,7 +548,7 @@ impl<'a> Nix<'a> {
532548
&self,
533549
command: &str,
534550
args: &[&str],
535-
options: &Options<'a>,
551+
options: &Options,
536552
) -> Result<std::process::Command> {
537553
let mut flags = options.nix_flags.to_vec();
538554
flags.push("--max-jobs");

devenv/src/devenv.rs

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ pub struct Devenv {
5353
pub config: config::Config,
5454
pub global_options: cli::GlobalOptions,
5555

56-
nix: cnix::Nix<'static>,
56+
nix: cnix::Nix,
5757

5858
// All kinds of paths
5959
devenv_root: PathBuf,
@@ -314,7 +314,7 @@ impl Devenv {
314314

315315
let container_store_path = self
316316
.nix
317-
.build(&[&format!("devenv.containers.{name}.derivation")])
317+
.build(&[&format!("devenv.containers.{name}.derivation")], None)
318318
.await?;
319319
let container_store_path = container_store_path[0]
320320
.to_str()
@@ -343,7 +343,7 @@ impl Devenv {
343343
async move {
344344
let copy_script = self
345345
.nix
346-
.build(&[&format!("devenv.containers.{name}.copyScript")])
346+
.build(&[&format!("devenv.containers.{name}.copyScript")], None)
347347
.await?;
348348
let copy_script = &copy_script[0];
349349
let copy_script_string = &copy_script.to_string_lossy();
@@ -393,7 +393,7 @@ impl Devenv {
393393
async move {
394394
let run_script = self
395395
.nix
396-
.build(&[&format!("devenv.containers.{name}.dockerRun")])
396+
.build(&[&format!("devenv.containers.{name}.dockerRun")], None)
397397
.await?;
398398

399399
let status = std::process::Command::new(&run_script[0])
@@ -462,7 +462,15 @@ impl Devenv {
462462
pub async fn search(&mut self, name: &str) -> Result<()> {
463463
self.assemble(false)?;
464464

465-
let options = self.nix.build(&["optionsJSON"]).await?;
465+
let build_options = cnix::Options {
466+
logging: false,
467+
cache_output: true,
468+
..Default::default()
469+
};
470+
let options = self
471+
.nix
472+
.build(&["optionsJSON"], Some(build_options))
473+
.await?;
466474
let options_path = options[0]
467475
.join("share")
468476
.join("doc")
@@ -531,7 +539,7 @@ impl Devenv {
531539
// TODO: No newline
532540
let span = info_span!("tasks_run", devenv.user_message = "Evaluating tasks");
533541
self.nix
534-
.build(&["devenv.task.config"])
542+
.build(&["devenv.task.config"], None)
535543
.instrument(span)
536544
.await?
537545
};
@@ -566,7 +574,10 @@ impl Devenv {
566574
// collect tests
567575
let test_script = {
568576
let span = info_span!("test", devenv.user_message = "Building tests");
569-
self.nix.build(&["devenv.test"]).instrument(span).await?
577+
self.nix
578+
.build(&["devenv.test"], None)
579+
.instrument(span)
580+
.await?
570581
};
571582
let test_script = test_script[0].to_string_lossy().to_string();
572583

@@ -643,7 +654,10 @@ impl Devenv {
643654
};
644655
let paths = self
645656
.nix
646-
.build(&attributes.iter().map(AsRef::as_ref).collect::<Vec<&str>>())
657+
.build(
658+
&attributes.iter().map(AsRef::as_ref).collect::<Vec<&str>>(),
659+
None,
660+
)
647661
.await?;
648662
for path in paths {
649663
println!("{}", path.display());
@@ -668,7 +682,7 @@ impl Devenv {
668682
devenv.user_message = "Building processes"
669683
);
670684
let proc_script_string = async {
671-
let proc_script = self.nix.build(&["procfileScript"]).await?;
685+
let proc_script = self.nix.build(&["procfileScript"], None).await?;
672686
let proc_script_string = proc_script[0]
673687
.to_str()
674688
.expect("Failed to get proc script path")

0 commit comments

Comments
 (0)