11use std:: collections:: HashSet ;
22use std:: path:: { Path , PathBuf } ;
3- use std:: process:: Command ;
43use std:: str;
54
65use color_eyre:: { eyre, Result } ;
76use tempfile:: TempDir ;
87
8+ use crate :: commands:: Command ;
9+
910/// Retrieves the installed Nix version as a string.
1011///
1112/// This function executes the `nix --version` command, parses the output to extract the version string,
@@ -15,10 +16,12 @@ use tempfile::TempDir;
1516///
1617/// * `Result<String>` - The Nix version string or an error if the version cannot be retrieved.
1718pub fn get_nix_version ( ) -> Result < String > {
18- let output = Command :: new ( "nix" ) . arg ( "--version" ) . output ( ) ?;
19+ let output = Command :: new ( "nix" )
20+ . arg ( "--version" )
21+ . run_capture ( ) ?
22+ . ok_or_else ( || eyre:: eyre!( "No output from command" ) ) ?;
1923
20- let output_str = str:: from_utf8 ( & output. stdout ) ?;
21- let version_str = output_str
24+ let version_str = output
2225 . lines ( )
2326 . next ( )
2427 . ok_or_else ( || eyre:: eyre!( "No version string found" ) ) ?;
@@ -42,10 +45,12 @@ pub fn get_nix_version() -> Result<String> {
4245///
4346/// * `Result<bool>` - True if the binary is Lix, false if it's standard Nix
4447pub fn is_lix ( ) -> Result < bool > {
45- let output = Command :: new ( "nix" ) . arg ( "--version" ) . output ( ) ?;
46- let output_str = str:: from_utf8 ( & output. stdout ) ?. to_lowercase ( ) ;
48+ let output = Command :: new ( "nix" )
49+ . arg ( "--version" )
50+ . run_capture ( ) ?
51+ . ok_or_else ( || eyre:: eyre!( "No output from command" ) ) ?;
4752
48- Ok ( output_str . contains ( "lix" ) )
53+ Ok ( output . to_lowercase ( ) . contains ( "lix" ) )
4954}
5055
5156/// Represents an object that may be a temporary path
@@ -109,16 +114,14 @@ pub fn get_hostname() -> Result<String> {
109114pub fn get_nix_experimental_features ( ) -> Result < HashSet < String > > {
110115 let output = Command :: new ( "nix" )
111116 . args ( [ "config" , "show" , "experimental-features" ] )
112- . output ( ) ?;
117+ . run_capture ( ) ?;
113118
114- if !output. status . success ( ) {
115- return Err ( eyre:: eyre!(
116- "Failed to get experimental features: {}" ,
117- String :: from_utf8_lossy( & output. stderr)
118- ) ) ;
119- }
119+ // If running with dry=true, output might be None
120+ let output_str = match output {
121+ Some ( output) => output,
122+ None => return Ok ( HashSet :: new ( ) ) ,
123+ } ;
120124
121- let output_str = str:: from_utf8 ( & output. stdout ) ?;
122125 let enabled_features: HashSet < String > =
123126 output_str. split_whitespace ( ) . map ( String :: from) . collect ( ) ;
124127
0 commit comments