@@ -4,12 +4,15 @@ use crate::ofborg::partition_result;
44
55use std:: collections:: HashMap ;
66use std:: env;
7+ use std:: error:: Error ;
78use std:: ffi:: OsStr ;
89use std:: fmt;
910use std:: fs;
11+ use std:: io;
1012use std:: io:: { BufRead , BufReader , Seek , SeekFrom } ;
1113use std:: path:: Path ;
1214use std:: process:: { Command , Stdio } ;
15+ use tracing:: { debug, info} ;
1316
1417use tempfile:: tempfile;
1518
@@ -137,6 +140,47 @@ impl Nix {
137140 n
138141 }
139142
143+ pub fn safely_query_cache_for_attr (
144+ & self ,
145+ nixpkgs : & Path ,
146+ file : File ,
147+ attr : String ,
148+ ) -> Result < bool , Box < dyn Error > > {
149+ let mut command = self . safe_command :: < & OsStr > ( & Operation :: Instantiate , nixpkgs, & [ ] , & [ ] ) ;
150+ self . set_attrs_command ( & mut command, file, vec ! [ attr] ) ;
151+ let output = command
152+ . stderr ( Stdio :: piped ( ) )
153+ . stdout ( Stdio :: piped ( ) )
154+ . output ( ) ?;
155+ debug ! ( "{}" , String :: from_utf8( output. stderr) ?. trim( ) ) ;
156+
157+ let drv = String :: from_utf8 ( output. stdout ) ?;
158+ let output = Command :: new ( "nix-store" )
159+ . args ( & [ "-q" , "--binding" , "out" ] )
160+ . arg ( drv. trim ( ) )
161+ . stderr ( Stdio :: piped ( ) )
162+ . stdout ( Stdio :: piped ( ) )
163+ . output ( ) ?;
164+ debug ! ( "{}" , String :: from_utf8( output. stderr) ?. trim( ) ) ;
165+ if !output. status . success ( ) {
166+ let err = io:: Error :: new ( io:: ErrorKind :: Other , "Could not evaluate stdenv" ) ;
167+ return Err ( Box :: new ( err) ) ;
168+ }
169+
170+ let out = String :: from_utf8 ( output. stdout ) ?;
171+ info ! ( "stdenv {}" , out) ;
172+ let output = Command :: new ( "nix-store" )
173+ . args ( & [ "--option" , "store" , "https://cache.nixos.org" ] )
174+ . args ( & [ "-q" , "--size" ] )
175+ . arg ( out. trim ( ) )
176+ . stderr ( Stdio :: piped ( ) )
177+ . stdout ( Stdio :: null ( ) )
178+ . output ( ) ?;
179+ debug ! ( "{}" , String :: from_utf8( output. stderr) ?. trim( ) ) ;
180+
181+ Ok ( output. status . success ( ) )
182+ }
183+
140184 pub fn safely_partition_instantiable_attrs (
141185 & self ,
142186 nixpkgs : & Path ,
0 commit comments