Skip to content

Commit 13bb210

Browse files
committed
Make XDG_DIRS global static
1 parent 5736afd commit 13bb210

File tree

3 files changed

+19
-12
lines changed

3 files changed

+19
-12
lines changed

Cargo.lock

+7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ blake3 = "0.3.3"
1010
bytelines = "2.2.2"
1111
itertools = "0.9.0"
1212
nix = "0.17.0"
13+
once_cell = "1.4.0"
1314
serde_json = "1.0.53"
1415
tempfile = "3.1.0"
1516
ufcs = "0.1.0"

src/main.rs

+11-12
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use crate::path_clean::PathClean;
44
use crate::trace::Trace;
55
use itertools::Itertools;
66
use nix::unistd::{access, AccessFlags};
7+
use once_cell::sync::Lazy;
78
use std::collections::{BTreeMap, HashSet};
89
use std::env::current_dir;
910
use std::ffi::{OsStr, OsString};
@@ -26,6 +27,11 @@ mod trace;
2627

2728
type EnvMap = BTreeMap<OsString, OsString>;
2829

30+
static XDG_DIRS: Lazy<xdg::BaseDirectories> = Lazy::new(|| {
31+
xdg::BaseDirectories::with_prefix("cached-nix-shell")
32+
.expect("Can't get find base cache directory")
33+
});
34+
2935
/// Serialize environment variables in the same way as `env -0` does.
3036
fn serialize_env(env: &EnvMap) -> Vec<u8> {
3137
let mut vec = Vec::new();
@@ -452,12 +458,9 @@ fn merge_env(mut env: EnvMap) -> EnvMap {
452458
}
453459

454460
fn check_cache(hash: &str) -> Option<BTreeMap<OsString, OsString>> {
455-
let xdg_dirs =
456-
xdg::BaseDirectories::with_prefix("cached-nix-shell").unwrap();
457-
458-
let env_fname = xdg_dirs.find_cache_file(format!("{}.env", hash))?;
459-
let drv_fname = xdg_dirs.find_cache_file(format!("{}.drv", hash))?;
460-
let trace_fname = xdg_dirs.find_cache_file(format!("{}.trace", hash))?;
461+
let env_fname = XDG_DIRS.find_cache_file(format!("{}.env", hash))?;
462+
let drv_fname = XDG_DIRS.find_cache_file(format!("{}.drv", hash))?;
463+
let trace_fname = XDG_DIRS.find_cache_file(format!("{}.trace", hash))?;
461464

462465
let env = read(env_fname).unwrap().pipe(deserealize_env);
463466

@@ -474,9 +477,7 @@ fn check_cache(hash: &str) -> Option<BTreeMap<OsString, OsString>> {
474477

475478
fn cache_write(hash: &str, ext: &str, text: &[u8]) {
476479
let f = || -> Result<(), std::io::Error> {
477-
let xdg_dirs =
478-
xdg::BaseDirectories::with_prefix("cached-nix-shell").unwrap();
479-
let fname = xdg_dirs.place_cache_file(format!("{}.{}", hash, ext))?;
480+
let fname = XDG_DIRS.place_cache_file(format!("{}.{}", hash, ext))?;
480481
let mut file = File::create(fname)?;
481482
file.write_all(text)?;
482483
Ok(())
@@ -489,9 +490,7 @@ fn cache_write(hash: &str, ext: &str, text: &[u8]) {
489490

490491
fn cache_symlink(hash: &str, ext: &str, target: &str) {
491492
let f = || -> Result<(), std::io::Error> {
492-
let xdg_dirs =
493-
xdg::BaseDirectories::with_prefix("cached-nix-shell").unwrap();
494-
let fname = xdg_dirs.place_cache_file(format!("{}.{}", hash, ext))?;
493+
let fname = XDG_DIRS.place_cache_file(format!("{}.{}", hash, ext))?;
495494
let _ = std::fs::remove_file(&fname);
496495
std::os::unix::fs::symlink(target, &fname)?;
497496
Ok(())

0 commit comments

Comments
 (0)