use std::path::{Path, PathBuf};
use std::process::Command;
use anyhow::{Context, Result};
pub fn find_repo() -> Result<PathBuf> {
if let Ok(cwd) = std::env::current_dir() {
let mut dir = cwd.as_path();
loop {
let flake = dir.join("flake.nix");
if flake.exists() && is_darwin_flake(&flake) {
return Ok(dir.to_path_buf());
}
match dir.parent() {
Some(parent) => dir = parent,
None => break,
}
}
}
if let Some(home) = dirs::home_dir() {
let candidates = [
home.join("workspace/black-meridian/styrene-lab/macos-nix"),
home.join("macos-nix"),
home.join(".config/nix-darwin"),
];
for path in &candidates {
let flake = path.join("flake.nix");
if flake.exists() && is_darwin_flake(&flake) {
return Ok(path.clone());
}
}
}
anyhow::bail!("could not find nix-darwin repo")
}
fn is_darwin_flake(path: &Path) -> bool {
std::fs::read_to_string(path)
.map(|content| content.contains("darwinConfigurations"))
.unwrap_or(false)
}
pub fn hostname() -> Result<String> {
let output = Command::new("scutil")
.args(["--get", "LocalHostName"])
.output()
.context("failed to run scutil --get LocalHostName")?;
if !output.status.success() {
anyhow::bail!("scutil --get LocalHostName failed");
}
let name = String::from_utf8(output.stdout)
.context("hostname is not valid UTF-8")?
.trim()
.to_string();
Ok(name)
}