Skip to content

Commit 4af62e6

Browse files
committed
fix: get hostname consistently
Fixes #242
1 parent 7bd0d00 commit 4af62e6

File tree

4 files changed

+36
-49
lines changed

4 files changed

+36
-49
lines changed

src/darwin.rs

Lines changed: 3 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use crate::installable::Installable;
77
use crate::interface::{DarwinArgs, DarwinRebuildArgs, DarwinReplArgs, DarwinSubcommand};
88
use crate::nixos::toplevel_for;
99
use crate::update::update;
10+
use crate::util::get_hostname;
1011
use crate::Result;
1112

1213
const SYSTEM_PROFILE: &str = "/nix/var/nix/profiles/system";
@@ -33,37 +34,6 @@ enum DarwinRebuildVariant {
3334
Build,
3435
}
3536

36-
fn get_hostname(hostname: Option<String>) -> Result<String> {
37-
match &hostname {
38-
Some(h) => Ok(h.to_owned()),
39-
None => {
40-
#[cfg(not(target_os = "macos"))]
41-
{
42-
Ok(hostname::get()
43-
.context("Failed to get hostname")?
44-
.to_str()
45-
.unwrap()
46-
.to_string())
47-
}
48-
#[cfg(target_os = "macos")]
49-
{
50-
use system_configuration::{
51-
core_foundation::{base::TCFType, string::CFString},
52-
sys::dynamic_store_copy_specific::SCDynamicStoreCopyLocalHostName,
53-
};
54-
55-
let ptr = unsafe { SCDynamicStoreCopyLocalHostName(std::ptr::null()) };
56-
if ptr.is_null() {
57-
bail!("Failed to get hostname");
58-
}
59-
let name = unsafe { CFString::wrap_under_get_rule(ptr) };
60-
61-
Ok(name.to_string())
62-
}
63-
}
64-
}
65-
}
66-
6737
impl DarwinRebuildArgs {
6838
fn rebuild(self, variant: DarwinRebuildVariant) -> Result<()> {
6939
use DarwinRebuildVariant::*;
@@ -76,7 +46,7 @@ impl DarwinRebuildArgs {
7646
update(&self.common.installable, self.update_args.update_input)?;
7747
}
7848

79-
let hostname = get_hostname(self.hostname)?;
49+
let hostname = self.hostname.ok_or(()).or_else(|()| get_hostname())?;
8050

8151
let out_path: Box<dyn crate::util::MaybeTempPath> = match self.common.out_link {
8252
Some(ref p) => Box::new(p.clone()),
@@ -170,7 +140,7 @@ impl DarwinReplArgs {
170140
bail!("Nix doesn't support nix store installables.");
171141
}
172142

173-
let hostname = get_hostname(self.hostname)?;
143+
let hostname = self.hostname.ok_or(()).or_else(|()| get_hostname())?;
174144

175145
if let Installable::Flake {
176146
ref mut attribute, ..

src/home.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use crate::commands::Command;
1010
use crate::installable::Installable;
1111
use crate::interface::{self, HomeRebuildArgs, HomeReplArgs, HomeSubcommand};
1212
use crate::update::update;
13+
use crate::util::get_hostname;
1314

1415
impl interface::HomeArgs {
1516
pub fn run(self) -> Result<()> {
@@ -170,11 +171,7 @@ where
170171

171172
// check for <user> and <user@hostname>
172173
let username = std::env::var("USER").expect("Couldn't get username");
173-
let hostname = hostname::get()
174-
.expect("Couldn't get hostname")
175-
.to_str()
176-
.unwrap()
177-
.to_string();
174+
let hostname = get_hostname()?;
178175

179176
let mut tried = vec![];
180177

src/nixos.rs

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use crate::installable::Installable;
1212
use crate::interface::OsSubcommand::{self};
1313
use crate::interface::{self, OsGenerationsArgs, OsRebuildArgs, OsReplArgs};
1414
use crate::update::update;
15+
use crate::util::get_hostname;
1516

1617
const SYSTEM_PROFILE: &str = "/nix/var/nix/profiles/system";
1718
const CURRENT_PROFILE: &str = "/run/current-system";
@@ -63,14 +64,7 @@ impl OsRebuildArgs {
6364
update(&self.common.installable, self.update_args.update_input)?;
6465
}
6566

66-
let hostname = match &self.hostname {
67-
Some(h) => h.to_owned(),
68-
None => hostname::get()
69-
.context("Failed to get hostname")?
70-
.to_str()
71-
.unwrap()
72-
.to_owned(),
73-
};
67+
let hostname = self.hostname.ok_or(()).or_else(|()| get_hostname())?;
7468

7569
let out_path: Box<dyn crate::util::MaybeTempPath> = match self.common.out_link {
7670
Some(ref p) => Box::new(p.clone()),
@@ -216,9 +210,7 @@ impl OsReplArgs {
216210
bail!("Nix doesn't support nix store installables.");
217211
}
218212

219-
let hostname = self
220-
.hostname
221-
.unwrap_or_else(|| hostname::get().unwrap().to_str().unwrap().to_string());
213+
let hostname = self.hostname.ok_or(()).or_else(|()| get_hostname())?;
222214

223215
if let Installable::Flake {
224216
ref mut attribute, ..

src/util.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,3 +74,31 @@ impl MaybeTempPath for (PathBuf, TempDir) {
7474
self.0.as_ref()
7575
}
7676
}
77+
78+
pub fn get_hostname() -> Result<String> {
79+
#[cfg(not(target_os = "macos"))]
80+
{
81+
use color_eyre::eyre::Context;
82+
Ok(hostname::get()
83+
.context("Failed to get hostname")?
84+
.to_str()
85+
.unwrap()
86+
.to_string())
87+
}
88+
#[cfg(target_os = "macos")]
89+
{
90+
use color_eyre::eyre::bail;
91+
use system_configuration::{
92+
core_foundation::{base::TCFType, string::CFString},
93+
sys::dynamic_store_copy_specific::SCDynamicStoreCopyLocalHostName,
94+
};
95+
96+
let ptr = unsafe { SCDynamicStoreCopyLocalHostName(std::ptr::null()) };
97+
if ptr.is_null() {
98+
bail!("Failed to get hostname");
99+
}
100+
let name = unsafe { CFString::wrap_under_get_rule(ptr) };
101+
102+
Ok(name.to_string())
103+
}
104+
}

0 commit comments

Comments
 (0)