Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 3 additions & 33 deletions src/darwin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use crate::installable::Installable;
use crate::interface::{DarwinArgs, DarwinRebuildArgs, DarwinReplArgs, DarwinSubcommand};
use crate::nixos::toplevel_for;
use crate::update::update;
use crate::util::get_hostname;
use crate::Result;

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

fn get_hostname(hostname: Option<String>) -> Result<String> {
match &hostname {
Some(h) => Ok(h.to_owned()),
None => {
#[cfg(not(target_os = "macos"))]
{
Ok(hostname::get()
.context("Failed to get hostname")?
.to_str()
.unwrap()
.to_string())
}
#[cfg(target_os = "macos")]
{
use system_configuration::{
core_foundation::{base::TCFType, string::CFString},
sys::dynamic_store_copy_specific::SCDynamicStoreCopyLocalHostName,
};

let ptr = unsafe { SCDynamicStoreCopyLocalHostName(std::ptr::null()) };
if ptr.is_null() {
bail!("Failed to get hostname");
}
let name = unsafe { CFString::wrap_under_get_rule(ptr) };

Ok(name.to_string())
}
}
}
}

impl DarwinRebuildArgs {
fn rebuild(self, variant: DarwinRebuildVariant) -> Result<()> {
use DarwinRebuildVariant::*;
Expand All @@ -76,7 +46,7 @@ impl DarwinRebuildArgs {
update(&self.common.installable, self.update_args.update_input)?;
}

let hostname = get_hostname(self.hostname)?;
let hostname = self.hostname.ok_or(()).or_else(|()| get_hostname())?;

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

let hostname = get_hostname(self.hostname)?;
let hostname = self.hostname.ok_or(()).or_else(|()| get_hostname())?;

if let Installable::Flake {
ref mut attribute, ..
Expand Down
7 changes: 2 additions & 5 deletions src/home.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use crate::commands::Command;
use crate::installable::Installable;
use crate::interface::{self, HomeRebuildArgs, HomeReplArgs, HomeSubcommand};
use crate::update::update;
use crate::util::get_hostname;

impl interface::HomeArgs {
pub fn run(self) -> Result<()> {
Expand Down Expand Up @@ -170,11 +171,7 @@ where

// check for <user> and <user@hostname>
let username = std::env::var("USER").expect("Couldn't get username");
let hostname = hostname::get()
.expect("Couldn't get hostname")
.to_str()
.unwrap()
.to_string();
let hostname = get_hostname()?;

let mut tried = vec![];

Expand Down
14 changes: 3 additions & 11 deletions src/nixos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use crate::installable::Installable;
use crate::interface::OsSubcommand::{self};
use crate::interface::{self, OsGenerationsArgs, OsRebuildArgs, OsReplArgs};
use crate::update::update;
use crate::util::get_hostname;

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

let hostname = match &self.hostname {
Some(h) => h.to_owned(),
None => hostname::get()
.context("Failed to get hostname")?
.to_str()
.unwrap()
.to_owned(),
};
let hostname = self.hostname.ok_or(()).or_else(|()| get_hostname())?;

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

let hostname = self
.hostname
.unwrap_or_else(|| hostname::get().unwrap().to_str().unwrap().to_string());
let hostname = self.hostname.ok_or(()).or_else(|()| get_hostname())?;

if let Installable::Flake {
ref mut attribute, ..
Expand Down
28 changes: 28 additions & 0 deletions src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,31 @@ impl MaybeTempPath for (PathBuf, TempDir) {
self.0.as_ref()
}
}

pub fn get_hostname() -> Result<String> {
#[cfg(not(target_os = "macos"))]
{
use color_eyre::eyre::Context;
Ok(hostname::get()
.context("Failed to get hostname")?
.to_str()
.unwrap()
.to_string())
}
#[cfg(target_os = "macos")]
{
use color_eyre::eyre::bail;
use system_configuration::{
core_foundation::{base::TCFType, string::CFString},
sys::dynamic_store_copy_specific::SCDynamicStoreCopyLocalHostName,
};

let ptr = unsafe { SCDynamicStoreCopyLocalHostName(std::ptr::null()) };
if ptr.is_null() {
bail!("Failed to get hostname");
}
let name = unsafe { CFString::wrap_under_get_rule(ptr) };

Ok(name.to_string())
}
}