1- use std:: { env, path:: PathBuf } ;
1+ use std:: { env, fmt , path:: PathBuf } ;
22
33use color_eyre:: eyre:: { Context , bail, eyre} ;
44use tracing:: { debug, warn} ;
55
66use crate :: {
77 Result ,
8- commands,
9- commands:: { Command , ElevationStrategy } ,
8+ commands:: { self , Command , ElevationStrategy } ,
109 installable:: Installable ,
1110 interface:: {
1211 DarwinArgs ,
1312 DarwinRebuildArgs ,
1413 DarwinReplArgs ,
1514 DarwinSubcommand ,
1615 DiffType ,
16+ NotifyAskMode ,
1717 } ,
1818 nixos:: toplevel_for,
19+ notify:: NotificationSender ,
1920 update:: update,
2021 util:: { get_hostname, print_dix_diff} ,
2122} ;
@@ -34,7 +35,7 @@ impl DarwinArgs {
3435 match self . subcommand {
3536 DarwinSubcommand :: Switch ( args) => args. rebuild ( & Switch , elevation) ,
3637 DarwinSubcommand :: Build ( args) => {
37- if args. common . ask || args. common . dry {
38+ if args. common . ask . is_some ( ) || args. common . dry {
3839 warn ! ( "`--ask` and `--dry` have no effect for `nh darwin build`" ) ;
3940 }
4041 args. rebuild ( & Build , elevation)
@@ -49,6 +50,16 @@ enum DarwinRebuildVariant {
4950 Build ,
5051}
5152
53+ impl fmt:: Display for DarwinRebuildVariant {
54+ fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
55+ let s = match self {
56+ DarwinRebuildVariant :: Build => "build" ,
57+ DarwinRebuildVariant :: Switch => "switch" ,
58+ } ;
59+ write ! ( f, "{s}" )
60+ }
61+ }
62+
5263impl DarwinRebuildArgs {
5364 fn rebuild (
5465 self ,
@@ -144,13 +155,29 @@ impl DarwinRebuildArgs {
144155 let _ = print_dix_diff ( & PathBuf :: from ( CURRENT_PROFILE ) , & target_profile) ;
145156 }
146157
147- if self . common . ask && !self . common . dry && !matches ! ( variant, Build ) {
148- let confirmation = inquire:: Confirm :: new ( "Apply the config?" )
149- . with_default ( false )
150- . prompt ( ) ?;
158+ if !self . common . dry && !matches ! ( variant, Build ) {
159+ if let Some ( ask) = self . common . ask {
160+ let confirmation = match ask {
161+ NotifyAskMode :: Prompt => {
162+ inquire:: Confirm :: new ( "Apply the config?" )
163+ . with_default ( false )
164+ . prompt ( ) ?
165+ } ,
166+ // MacOS doesn't support notification actions
167+ NotifyAskMode :: Notify | NotifyAskMode :: Both => {
168+ NotificationSender :: new ( & format ! ( "nh darwin {variant}" ) , "testing" )
169+ . send ( )
170+ . unwrap ( ) ;
171+
172+ inquire:: Confirm :: new ( "Apply the config?" )
173+ . with_default ( false )
174+ . prompt ( ) ?
175+ } ,
176+ } ;
151177
152- if !confirmation {
153- bail ! ( "User rejected the new config" ) ;
178+ if !confirmation {
179+ bail ! ( "User rejected the new config" ) ;
180+ }
154181 }
155182 }
156183
0 commit comments