-
-
Notifications
You must be signed in to change notification settings - Fork 92
Expand file tree
/
Copy pathinstallable.rs
More file actions
1095 lines (993 loc) · 29.7 KB
/
installable.rs
File metadata and controls
1095 lines (993 loc) · 29.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
use std::{env, fs, path::PathBuf};
use clap::{Arg, ArgAction, Args, FromArgMatches, error::ErrorKind};
use tracing::debug;
use yansi::{Color, Paint};
// Reference: https://nix.dev/manual/nix/2.18/command-ref/new-cli/nix
/// Command context for resolving installable env var priority
#[derive(Debug, Clone, Copy)]
pub enum CommandContext {
Os,
Home,
Darwin,
}
#[derive(Debug, Clone)]
pub enum Installable {
Flake {
reference: String,
attribute: Vec<String>,
},
File {
path: PathBuf,
attribute: Vec<String>,
},
Store {
path: PathBuf,
},
Expression {
expression: String,
attribute: Vec<String>,
},
/// Represents a deferred resolution of a missing installable.
/// This variant should be resolved to a concrete installable before use.
Unspecified,
}
impl FromArgMatches for Installable {
fn from_arg_matches(matches: &clap::ArgMatches) -> Result<Self, clap::Error> {
let mut matches = matches.clone();
Self::from_arg_matches_mut(&mut matches)
}
fn from_arg_matches_mut(
matches: &mut clap::ArgMatches,
) -> Result<Self, clap::Error> {
let installable = matches.get_one::<String>("installable");
let file = matches.get_one::<String>("file");
let expr = matches.get_one::<String>("expr");
if let Some(i) = installable {
let canonincal = fs::canonicalize(i);
if let Ok(p) = canonincal {
if p.starts_with("/nix/store") {
return Ok(Self::Store { path: p });
}
}
}
if let Some(f) = file {
return Ok(Self::File {
path: PathBuf::from(f),
attribute: parse_attribute(installable.cloned().unwrap_or_default()),
});
}
if let Some(e) = expr {
return Ok(Self::Expression {
expression: e.clone(),
attribute: parse_attribute(installable.cloned().unwrap_or_default()),
});
}
if let Some(i) = installable {
let mut elems = i.splitn(2, '#');
let reference = elems
.next()
.ok_or_else(|| {
clap::Error::raw(
ErrorKind::ValueValidation,
"Invalid installable format: missing reference",
)
})?
.to_owned();
return Ok(Self::Flake {
reference,
attribute: parse_attribute(
elems
.next()
.map(std::string::ToString::to_string)
.unwrap_or_default(),
),
});
}
Ok(Self::Unspecified)
}
fn update_from_arg_matches(
&mut self,
_matches: &clap::ArgMatches,
) -> Result<(), clap::Error> {
todo!()
}
}
impl Args for Installable {
fn augment_args(cmd: clap::Command) -> clap::Command {
cmd
.arg(
Arg::new("file")
.short('f')
.long("file")
.action(ArgAction::Set)
.hide(true),
)
.arg(
Arg::new("expr")
.short('E')
.long("expr")
.conflicts_with("file")
.hide(true)
.action(ArgAction::Set),
)
.arg(
Arg::new("installable")
.action(ArgAction::Set)
.value_name("INSTALLABLE")
.value_parser(clap::builder::PathBufValueParser::new())
.help("Which installable to use")
.long_help(format!(
r"Which installable to use.
Nix accepts various kinds of installables:
[FLAKEREF[#ATTRPATH]]
Flake reference with an optional attribute path.
[env: NH_FLAKE={}]
[env: NH_OS_FLAKE={}]
[env: NH_HOME_FLAKE={}]
[env: NH_DARWIN_FLAKE={}]
{}, {} <FILE> [ATTRPATH]
Path to file with an optional attribute path.
[env: NH_FILE={}]
[env: NH_ATTRP={}]
{}, {} <EXPR> [ATTRPATH]
Nix expression with an optional attribute path.
[PATH]
Path or symlink to a /nix/store path
",
env::var("NH_FLAKE").unwrap_or_default(),
env::var("NH_OS_FLAKE").unwrap_or_default(),
env::var("NH_HOME_FLAKE").unwrap_or_default(),
env::var("NH_DARWIN_FLAKE").unwrap_or_default(),
Paint::new("-f").fg(Color::Yellow),
Paint::new("--file").fg(Color::Yellow),
env::var("NH_FILE").unwrap_or_default(),
env::var("NH_ATTRP").unwrap_or_default(),
Paint::new("-e").fg(Color::Yellow),
Paint::new("--expr").fg(Color::Yellow),
)),
)
}
fn augment_args_for_update(cmd: clap::Command) -> clap::Command {
Self::augment_args(cmd)
}
}
// TODO: `parse_attribute` should handle quoted attributes, such as:
// foo."bar.baz" -> ["foo", "bar.baz"]
// Maybe we want to use chumsky for this?
pub fn parse_attribute<S>(s: S) -> Vec<String>
where
S: AsRef<str>,
{
let s = s.as_ref();
let mut res = Vec::new();
if s.is_empty() {
return res;
}
let mut in_quote = false;
let mut elem = String::new();
for char in s.chars() {
match char {
'.' => {
if in_quote {
elem.push(char);
} else {
res.push(elem.clone());
elem = String::new();
}
},
'"' => {
in_quote = !in_quote;
},
_ => elem.push(char),
}
}
res.push(elem);
assert!(!in_quote, "Failed to parse attribute: {s}");
res
}
#[test]
fn test_parse_attribute() {
assert_eq!(parse_attribute(r"foo.bar"), vec!["foo", "bar"]);
assert_eq!(parse_attribute(r#"foo."bar.baz""#), vec!["foo", "bar.baz"]);
let v: Vec<String> = vec![];
assert_eq!(parse_attribute(""), v);
}
impl Installable {
#[must_use]
pub fn to_args(&self) -> Vec<String> {
let mut res = Vec::new();
match self {
Self::Flake {
reference,
attribute,
} => {
res.push(format!("{reference}#{}", join_attribute(attribute)));
},
Self::File { path, attribute } => {
if let Some(path_str) = path.to_str() {
res.push(String::from("--file"));
res.push(path_str.to_string());
res.push(join_attribute(attribute));
} else {
// Return empty args if path contains invalid UTF-8
return Vec::new();
}
},
Self::Expression {
expression,
attribute,
} => {
res.push(String::from("--expr"));
res.push(expression.clone());
res.push(join_attribute(attribute));
},
Self::Store { path } => {
if let Some(path_str) = path.to_str() {
res.push(path_str.to_string());
} else {
// Return empty args if path contains invalid UTF-8
return Vec::new();
}
},
Self::Unspecified => {
unreachable!(
"Unspecified installable should have been resolved before calling \
to_args"
)
},
}
res
}
/// Resolves an installable, checking environment variables in
/// command-specific priority order.
///
/// If the installable is not Unspecified, returns it as-is.
/// Otherwise, checks env vars in priority order based on the command context:
/// - For NixOS: NH_OS_FLAKE, then NH_FLAKE
/// - For Home: NH_HOME_FLAKE, then NH_FLAKE
/// - For Darwin: NH_DARWIN_FLAKE, then NH_FLAKE
///
/// Returns an error if no installable could be resolved and no default is
/// available.
pub fn resolve(self, context: CommandContext) -> color_eyre::Result<Self> {
match self {
Self::Unspecified => {
// Check command-specific env var first
let specific_var = match context {
CommandContext::Os => "NH_OS_FLAKE",
CommandContext::Home => "NH_HOME_FLAKE",
CommandContext::Darwin => "NH_DARWIN_FLAKE",
};
if let Ok(flake) = env::var(specific_var) {
debug!("Using {specific_var}: {flake}");
let mut elems = flake.splitn(2, '#');
let reference = elems.next().ok_or_else(|| {
color_eyre::eyre::eyre!("{specific_var} missing reference part")
})?;
return Ok(Self::Flake {
reference: reference.to_owned(),
attribute: parse_attribute(
elems
.next()
.map(std::string::ToString::to_string)
.unwrap_or_default(),
),
});
}
// Fall back to NH_FILE
if let Ok(file) = env::var("NH_FILE") {
debug!("Using NH_FILE: {file}");
return Ok(Self::File {
path: PathBuf::from(file),
attribute: parse_attribute(
env::var("NH_ATTRP").unwrap_or_default(),
),
});
}
// Fall back to NH_FLAKE
if let Ok(flake) = env::var("NH_FLAKE") {
debug!("Using NH_FLAKE: {flake}");
let mut elems = flake.splitn(2, '#');
let reference = elems.next().ok_or_else(|| {
color_eyre::eyre::eyre!("NH_FLAKE missing reference part")
})?;
return Ok(Self::Flake {
reference: reference.to_owned(),
attribute: parse_attribute(
elems
.next()
.map(std::string::ToString::to_string)
.unwrap_or_default(),
),
});
}
// Return Unspecified - caller should try default resolution
Ok(Self::Unspecified)
},
other => Ok(other),
}
}
}
#[test]
fn test_installable_to_args() {
assert_eq!(
(Installable::Flake {
reference: String::from("w"),
attribute: ["x", "y.z"].into_iter().map(str::to_string).collect(),
})
.to_args(),
vec![r#"w#x."y.z""#]
);
assert_eq!(
(Installable::File {
path: PathBuf::from("w"),
attribute: ["x", "y.z"].into_iter().map(str::to_string).collect(),
})
.to_args(),
vec!["--file", "w", r#"x."y.z""#]
);
}
fn join_attribute<I>(attribute: I) -> String
where
I: IntoIterator,
I::Item: AsRef<str>,
{
let mut res = String::new();
let mut first = true;
for elem in attribute {
if first {
first = false;
} else {
res.push('.');
}
let s = elem.as_ref();
if s.contains('.') {
res.push_str(&format!(r#""{s}""#));
} else {
res.push_str(s);
}
}
res
}
#[test]
fn test_join_attribute() {
assert_eq!(join_attribute(vec!["foo", "bar"]), "foo.bar");
assert_eq!(join_attribute(vec!["foo", "bar.baz"]), r#"foo."bar.baz""#);
}
enum FallbackError {
NotFound,
PermissionDenied(PathBuf),
Io(std::io::Error),
}
/// Resolves a fallback flake directory.
///
/// # Returns
///
/// The resolved path to use as a flake reference. This handles three cases:
///
/// 1. Directory is a symlink -> returns the resolved directory path
/// 2. Directory is real but flake.nix is a symlink → returns the parent
/// directory of the resolved flake.nix
/// 3. Both are real -> returns the original directory
///
/// # Errors
///
/// Returns an error if:
///
/// - The directory does not exist
/// - The directory exists but does not contain a flake.nix file
/// - Permission is denied accessing the directory or flake.nix
/// - Any other I/O error occurs
fn resolve_fallback_flake_dir(
dir: &std::path::Path,
) -> Result<PathBuf, FallbackError> {
use std::io::ErrorKind;
// Check if the directory itself is a symlink
let dir_is_symlink = dir.is_symlink();
// Resolve the directory path
let resolved_dir = match fs::canonicalize(dir) {
Ok(p) => p,
Err(e) => {
return match e.kind() {
ErrorKind::NotFound => Err(FallbackError::NotFound),
ErrorKind::PermissionDenied => {
Err(FallbackError::PermissionDenied(dir.to_path_buf()))
},
_ => Err(FallbackError::Io(e)),
};
},
};
// If the directory itself was a symlink, use the resolved directory
if dir_is_symlink {
let flake_path = resolved_dir.join("flake.nix");
return match fs::metadata(&flake_path) {
Ok(m) if m.is_file() => Ok(resolved_dir),
Ok(_) => Err(FallbackError::NotFound),
Err(e) => {
match e.kind() {
ErrorKind::NotFound => Err(FallbackError::NotFound),
ErrorKind::PermissionDenied => {
Err(FallbackError::PermissionDenied(flake_path))
},
_ => Err(FallbackError::Io(e)),
}
},
};
}
// Directory is real, check flake.nix
let flake_path = resolved_dir.join("flake.nix");
// Check if flake.nix is a symlink
if flake_path.is_symlink() {
// Resolve the symlink to get the actual flake.nix location
match fs::canonicalize(&flake_path) {
Ok(resolved_flake) => {
// Use the parent directory of the resolved flake.nix
resolved_flake
.parent()
.map_or(Err(FallbackError::NotFound), |parent| {
Ok(parent.to_path_buf())
})
},
Err(e) => {
match e.kind() {
ErrorKind::NotFound => Err(FallbackError::NotFound),
ErrorKind::PermissionDenied => {
Err(FallbackError::PermissionDenied(flake_path))
},
_ => Err(FallbackError::Io(e)),
}
},
}
} else {
// flake.nix is a real file, check it exists
match fs::metadata(&flake_path) {
Ok(m) if m.is_file() => Ok(resolved_dir),
Ok(_) => Err(FallbackError::NotFound),
Err(e) => {
match e.kind() {
ErrorKind::NotFound => Err(FallbackError::NotFound),
ErrorKind::PermissionDenied => {
Err(FallbackError::PermissionDenied(flake_path))
},
_ => Err(FallbackError::Io(e)),
}
},
}
}
}
const FALLBACK_HELP_HINT: &str =
"See 'man nh' or https://github.com/nix-community/nh for more details.";
impl Installable {
#[must_use]
pub const fn str_kind(&self) -> &str {
match self {
Self::Flake { .. } => "flake",
Self::File { .. } => "file",
Self::Store { .. } => "store path",
Self::Expression { .. } => "expression",
Self::Unspecified => "unspecified",
}
}
/// Attempts to find a default installable for `NixOS` builds.
///
/// Checks if `/etc/nixos/flake.nix` exists and returns a flake installable
/// pointing to it if found. If the directory is a symlink, it is resolved
/// to its canonical path. Otherwise, returns an error with instructions on
/// how to specify an installable.
///
/// # Errors
///
/// Returns an error if:
///
/// - No flake is found at `/etc/nixos/flake.nix`
/// - Permission is denied accessing the path
/// - The resolved path contains invalid UTF-8
pub fn try_find_default_for_os() -> color_eyre::Result<Self> {
use tracing::warn;
let default_dir = std::path::Path::new("/etc/nixos");
match resolve_fallback_flake_dir(default_dir) {
Ok(resolved) => {
warn!(
"No installable was specified, falling back to {}",
resolved.display()
);
Ok(Self::Flake {
reference: resolved
.to_str()
.ok_or_else(|| {
color_eyre::eyre::eyre!(
"Resolved path {} contains invalid UTF-8",
resolved.display()
)
})?
.to_string(),
attribute: vec![],
})
},
Err(FallbackError::PermissionDenied(path)) => {
Err(color_eyre::eyre::eyre!(
"Permission denied accessing {}.\nPlease either:\n- Pass a flake \
path as an argument (e.g., 'nh os switch .')\n- Set the NH_FLAKE \
environment variable\n- Set the NH_OS_FLAKE environment \
variable\n\n{}",
path.display(),
FALLBACK_HELP_HINT
))
},
Err(FallbackError::Io(e)) => {
Err(color_eyre::eyre::eyre!(
"I/O error accessing {}: {}\n\n{}",
default_dir.display(),
e,
FALLBACK_HELP_HINT
))
},
Err(FallbackError::NotFound) => {
Err(color_eyre::eyre::eyre!(
"No installable specified and no flake found at \
{}/flake.nix.\nPlease either:\n- Pass a flake path as an argument \
(e.g., 'nh os switch .')\n- Set the NH_FLAKE environment \
variable\n- Set the NH_OS_FLAKE environment variable\n\n{}",
default_dir.display(),
FALLBACK_HELP_HINT
))
},
}
}
/// Attempts to find a default installable for Home Manager builds.
///
/// Checks if `$HOME/.config/home-manager/flake.nix` exists and returns a
/// flake installable pointing to it if found. If the directory is a
/// symlink, it is resolved to its canonical path. Otherwise, returns an
/// error with instructions on how to specify an installable.
///
/// # Errors
///
/// Returns an error if:
///
/// - The `HOME` environment variable is not set
/// - No flake is found at `$HOME/.config/home-manager/flake.nix`
/// - Permission is denied accessing the path
/// - The resolved path contains invalid UTF-8
pub fn try_find_default_for_home() -> color_eyre::Result<Self> {
use tracing::warn;
let home = env::var("HOME").map_err(|_| {
color_eyre::eyre::eyre!("HOME environment variable not set")
})?;
let default_dir = PathBuf::from(&home).join(".config/home-manager");
match resolve_fallback_flake_dir(&default_dir) {
Ok(resolved) => {
warn!(
"No installable was specified, falling back to {}",
resolved.display()
);
Ok(Self::Flake {
reference: resolved
.to_str()
.ok_or_else(|| {
color_eyre::eyre::eyre!(
"Resolved path {} contains invalid UTF-8",
resolved.display()
)
})?
.to_string(),
attribute: vec![],
})
},
Err(FallbackError::PermissionDenied(path)) => {
Err(color_eyre::eyre::eyre!(
"Permission denied accessing {}.\nPlease either:\n- Pass a flake \
path as an argument (e.g., 'nh home switch .')\n- Set the NH_FLAKE \
environment variable\n- Set the NH_HOME_FLAKE environment \
variable\n\n{}",
path.display(),
FALLBACK_HELP_HINT
))
},
Err(FallbackError::Io(e)) => {
Err(color_eyre::eyre::eyre!(
"I/O error accessing {}: {}\n\n{}",
default_dir.display(),
e,
FALLBACK_HELP_HINT
))
},
Err(FallbackError::NotFound) => {
Err(color_eyre::eyre::eyre!(
"No installable specified and no flake found at \
{}/flake.nix.\nPlease either:\n- Pass a flake path as an argument \
(e.g., 'nh home switch .')\n- Set the NH_FLAKE environment \
variable\n- Set the NH_HOME_FLAKE environment variable\n\n{}",
default_dir.display(),
FALLBACK_HELP_HINT
))
},
}
}
/// Attempts to find a default installable for Darwin builds.
///
/// Checks if `/etc/nix-darwin/flake.nix` exists and returns a flake
/// installable pointing to it if found. If the directory is a symlink,
/// it is resolved to its canonical path. Otherwise, returns an error with
/// instructions on how to specify an installable.
///
/// # Errors
///
/// Returns an error if:
///
/// - No flake is found at `/etc/nix-darwin/flake.nix`
/// - Permission is denied accessing the path
/// - The resolved path contains invalid UTF-8
pub fn try_find_default_for_darwin() -> color_eyre::Result<Self> {
use tracing::warn;
let default_dir = std::path::Path::new("/etc/nix-darwin");
match resolve_fallback_flake_dir(default_dir) {
Ok(resolved) => {
warn!(
"No installable was specified, falling back to {}",
resolved.display()
);
Ok(Self::Flake {
reference: resolved
.to_str()
.ok_or_else(|| {
color_eyre::eyre::eyre!(
"Resolved path {} contains invalid UTF-8",
resolved.display()
)
})?
.to_string(),
attribute: vec![],
})
},
Err(FallbackError::PermissionDenied(path)) => {
Err(color_eyre::eyre::eyre!(
"Permission denied accessing {}.\nPlease either:\n- Pass a flake \
path as an argument (e.g., 'nh darwin switch .')\n- Set the \
NH_FLAKE environment variable\n- Set the NH_DARWIN_FLAKE \
environment variable\n\n{}",
path.display(),
FALLBACK_HELP_HINT
))
},
Err(FallbackError::Io(e)) => {
Err(color_eyre::eyre::eyre!(
"I/O error accessing {}: {}\n\n{}",
default_dir.display(),
e,
FALLBACK_HELP_HINT
))
},
Err(FallbackError::NotFound) => {
Err(color_eyre::eyre::eyre!(
"No installable specified and no flake found at \
{}/flake.nix.\nPlease either:\n- Pass a flake path as an argument \
(e.g., 'nh darwin switch .')\n- Set the NH_FLAKE environment \
variable\n- Set the NH_DARWIN_FLAKE environment variable\n\n{}",
default_dir.display(),
FALLBACK_HELP_HINT
))
},
}
}
}
#[cfg(test)]
mod tests {
use std::env;
use serial_test::serial;
use super::*;
#[test]
fn test_resolve_non_unspecified_returns_unchanged() {
// Test that non-Unspecified installables are returned as-is
let flake = Installable::Flake {
reference: String::from("/path/to/flake"),
attribute: vec![String::from("host")],
};
let resolved = flake.clone().resolve(CommandContext::Os).unwrap();
assert_eq!(flake.to_args(), resolved.to_args());
let file = Installable::File {
path: PathBuf::from("/path/to/file.nix"),
attribute: vec![String::from("config")],
};
let resolved = file.clone().resolve(CommandContext::Home).unwrap();
assert_eq!(file.to_args(), resolved.to_args());
let store = Installable::Store {
path: PathBuf::from("/nix/store/abc"),
};
let resolved = store.clone().resolve(CommandContext::Darwin).unwrap();
assert_eq!(store.to_args(), resolved.to_args());
let expr = Installable::Expression {
expression: String::from("{ pkgs }: pkgs.hello"),
attribute: vec![],
};
let resolved = expr.clone().resolve(CommandContext::Os).unwrap();
assert_eq!(expr.to_args(), resolved.to_args());
}
#[test]
#[serial]
fn test_resolve_os_context_uses_nh_os_flake() {
// Set only NH_OS_FLAKE
unsafe {
env::set_var("NH_OS_FLAKE", "/etc/nixos#myhost");
env::remove_var("NH_FLAKE");
env::remove_var("NH_HOME_FLAKE");
env::remove_var("NH_DARWIN_FLAKE");
}
let resolved = Installable::Unspecified
.resolve(CommandContext::Os)
.unwrap();
match resolved {
Installable::Flake {
reference,
attribute,
} => {
assert_eq!(reference, "/etc/nixos");
assert_eq!(attribute, vec!["myhost"]);
},
_ => panic!("Expected Flake, got {:?}", resolved),
}
unsafe {
env::remove_var("NH_OS_FLAKE");
}
}
#[test]
#[serial]
fn test_resolve_os_context_prefers_os_flake_over_generic() {
// Set both NH_OS_FLAKE and NH_FLAKE
unsafe {
env::set_var("NH_OS_FLAKE", "/etc/nixos#myhost");
env::set_var("NH_FLAKE", "/home/user/flake#other");
}
let resolved = Installable::Unspecified
.resolve(CommandContext::Os)
.unwrap();
match resolved {
Installable::Flake {
reference,
attribute,
} => {
assert_eq!(reference, "/etc/nixos");
assert_eq!(attribute, vec!["myhost"]);
},
_ => panic!("Expected Flake, got {:?}", resolved),
}
unsafe {
env::remove_var("NH_OS_FLAKE");
env::remove_var("NH_FLAKE");
}
}
#[test]
#[serial]
fn test_resolve_os_context_falls_back_to_nh_flake() {
// Set only NH_FLAKE
unsafe {
env::remove_var("NH_OS_FLAKE");
env::set_var("NH_FLAKE", "/home/user/flake#fallback");
}
let resolved = Installable::Unspecified
.resolve(CommandContext::Os)
.unwrap();
match resolved {
Installable::Flake {
reference,
attribute,
} => {
assert_eq!(reference, "/home/user/flake");
assert_eq!(attribute, vec!["fallback"]);
},
_ => panic!("Expected Flake, got {:?}", resolved),
}
unsafe {
env::remove_var("NH_FLAKE");
}
}
#[test]
#[serial]
fn test_resolve_home_context_uses_nh_home_flake() {
// Set only NH_HOME_FLAKE
unsafe {
env::set_var("NH_HOME_FLAKE", "~/.config/home-manager#myuser");
env::remove_var("NH_FLAKE");
}
let resolved = Installable::Unspecified
.resolve(CommandContext::Home)
.unwrap();
match resolved {
Installable::Flake {
reference,
attribute,
} => {
assert_eq!(reference, "~/.config/home-manager");
assert_eq!(attribute, vec!["myuser"]);
},
_ => panic!("Expected Flake, got {:?}", resolved),
}
unsafe {
env::remove_var("NH_HOME_FLAKE");
}
}
#[test]
#[serial]
fn test_resolve_home_context_prefers_home_flake_over_generic() {
// Set both NH_HOME_FLAKE and NH_FLAKE
unsafe {
env::set_var("NH_HOME_FLAKE", "~/.config/home-manager#myuser");
env::set_var("NH_FLAKE", "/other/flake#other");
}
let resolved = Installable::Unspecified
.resolve(CommandContext::Home)
.unwrap();
match resolved {
Installable::Flake {
reference,
attribute,
} => {
assert_eq!(reference, "~/.config/home-manager");
assert_eq!(attribute, vec!["myuser"]);
},
_ => panic!("Expected Flake, got {:?}", resolved),
}
unsafe {
env::remove_var("NH_HOME_FLAKE");
env::remove_var("NH_FLAKE");
}
}
#[test]
#[serial]
fn test_resolve_darwin_context_uses_nh_darwin_flake() {
// Set only NH_DARWIN_FLAKE
unsafe {
env::set_var("NH_DARWIN_FLAKE", "/etc/nix-darwin#macbook");
env::remove_var("NH_FLAKE");
}
let resolved = Installable::Unspecified
.resolve(CommandContext::Darwin)
.unwrap();
match resolved {
Installable::Flake {
reference,
attribute,
} => {
assert_eq!(reference, "/etc/nix-darwin");
assert_eq!(attribute, vec!["macbook"]);
},
_ => panic!("Expected Flake, got {:?}", resolved),
}
unsafe {
env::remove_var("NH_DARWIN_FLAKE");
}
}
#[test]
#[serial]
fn test_resolve_darwin_context_prefers_darwin_flake_over_generic() {
// Set both NH_DARWIN_FLAKE and NH_FLAKE
unsafe {
env::set_var("NH_DARWIN_FLAKE", "/etc/nix-darwin#macbook");
env::set_var("NH_FLAKE", "/other/flake#other");
}
let resolved = Installable::Unspecified
.resolve(CommandContext::Darwin)
.unwrap();
match resolved {
Installable::Flake {
reference,
attribute,
} => {
assert_eq!(reference, "/etc/nix-darwin");
assert_eq!(attribute, vec!["macbook"]);
},
_ => panic!("Expected Flake, got {:?}", resolved),
}
unsafe {
env::remove_var("NH_DARWIN_FLAKE");
env::remove_var("NH_FLAKE");
}
}
#[test]
#[serial]
fn test_resolve_no_env_vars_returns_unspecified() {
// Clear all env vars
unsafe {
env::remove_var("NH_FLAKE");
env::remove_var("NH_OS_FLAKE");
env::remove_var("NH_HOME_FLAKE");
env::remove_var("NH_DARWIN_FLAKE");
}
let resolved = Installable::Unspecified
.resolve(CommandContext::Os)
.unwrap();
assert!(matches!(resolved, Installable::Unspecified));
let resolved = Installable::Unspecified
.resolve(CommandContext::Home)
.unwrap();
assert!(matches!(resolved, Installable::Unspecified));
let resolved = Installable::Unspecified
.resolve(CommandContext::Darwin)
.unwrap();
assert!(matches!(resolved, Installable::Unspecified));
}