@@ -4,6 +4,7 @@ use crate::path_clean::PathClean;
4
4
use crate :: trace:: Trace ;
5
5
use itertools:: Itertools ;
6
6
use nix:: unistd:: { access, AccessFlags } ;
7
+ use once_cell:: sync:: Lazy ;
7
8
use std:: collections:: { BTreeMap , HashSet } ;
8
9
use std:: env:: current_dir;
9
10
use std:: ffi:: { OsStr , OsString } ;
@@ -26,6 +27,11 @@ mod trace;
26
27
27
28
type EnvMap = BTreeMap < OsString , OsString > ;
28
29
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
+
29
35
/// Serialize environment variables in the same way as `env -0` does.
30
36
fn serialize_env ( env : & EnvMap ) -> Vec < u8 > {
31
37
let mut vec = Vec :: new ( ) ;
@@ -452,12 +458,9 @@ fn merge_env(mut env: EnvMap) -> EnvMap {
452
458
}
453
459
454
460
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) ) ?;
461
464
462
465
let env = read ( env_fname) . unwrap ( ) . pipe ( deserealize_env) ;
463
466
@@ -474,9 +477,7 @@ fn check_cache(hash: &str) -> Option<BTreeMap<OsString, OsString>> {
474
477
475
478
fn cache_write ( hash : & str , ext : & str , text : & [ u8 ] ) {
476
479
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) ) ?;
480
481
let mut file = File :: create ( fname) ?;
481
482
file. write_all ( text) ?;
482
483
Ok ( ( ) )
@@ -489,9 +490,7 @@ fn cache_write(hash: &str, ext: &str, text: &[u8]) {
489
490
490
491
fn cache_symlink ( hash : & str , ext : & str , target : & str ) {
491
492
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) ) ?;
495
494
let _ = std:: fs:: remove_file ( & fname) ;
496
495
std:: os:: unix:: fs:: symlink ( target, & fname) ?;
497
496
Ok ( ( ) )
0 commit comments