Skip to content

Commit 3f4f0bc

Browse files
committed
{home,nixos}: make nh commands aware of NH_OS_FLAKE and NH_HOME_FLAKE variables
1 parent 3262f3c commit 3f4f0bc

File tree

3 files changed

+68
-6
lines changed

3 files changed

+68
-6
lines changed

src/home.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,14 @@ where
162162
ref reference,
163163
ref mut attribute,
164164
} => 'flake: {
165-
// If user explicitely selects some other attribute, don't push homeConfigurations
165+
// Check if we're using NH_HOME_FLAKE to give it precedence
166+
if env::var("NH_HOME_FLAKE").is_ok()
167+
&& env::var("NH_HOME_FLAKE").unwrap() == reference.to_string()
168+
{
169+
debug!("Using NH_HOME_FLAKE: {}", reference);
170+
}
171+
172+
// If user explicitly selects some other attribute, don't push homeConfigurations
166173
if !attribute.is_empty() {
167174
break 'flake;
168175
}

src/installable.rs

Lines changed: 49 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,32 @@ impl FromArgMatches for Installable {
7070
});
7171
}
7272

73-
// env var fallacks
73+
// env var fallbacks
74+
75+
// Check for command-specific flake env vars first
76+
if let Ok(subcommand) = env::var("NH_CURRENT_COMMAND") {
77+
if subcommand == "os" {
78+
if let Ok(f) = env::var("NH_OS_FLAKE") {
79+
let mut elems = f.splitn(2, '#');
80+
return Ok(Self::Flake {
81+
reference: elems.next().unwrap().to_owned(),
82+
attribute: parse_attribute(
83+
elems.next().map(|s| s.to_string()).unwrap_or_default(),
84+
),
85+
});
86+
}
87+
} else if subcommand == "home" {
88+
if let Ok(f) = env::var("NH_HOME_FLAKE") {
89+
let mut elems = f.splitn(2, '#');
90+
return Ok(Self::Flake {
91+
reference: elems.next().unwrap().to_owned(),
92+
attribute: parse_attribute(
93+
elems.next().map(|s| s.to_string()).unwrap_or_default(),
94+
),
95+
});
96+
}
97+
}
98+
}
7499

75100
if let Ok(f) = env::var("NH_FLAKE") {
76101
let mut elems = f.splitn(2, '#');
@@ -80,10 +105,26 @@ impl FromArgMatches for Installable {
80105
});
81106
}
82107

108+
if let Ok(f) = env::var("NH_OS_FLAKE") {
109+
let mut elems = f.splitn(2, '#');
110+
return Ok(Self::Flake {
111+
reference: elems.next().unwrap().to_owned(),
112+
attribute: parse_attribute(elems.next().map(|s| s.to_string()).unwrap_or_default()),
113+
});
114+
}
115+
116+
if let Ok(f) = env::var("NH_HOME_FLAKE") {
117+
let mut elems = f.splitn(2, '#');
118+
return Ok(Self::Flake {
119+
reference: elems.next().unwrap().to_owned(),
120+
attribute: parse_attribute(elems.next().map(|s| s.to_string()).unwrap_or_default()),
121+
});
122+
}
123+
83124
if let Ok(f) = env::var("NH_FILE") {
84125
return Ok(Self::File {
85126
path: PathBuf::from(f),
86-
attribute: parse_attribute(env::var("NH_ATTR").unwrap_or_default()),
127+
attribute: parse_attribute(env::var("NH_ATTRP").unwrap_or_default()),
87128
});
88129
}
89130

@@ -124,11 +165,13 @@ Nix accepts various kinds of installables:
124165
[FLAKEREF[#ATTRPATH]]
125166
Flake reference with an optional attribute path.
126167
[env: NH_FLAKE={}]
168+
[env: NH_OS_FLAKE={}]
169+
[env: NH_HOME_FLAKE={}]
127170
128171
{}, {} <FILE> [ATTRPATH]
129172
Path to file with an optional attribute path.
130173
[env: NH_FILE={}]
131-
[env: NH_ATTR={}]
174+
[env: NH_ATTRP={}]
132175
133176
{}, {} <EXPR> [ATTRPATH]
134177
Nix expression with an optional attribute path.
@@ -137,10 +180,12 @@ Nix accepts various kinds of installables:
137180
Path or symlink to a /nix/store path
138181
"#,
139182
env::var("NH_FLAKE").unwrap_or_default(),
183+
env::var("NH_OS_FLAKE").unwrap_or_default(),
184+
env::var("NH_HOME_FLAKE").unwrap_or_default(),
140185
"-f".yellow(),
141186
"--file".yellow(),
142187
env::var("NH_FILE").unwrap_or_default(),
143-
env::var("NH_ATTRP").unwrap_or_default(),
188+
env::var("NH_ATTR").unwrap_or_default(),
144189
"-e".yellow(),
145190
"--expr".yellow(),
146191
)),

src/nixos.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use std::env;
12
use std::fs;
23
use std::path::{Path, PathBuf};
34

@@ -177,8 +178,17 @@ pub fn toplevel_for<S: AsRef<str>>(hostname: S, installable: Installable) -> Ins
177178

178179
match res {
179180
Installable::Flake {
180-
ref mut attribute, ..
181+
ref reference,
182+
ref mut attribute,
183+
..
181184
} => {
185+
// Check if using NH_OS_FLAKE
186+
if let Ok(os_flake) = env::var("NH_OS_FLAKE") {
187+
if os_flake == *reference {
188+
debug!("Using NH_OS_FLAKE: {}", reference);
189+
}
190+
}
191+
182192
// If user explicitly selects some other attribute, don't push nixosConfigurations
183193
if attribute.is_empty() {
184194
attribute.push(String::from("nixosConfigurations"));

0 commit comments

Comments
 (0)