Skip to content

Commit c5730bf

Browse files
committed
refactor: update installable resolution to use fallback option
1 parent b5f4b62 commit c5730bf

File tree

4 files changed

+28
-25
lines changed

4 files changed

+28
-25
lines changed

src/darwin.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,8 @@ impl DarwinRebuildArgs {
7272
debug!(?out_path);
7373

7474
// Resolve the installable from env var or from the provided argument
75-
let installable =
76-
platform::resolve_env_installable("NH_DARWIN_FLAKE", self.common.installable.clone());
75+
let installable = platform::resolve_env_installable("NH_DARWIN_FLAKE")
76+
.unwrap_or_else(|| self.common.installable.clone());
7777

7878
// Build the darwin configuration with proper attribute path handling
7979
let target_profile = platform::handle_rebuild_workflow(
@@ -151,7 +151,8 @@ impl DarwinReplArgs {
151151

152152
// Open an interactive REPL session for exploring darwin configurations
153153
platform::run_repl(
154-
platform::resolve_env_installable("NH_DARWIN_FLAKE", self.installable),
154+
platform::resolve_env_installable("NH_DARWIN_FLAKE")
155+
.unwrap_or_else(|| self.installable),
155156
"darwinConfigurations",
156157
&[], // REPL doesn't need additional path elements
157158
Some(hostname),

src/home.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,8 @@ impl HomeRebuildArgs {
6767
debug!(?out_path);
6868

6969
// Use NH_HOME_FLAKE if available, otherwise use the provided installable
70-
let installable =
71-
platform::resolve_env_installable("NH_HOME_FLAKE", self.common.installable.clone());
70+
let installable = platform::resolve_env_installable("NH_HOME_FLAKE")
71+
.unwrap_or_else(|| self.common.installable.clone());
7272

7373
// Set up the specialisation path
7474
let spec_location =
@@ -168,7 +168,8 @@ impl HomeReplArgs {
168168
/// debugging or exploring available options.
169169
fn run(self) -> Result<()> {
170170
// Use NH_HOME_FLAKE if available, otherwise use the provided installable
171-
let installable = platform::resolve_env_installable("NH_HOME_FLAKE", self.installable);
171+
let installable =
172+
platform::resolve_env_installable("NH_HOME_FLAKE").unwrap_or_else(|| self.installable);
172173

173174
// Launch an interactive REPL session for exploring the configuration
174175
platform::run_repl(

src/nixos.rs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -36,19 +36,19 @@ impl interface::OsArgs {
3636
/// - `BuildVm`: Builds a `NixOS` VM image
3737
pub fn run(self) -> Result<()> {
3838
use OsRebuildVariant::{Boot, Build, Switch, Test};
39-
// Always resolve installable from env var at the top
40-
let resolved_installable = platform::resolve_env_installable(
41-
"NH_OS_FLAKE",
42-
match &self.subcommand {
43-
OsSubcommand::Boot(args) => args.common.installable.clone(),
44-
OsSubcommand::Test(args) => args.common.installable.clone(),
45-
OsSubcommand::Switch(args) => args.common.installable.clone(),
46-
OsSubcommand::Build(args) => args.common.installable.clone(),
47-
OsSubcommand::BuildVm(args) => args.common.common.installable.clone(),
48-
OsSubcommand::Repl(args) => args.installable.clone(),
49-
_ => Installable::default(), // fallback for Info/Rollback, not used
50-
},
51-
);
39+
// Always resolve installable from env var at the top, or use the provided one
40+
let fallback_installable = match &self.subcommand {
41+
OsSubcommand::Boot(args) => args.common.installable.clone(),
42+
OsSubcommand::Test(args) => args.common.installable.clone(),
43+
OsSubcommand::Switch(args) => args.common.installable.clone(),
44+
OsSubcommand::Build(args) => args.common.installable.clone(),
45+
OsSubcommand::BuildVm(args) => args.common.common.installable.clone(),
46+
OsSubcommand::Repl(args) => args.installable.clone(),
47+
_ => Installable::default(), // fallback for Info/Rollback, not used
48+
};
49+
50+
let resolved_installable =
51+
platform::resolve_env_installable("NH_OS_FLAKE").unwrap_or(fallback_installable);
5252
match self.subcommand {
5353
OsSubcommand::Boot(args) => {
5454
args.rebuild_with_installable(Boot, None, resolved_installable)

src/util/platform.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,12 @@ use tracing::{debug, info, warn};
1010
use crate::commands;
1111
use crate::installable::Installable;
1212

13-
/// Resolves an Installable from an environment variable, or falls back to the provided one.
14-
pub fn resolve_env_installable(var: &str, fallback: Installable) -> Installable {
15-
if let Ok(val) = env::var(var) {
13+
/// Resolves an Installable from an environment variable.
14+
///
15+
/// Returns `Some(Installable)` if the environment variable is set and can be parsed,
16+
/// or `None` if the environment variable is not set.
17+
pub fn resolve_env_installable(var: &str) -> Option<Installable> {
18+
env::var(var).ok().map(|val| {
1619
let mut elems = val.splitn(2, '#');
1720
let reference = elems.next().unwrap().to_owned();
1821
let attribute = elems
@@ -23,9 +26,7 @@ pub fn resolve_env_installable(var: &str, fallback: Installable) -> Installable
2326
reference,
2427
attribute,
2528
}
26-
} else {
27-
fallback
28-
}
29+
})
2930
}
3031

3132
/// Extends an Installable with the appropriate attribute path for a platform.

0 commit comments

Comments
 (0)