@@ -56,6 +56,7 @@ enum OsRebuildVariant {
5656impl OsBuildVmArgs {
5757 fn build_vm ( self ) -> Result < ( ) > {
5858 let final_attr = get_final_attr ( true , self . with_bootloader ) ;
59+ debug ! ( "Building VM with attribute: {}" , final_attr) ;
5960 self . common
6061 . rebuild ( OsRebuildVariant :: BuildVm , Some ( final_attr) )
6162 }
@@ -97,7 +98,16 @@ impl OsRebuildArgs {
9798 Some ( h) => h. to_owned ( ) ,
9899 None => match & system_hostname {
99100 Some ( hostname) => {
100- tracing:: warn!( "Guessing system is {hostname} for a VM image. If this isn't intended, use --hostname to change." ) ;
101+ // Only show the warning if we're explicitly building a VM
102+ // by directly calling build_vm(), not when the BuildVm variant
103+ // is used internally via other code paths
104+ if matches ! ( variant, OsRebuildVariant :: BuildVm )
105+ && final_attr
106+ . as_deref ( )
107+ . map_or ( false , |attr| attr == "vm" || attr == "vmWithBootLoader" )
108+ {
109+ tracing:: warn!( "Guessing system is {hostname} for a VM image. If this isn't intended, use --hostname to change." ) ;
110+ }
101111 hostname. clone ( )
102112 }
103113 None => return Err ( eyre ! ( "Unable to fetch hostname, and no hostname supplied." ) ) ,
@@ -143,7 +153,6 @@ impl OsRebuildArgs {
143153 BuildVm => "Building NixOS VM image" ,
144154 _ => "Building NixOS configuration" ,
145155 } ;
146-
147156 commands:: Build :: new ( toplevel)
148157 . extra_arg ( "--out-link" )
149158 . extra_arg ( out_path. get_path ( ) )
@@ -168,20 +177,49 @@ impl OsRebuildArgs {
168177 Some ( spec) => out_path. get_path ( ) . join ( "specialisation" ) . join ( spec) ,
169178 } ;
170179
171- debug ! ( "exists: {}" , target_profile. exists( ) ) ;
180+ debug ! ( "Output path: {}" , out_path. get_path( ) . display( ) ) ;
181+ debug ! ( "Target profile path: {}" , target_profile. display( ) ) ;
182+ debug ! ( "Target profile exists: {}" , target_profile. exists( ) ) ;
183+
184+ // Take a strong reference to out_path to prevent premature dropping
185+ // This prevents the tempdir from being dropped early, which would cause nvd diff to fail
186+ #[ allow( unused_variables) ]
187+ let keep_alive = out_path. get_path ( ) . to_owned ( ) ;
172188
173- target_profile. try_exists ( ) . context ( "Doesn't exist" ) ?;
189+ if !target_profile
190+ . try_exists ( )
191+ . context ( "Failed to check if target profile exists" ) ?
192+ {
193+ return Err ( eyre ! (
194+ "Target profile path does not exist: {}" ,
195+ target_profile. display( )
196+ ) ) ;
197+ }
174198
175199 if self . build_host . is_none ( )
176200 && self . target_host . is_none ( )
177201 && system_hostname. map_or ( true , |h| h == target_hostname)
178202 {
179- Command :: new ( "nvd" )
180- . arg ( "diff" )
181- . arg ( CURRENT_PROFILE )
182- . arg ( & target_profile)
183- . message ( "Comparing changes" )
184- . run ( ) ?;
203+ // Verify the target_profile path exists before attempting to use it
204+ if matches ! ( target_profile. try_exists( ) , Ok ( true ) ) {
205+ debug ! (
206+ "Comparing with target profile: {}" ,
207+ target_profile. display( )
208+ ) ;
209+
210+ Command :: new ( "nvd" )
211+ . arg ( "diff" )
212+ . arg ( CURRENT_PROFILE )
213+ . arg ( & target_profile)
214+ . message ( "Comparing changes" )
215+ . show_output ( true )
216+ . run ( ) ?;
217+ } else {
218+ warn ! (
219+ "Cannot compare changes - target profile path does not exist: {}" ,
220+ target_profile. display( )
221+ ) ;
222+ }
185223 } else {
186224 debug ! ( "Not running nvd as the target hostname is different from the system hostname." ) ;
187225 }
@@ -253,8 +291,12 @@ impl OsRebuildArgs {
253291 . run ( ) ?;
254292 }
255293
256- // Make sure out_path is not accidentally dropped
294+ // Make sure out_path is not acidentally dropped
257295 // https://docs.rs/tempfile/3.12.0/tempfile/index.html#early-drop-pitfall
296+ debug ! (
297+ "Completed operation with output path: {:?}" ,
298+ out_path. get_path( )
299+ ) ;
258300 drop ( out_path) ;
259301
260302 Ok ( ( ) )
@@ -305,6 +347,7 @@ impl OsRollbackArgs {
305347 . arg ( CURRENT_PROFILE )
306348 . arg ( & generation_link)
307349 . message ( "Comparing changes" )
350+ . show_output ( true )
308351 . run ( ) ?;
309352
310353 if self . dry {
@@ -345,9 +388,6 @@ impl OsRollbackArgs {
345388 . message ( "Setting system profile" )
346389 . run ( ) ?;
347390
348- // Set up rollback protection flag
349- let mut rollback_profile = false ;
350-
351391 // Determine the correct profile to use with specialisations
352392 let final_profile = match & target_specialisation {
353393 None => generation_link,
@@ -383,10 +423,8 @@ impl OsRollbackArgs {
383423 ) ;
384424 }
385425 Err ( e) => {
386- rollback_profile = true ;
387-
388426 // If activation fails, rollback the profile
389- if rollback_profile && current_gen_number > 0 {
427+ if current_gen_number > 0 {
390428 let current_gen_link =
391429 profile_dir. join ( format ! ( "system-{current_gen_number}-link" ) ) ;
392430
0 commit comments