@@ -15,23 +15,17 @@ pub enum NixCheckError {
1515 CommandError ( #[ from] command_run:: Error ) ,
1616}
1717
18- pub fn check_nix_version ( min_version : & str ) -> Result < ( ) , NixCheckError > {
19- info ! ( "Checking nix version" ) ;
20-
18+ fn compare_nix_versions ( min_version : & str , actual_version : & str ) -> Result < ( ) , NixCheckError > {
2119 let nix_version_requirement = VersionReq :: parse ( & format ! ( ">={}" , min_version) ) ?;
2220
23- let mut command =
24- Command :: with_args ( "nix" , & [ "eval" , "--raw" , "--expr" , "builtins.nixVersion" ] ) ;
25- command. log_command = false ;
26- command. enable_capture ( ) ;
27- let output = command. run ( ) ?;
2821 let nix_version = Version :: parse (
29- output
30- . stdout_string_lossy ( )
22+ actual_version
23+ . replace ( "pre" , ".0-pre" )
3124 . split ( |c : char | c != '.' && !c. is_ascii_digit ( ) )
3225 . next ( )
3326 . unwrap ( ) ,
3427 ) ?;
28+
3529 if !nix_version_requirement. matches ( & nix_version) {
3630 return Err ( NixCheckError :: IncompatibleNixVersion (
3731 nix_version,
@@ -40,3 +34,31 @@ pub fn check_nix_version(min_version: &str) -> Result<(), NixCheckError> {
4034 }
4135 Ok ( ( ) )
4236}
37+
38+ pub fn check_nix_version ( min_version : & str ) -> Result < ( ) , NixCheckError > {
39+ info ! ( "Checking nix version" ) ;
40+
41+ let mut command =
42+ Command :: with_args ( "nix" , & [ "eval" , "--raw" , "--expr" , "builtins.nixVersion" ] ) ;
43+ command. log_command = false ;
44+ command. enable_capture ( ) ;
45+ let output = command. run ( ) ?;
46+ return compare_nix_versions (
47+ min_version,
48+ output. stdout_string_lossy ( ) . into_owned ( ) . as_str ( ) ,
49+ ) ;
50+ }
51+
52+ #[ cfg( test) ]
53+ mod tests {
54+ use super :: * ;
55+
56+ #[ test]
57+ fn ok ( ) {
58+ assert ! ( compare_nix_versions( "2.4.0" , "2.3.0" ) . is_err( ) ) ;
59+ compare_nix_versions ( "2.4.0" , "2.4.0" ) . expect ( "Exactly matching version" ) ;
60+ compare_nix_versions ( "2.4.0" , "2.14.0" ) . expect ( "Other matching version" ) ;
61+ compare_nix_versions ( "2.4.0" , "2.33pre20251107_6a3e3982" )
62+ . expect ( "Matching prerelease version" ) ;
63+ }
64+ }
0 commit comments