provisioning/scripts/libs/os/detect.nu

86 lines
2.1 KiB
Text
Raw Normal View History

#!/usr/bin/env nu
# OS detection utilities
# Detect running OS and return structured results
use std
# Detect current OS type
export def detect [] {
let os_info = (^uname -s | str trim)
if $os_info == "Linux" {
# Check for NixOS (most distinctive)
if ("/etc/os-release" | path exists) {
let release_info = (open /etc/os-release)
if ($release_info | str contains "nixos" or $release_info | str contains "NixOS") {
"nixos"
} else if ($release_info | str contains "Ubuntu") {
"ubuntu"
} else if ($release_info | str contains "Debian") {
"debian"
} else if ($release_info | str contains "Alpine") {
"alpine"
} else {
"linux-generic"
}
} else {
"linux-generic"
}
} else if $os_info == "Darwin" {
"macos"
} else if ($os_info | str starts-with "MINGW") {
"windows"
} else {
$"unknown($os_info)"
}
}
# Detect CPU architecture
export def detect-arch [] {
let arch_raw = (^uname -m | str trim)
match $arch_raw {
"x86_64" => { "x86_64" }
"aarch64" | "arm64" => { "aarch64" }
"armv7l" | "armv7" => { "armv7" }
"i386" | "i686" => { "i386" }
_ => { $arch_raw }
}
}
# Get system information as record
export def info [] {
{
os: (detect),
arch: (detect-arch),
kernel: (^uname -s | str trim),
release: (^uname -r | str trim),
}
}
# Validate OS type is supported
export def is-supported [os_type: string] {
$os_type in ["debian" "ubuntu" "nixos" "alpine" "linux-generic" "macos"]
}
# Check if running on NixOS
export def is-nixos [] {
(detect) == "nixos"
}
# Check if running on Debian-based
export def is-debian-based [] {
let current = (detect)
$current in ["debian" "ubuntu"]
}
# Main: Print OS info
export def main [] {
let system_info = (info)
print $"OS: ($system_info.os)"
print $"Architecture: ($system_info.arch)"
print $"Kernel: ($system_info.kernel)"
print $"Release: ($system_info.release)"
}