From 6c6e712dc581df6bc5d25dd173019ba3f2fa73f9 Mon Sep 17 00:00:00 2001 From: Sander Date: Mon, 23 Dec 2024 12:16:53 +0400 Subject: [PATCH 1/3] devenv: avoid touching .devenv.flake.nix if not necessary Improves our integration with direnv, which is only capable of watching files based on mtime. We have to take care not to touch watched files needlessly. --- devenv/src/devenv.rs | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/devenv/src/devenv.rs b/devenv/src/devenv.rs index 464dd2bf4..c7fcb8cb5 100644 --- a/devenv/src/devenv.rs +++ b/devenv/src/devenv.rs @@ -773,7 +773,7 @@ impl Devenv { $ devenv init "}); } - std::fs::create_dir_all(&self.devenv_dot_gc) + fs::create_dir_all(&self.devenv_dot_gc) .unwrap_or_else(|_| panic!("Failed to create {}", self.devenv_dot_gc.display())); let mut flake_inputs = HashMap::new(); @@ -832,8 +832,14 @@ impl Devenv { is_testing ); let flake = FLAKE_TMPL.replace("__DEVENV_VARS__", &vars); - std::fs::write(self.devenv_root.join(DEVENV_FLAKE), flake) - .expect("Failed to write flake.nix"); + let flake_path = self.devenv_root.join(DEVENV_FLAKE); + + // Avoid writing the flake if it hasn't changed. + // direnv's watch_file triggers a reload based solely on mtime, which becomes annoying if we constantly touch this file. + let existing_flake = fs::read_to_string(&flake_path).unwrap_or_default(); + if flake != existing_flake { + fs::write(flake_path, flake).expect("Failed to write flake.nix"); + } self.assembled = true; Ok(()) From 1b397b7d0bd9b6b8b67b8cd8e314c3e2cf2cbe94 Mon Sep 17 00:00:00 2001 From: Sander Date: Mon, 23 Dec 2024 12:18:07 +0400 Subject: [PATCH 2/3] eval-cache: detect changes to TMPDIR and PWD --- devenv/src/cnix.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/devenv/src/cnix.rs b/devenv/src/cnix.rs index 89506bca6..2817e4cc8 100644 --- a/devenv/src/cnix.rs +++ b/devenv/src/cnix.rs @@ -355,7 +355,6 @@ impl<'a> Nix<'a> { cached_cmd.watch_path(self.devenv_root.join("devenv.yaml")); cached_cmd.watch_path(self.devenv_root.join("devenv.lock")); - cached_cmd.unwatch_path(self.devenv_root.join(".devenv.flake.nix")); // Ignore anything in .devenv. cached_cmd.unwatch_path(&self.devenv_dotfile); From cfe2a0bdb6220aa9215836e9923f3659ed2f0410 Mon Sep 17 00:00:00 2001 From: Sander Date: Mon, 23 Dec 2024 14:14:15 +0400 Subject: [PATCH 3/3] devenv: reduce cache misses from non-deterministic cache key order --- devenv/src/cnix.rs | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/devenv/src/cnix.rs b/devenv/src/cnix.rs index 2817e4cc8..7f881f735 100644 --- a/devenv/src/cnix.rs +++ b/devenv/src/cnix.rs @@ -447,8 +447,8 @@ impl<'a> Nix<'a> { options: &Options<'a>, ) -> Result { let mut final_args = Vec::new(); - let known_keys; - let pull_caches; + let known_keys_str; + let pull_caches_str; let mut push_cache = None; if !self.global_options.offline { @@ -463,28 +463,31 @@ impl<'a> Nix<'a> { push_cache = cachix_caches.caches.push.clone(); // handle cachix.pull if !cachix_caches.caches.pull.is_empty() { - pull_caches = cachix_caches + let mut pull_caches = cachix_caches .caches .pull .iter() .map(|cache| format!("https://{}.cachix.org", cache)) - .collect::>() - .join(" "); + .collect::>(); + pull_caches.sort(); + pull_caches_str = pull_caches.join(" "); final_args.extend_from_slice(&[ "--option", "extra-substituters", - &pull_caches, + &pull_caches_str, ]); - known_keys = cachix_caches + + let mut known_keys = cachix_caches .known_keys .values() .cloned() - .collect::>() - .join(" "); + .collect::>(); + known_keys.sort(); + known_keys_str = known_keys.join(" "); final_args.extend_from_slice(&[ "--option", "extra-trusted-public-keys", - &known_keys, + &known_keys_str, ]); } }