Skip to content

Commit

Permalink
Merge pull request #1649 from cachix/direnv-feats
Browse files Browse the repository at this point in the history
devenv: caching fixes and improvements
  • Loading branch information
domenkozar authored Dec 23, 2024
2 parents 9e1b29e + cfe2a0b commit 4b8677f
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 14 deletions.
24 changes: 13 additions & 11 deletions devenv/src/cnix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -448,8 +447,8 @@ impl<'a> Nix<'a> {
options: &Options<'a>,
) -> Result<std::process::Command> {
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 {
Expand All @@ -464,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::<Vec<String>>()
.join(" ");
.collect::<Vec<String>>();
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::<Vec<String>>()
.join(" ");
.collect::<Vec<String>>();
known_keys.sort();
known_keys_str = known_keys.join(" ");
final_args.extend_from_slice(&[
"--option",
"extra-trusted-public-keys",
&known_keys,
&known_keys_str,
]);
}
}
Expand Down
12 changes: 9 additions & 3 deletions devenv/src/devenv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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(())
Expand Down

0 comments on commit 4b8677f

Please sign in to comment.