Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Expose AppleEmulation for *all* macOS arch #7

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "nix_rs"
# Important: remember to update the top-level Cargo.toml if updating major version
version = "0.3.3"
version = "0.4.0"
license = "Apache-2.0"
repository = "https://github.com/juspay/nix-rs"
description = "Rust library for interacting with the Nix command"
Expand Down
52 changes: 11 additions & 41 deletions src/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,59 +89,24 @@ pub enum OS {
MacOS {
/// Using https://github.com/LnL7/nix-darwin
nix_darwin: bool,
arch: MacOSArch,
arch: Option<String>,
/// https://developer.apple.com/documentation/apple-silicon/about-the-rosetta-translation-environment
proc_translated: bool,
},
/// https://nixos.org/
NixOS,
/// Nix is individually installed on Linux or macOS
Other(os_info::Type),
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum MacOSArch {
Arm64(AppleEmulation),
Other(Option<String>),
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum AppleEmulation {
None,
Rosetta,
}

impl AppleEmulation {
pub fn new() -> Self {
use is_proc_translated::is_proc_translated;
if is_proc_translated() {
AppleEmulation::Rosetta
} else {
AppleEmulation::None
}
}
}

impl Default for AppleEmulation {
fn default() -> Self {
Self::new()
}
}

impl MacOSArch {
pub fn from(os_arch: Option<&str>) -> MacOSArch {
match os_arch {
Some("arm64") => MacOSArch::Arm64(AppleEmulation::new()),
other => MacOSArch::Other(other.map(|s| s.to_string())),
}
}
}

// The [Display] instance affects how [OS] is displayed to the app user
impl Display for OS {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
OS::MacOS {
nix_darwin,
arch: _,
proc_translated: _,
} => {
if *nix_darwin {
write!(f, "nix-darwin")
Expand All @@ -159,7 +124,7 @@ impl OS {
pub async fn detect() -> Self {
let os_info = tokio::task::spawn_blocking(os_info::get).await.unwrap();
let os_type = os_info.os_type();
let arch = MacOSArch::from(os_info.architecture());
let arch = os_info.architecture();
async fn is_symlink(file_path: &str) -> std::io::Result<bool> {
let metadata = tokio::fs::symlink_metadata(file_path).await?;
Ok(metadata.file_type().is_symlink())
Expand All @@ -169,7 +134,11 @@ impl OS {
// To detect that we are on NixDarwin, we check if /etc/nix/nix.conf
// is a symlink (which nix-darwin manages like NixOS does)
let nix_darwin = is_symlink("/etc/nix/nix.conf").await.unwrap_or(false);
OS::MacOS { nix_darwin, arch }
OS::MacOS {
nix_darwin,
arch: arch.map(|s| s.to_string()),
proc_translated: is_proc_translated::is_proc_translated(),
}
}
os_info::Type::NixOS => OS::NixOS,
_ => OS::Other(os_type),
Expand All @@ -183,6 +152,7 @@ impl OS {
OS::MacOS {
nix_darwin,
arch: _,
proc_translated: _,
} if *nix_darwin => Some("nix-darwin configuration".to_string()),
OS::NixOS => Some("nixos configuration".to_string()),
_ => None,
Expand Down
Loading