@@ -29,13 +29,44 @@ pub struct Nix<'a> {
29
29
30
30
#[ derive( Clone ) ]
31
31
pub struct Options < ' a > {
32
+ /// Run `exec` to replace the shell with the command.
32
33
pub replace_shell : bool ,
34
+ /// Error out if the command returns a non-zero status code.
35
+ pub bail_on_error : bool ,
36
+ /// Cache the output of the command. This is opt-in per command.
33
37
pub cache_output : bool ,
38
+ /// Enable logging.
34
39
pub logging : bool ,
40
+ /// Log the stdout of the command.
35
41
pub logging_stdout : bool ,
42
+ /// Extra flags to pass to nix commands.
36
43
pub nix_flags : & ' a [ & ' a str ] ,
37
44
}
38
45
46
+ impl Default for Options < ' _ > {
47
+ fn default ( ) -> Self {
48
+ Self {
49
+ replace_shell : false ,
50
+ bail_on_error : true ,
51
+ // Individual commands opt into caching
52
+ cache_output : false ,
53
+ logging : true ,
54
+ logging_stdout : false ,
55
+ nix_flags : & [
56
+ "--show-trace" ,
57
+ "--extra-experimental-features" ,
58
+ "nix-command" ,
59
+ "--extra-experimental-features" ,
60
+ "flakes" ,
61
+ "--option" ,
62
+ "warn-dirty" ,
63
+ "false" ,
64
+ "--keep-going" ,
65
+ ] ,
66
+ }
67
+ }
68
+ }
69
+
39
70
impl < ' a > Nix < ' a > {
40
71
pub async fn new < P : AsRef < Path > > (
41
72
logger : log:: Logger ,
@@ -54,24 +85,7 @@ impl<'a> Nix<'a> {
54
85
let devenv_root = devenv_root. as_ref ( ) . to_path_buf ( ) ;
55
86
56
87
let cachix_caches = RefCell :: new ( None ) ;
57
- let options = Options {
58
- replace_shell : false ,
59
- // Individual commands opt into caching
60
- cache_output : false ,
61
- logging : true ,
62
- logging_stdout : false ,
63
- nix_flags : & [
64
- "--show-trace" ,
65
- "--extra-experimental-features" ,
66
- "nix-command" ,
67
- "--extra-experimental-features" ,
68
- "flakes" ,
69
- "--option" ,
70
- "warn-dirty" ,
71
- "false" ,
72
- "--keep-going" ,
73
- ] ,
74
- } ;
88
+ let options = Options :: default ( ) ;
75
89
76
90
let database_url = format ! (
77
91
"sqlite:{}/nix-eval-cache.db" ,
@@ -106,6 +120,7 @@ impl<'a> Nix<'a> {
106
120
// Cannot cache this because we don't get the derivation back.
107
121
// We'd need to switch to print-dev-env and our own `nix develop`.
108
122
cache_output : false ,
123
+ bail_on_error : false ,
109
124
replace_shell,
110
125
..self . options
111
126
} ;
@@ -393,27 +408,32 @@ impl<'a> Nix<'a> {
393
408
Some ( code) => format ! ( "with exit code {}" , code) ,
394
409
None => "without exit code" . to_string ( ) ,
395
410
} ;
396
- if options. logging {
397
- eprintln ! ( ) ;
398
- self . logger . error ( & format ! (
399
- "Command produced the following output:\n {}\n {}" ,
400
- String :: from_utf8_lossy( & result. stdout) ,
401
- String :: from_utf8_lossy( & result. stderr) ,
402
- ) ) ;
403
- }
411
+
404
412
if self . global_options . nix_debugger
405
413
&& cmd. get_program ( ) . to_string_lossy ( ) . ends_with ( "bin/nix" )
406
414
{
407
415
self . logger . info ( "Starting Nix debugger ..." ) ;
408
416
cmd. arg ( "--debugger" ) . exec ( ) ;
409
417
}
410
- bail ! ( format!(
411
- "Command `{}` failed with {code}" ,
412
- display_command( & cmd)
413
- ) )
414
- } else {
415
- Ok ( result)
418
+
419
+ if options. bail_on_error {
420
+ if options. logging {
421
+ eprintln ! ( ) ;
422
+ self . logger . error ( & format ! (
423
+ "Command produced the following output:\n {}\n {}" ,
424
+ String :: from_utf8_lossy( & result. stdout) ,
425
+ String :: from_utf8_lossy( & result. stderr) ,
426
+ ) ) ;
427
+ }
428
+
429
+ bail ! ( format!(
430
+ "Command `{}` failed with {code}" ,
431
+ display_command( & cmd)
432
+ ) )
433
+ }
416
434
}
435
+
436
+ Ok ( result)
417
437
}
418
438
419
439
// We have a separate function to avoid recursion as this needs to call self.prepare_command
0 commit comments