1- extern crate semver;
2-
1+ use std:: collections:: HashSet ;
32use std:: path:: { Path , PathBuf } ;
43use std:: process:: Command ;
54use std:: str;
65
76use color_eyre:: { eyre, Result } ;
8- use semver:: Version ;
97use tempfile:: TempDir ;
108
11- /// Compares two semantic versions and returns their order.
12- ///
13- /// This function takes two version strings, parses them into `semver::Version` objects, and compares them.
14- /// It returns an `Ordering` indicating whether the current version is less than, equal to, or
15- /// greater than the target version.
16- ///
17- /// # Arguments
18- ///
19- /// * `current` - A string slice representing the current version.
20- /// * `target` - A string slice representing the target version to compare against.
21- ///
22- /// # Returns
23- ///
24- /// * `Result<std::cmp::Ordering>` - The comparison result.
25- pub fn compare_semver ( current : & str , target : & str ) -> Result < std:: cmp:: Ordering > {
26- let current = Version :: parse ( current) ?;
27- let target = Version :: parse ( target) ?;
28-
29- Ok ( current. cmp ( & target) )
30- }
31-
329/// Retrieves the installed Nix version as a string.
3310///
3411/// This function executes the `nix --version` command, parses the output to extract the version string,
@@ -59,6 +36,19 @@ pub fn get_nix_version() -> Result<String> {
5936 Err ( eyre:: eyre!( "Failed to extract version" ) )
6037}
6138
39+ /// Determines if the Nix binary is actually Lix
40+ ///
41+ /// # Returns
42+ ///
43+ /// * `Result<bool>` - True if the binary is Lix, false if it's standard Nix
44+ pub 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 ( ) ;
47+
48+ Ok ( output_str. contains ( "lix" ) )
49+ }
50+
51+ /// Represents an object that may be a temporary path
6252pub trait MaybeTempPath : std:: fmt:: Debug {
6353 fn get_path ( & self ) -> & Path ;
6454}
@@ -75,6 +65,11 @@ impl MaybeTempPath for (PathBuf, TempDir) {
7565 }
7666}
7767
68+ /// Gets the hostname of the current system
69+ ///
70+ /// # Returns
71+ ///
72+ /// * `Result<String>` - The hostname as a string or an error
7873pub fn get_hostname ( ) -> Result < String > {
7974 #[ cfg( not( target_os = "macos" ) ) ]
8075 {
@@ -102,3 +97,47 @@ pub fn get_hostname() -> Result<String> {
10297 Ok ( name. to_string ( ) )
10398 }
10499}
100+
101+ /// Retrieves all enabled experimental features in Nix.
102+ ///
103+ /// This function executes the `nix config show experimental-features` command and returns
104+ /// a HashSet of the enabled features.
105+ ///
106+ /// # Returns
107+ ///
108+ /// * `Result<HashSet<String>>` - A HashSet of enabled experimental features or an error.
109+ pub fn get_nix_experimental_features ( ) -> Result < HashSet < String > > {
110+ let output = Command :: new ( "nix" )
111+ . args ( [ "config" , "show" , "experimental-features" ] )
112+ . output ( ) ?;
113+
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+ }
120+
121+ let output_str = str:: from_utf8 ( & output. stdout ) ?;
122+ let enabled_features: HashSet < String > =
123+ output_str. split_whitespace ( ) . map ( String :: from) . collect ( ) ;
124+
125+ Ok ( enabled_features)
126+ }
127+
128+ /// Checks if all specified experimental features are enabled in Nix.
129+ ///
130+ /// # Arguments
131+ ///
132+ /// * `features` - A slice of string slices representing the features to check for.
133+ ///
134+ /// # Returns
135+ ///
136+ /// * `Result<bool>` - True if all specified features are enabled, false otherwise.
137+ pub fn has_all_experimental_features ( features : & [ & str ] ) -> Result < bool > {
138+ let enabled_features = get_nix_experimental_features ( ) ?;
139+ let features_set: HashSet < String > = features. iter ( ) . map ( |& s| s. to_string ( ) ) . collect ( ) ;
140+
141+ // Check if features_set is a subset of enabled_features
142+ Ok ( features_set. is_subset ( & enabled_features) )
143+ }
0 commit comments