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 OS detection in a docker container #13172

Merged
merged 7 commits into from
Apr 22, 2020
Merged
Show file tree
Hide file tree
Changes from 4 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
71 changes: 41 additions & 30 deletions lib/pure/distros.nim
Original file line number Diff line number Diff line change
Expand Up @@ -134,18 +134,24 @@ const
LacksDevPackages* = {Distribution.Gentoo, Distribution.Slackware,
Distribution.ArchLinux}

# we cache the result of the 'uname -a'
# we cache the result of the 'cmdRelease'
# execution for faster platform detections.
var unameRes, releaseRes, hostnamectlRes: string
var unameRes, osReleaseIDRes, releaseRes, hostnamectlRes: string

template unameRelease(cmd, cache): untyped =
template cmdRelease(cmd, cache): untyped =
if cache.len == 0:
cache = (when defined(nimscript): gorge(cmd) else: execProcess(cmd))
cache

template uname(): untyped = unameRelease("uname -a", unameRes)
template release(): untyped = unameRelease("lsb_release -d", releaseRes)
template hostnamectl(): untyped = unameRelease("hostnamectl", hostnamectlRes)
template uname(): untyped = cmdRelease("uname -a", unameRes)
template osReleaseID(): untyped = cmdRelease("cat /etc/os-release | grep ^ID=", osReleaseIDRes)
template release(): untyped = cmdRelease("lsb_release -d", releaseRes)
template hostnamectl(): untyped = cmdRelease("hostnamectl", hostnamectlRes)

proc detectOsWithAllCmd(d: Distribution): bool =
let dd = toLowerAscii($d)
result = dd in toLowerAscii(osReleaseID()) or dd in toLowerAscii(release()) or
dd in toLowerAscii(uname()) or ("operating system: " & dd) in toLowerAscii(hostnamectl())

proc detectOsImpl(d: Distribution): bool =
case d
Expand All @@ -154,32 +160,37 @@ proc detectOsImpl(d: Distribution): bool =
of Distribution.Posix: result = defined(posix)
of Distribution.MacOSX: result = defined(macosx)
of Distribution.Linux: result = defined(linux)
of Distribution.Ubuntu, Distribution.Gentoo, Distribution.FreeBSD,
Distribution.OpenBSD:
result = ("-" & $d & " ") in uname()
of Distribution.RedHat:
result = "Red Hat" in uname()
of Distribution.BSD: result = defined(bsd)
of Distribution.ArchLinux:
result = "arch" in toLowerAscii(uname())
of Distribution.NixOS:
result = existsEnv("NIX_BUILD_TOP") or existsEnv("__NIXOS_SET_ENVIRONMENT_DONE")
# Check if this is a Nix build or NixOS environment
of Distribution.OpenSUSE:
result = "suse" in toLowerAscii(uname()) or "suse" in toLowerAscii(release())
of Distribution.GoboLinux:
result = "-Gobo " in uname()
of Distribution.OpenMandriva:
result = "mandriva" in toLowerAscii(uname())
of Distribution.Solaris:
let uname = toLowerAscii(uname())
result = ("sun" in uname) or ("solaris" in uname)
of Distribution.Haiku:
result = defined(haiku)
else:
let dd = toLowerAscii($d)
result = dd in toLowerAscii(uname()) or dd in toLowerAscii(release()) or
("operating system: " & dd) in toLowerAscii(hostnamectl())
when defined(linux):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Huh? That cannot be correct for Haiku and the BSDs...

case d
of Distribution.Gentoo, Distribution.FreeBSD,
Distribution.OpenBSD:
result = ("-" & $d & " ") in uname()
of Distribution.Elementary, Distribution.Ubuntu, Distribution.Debian, Distribution.Fedora,
Distribution.OpenMandriva, Distribution.CentOS, Distribution.Alpine,
Distribution.Mageia, Distribution.Zorin:
result = toLowerAscii($d) in osReleaseID()
of Distribution.RedHat:
result = "rhel" in osReleaseID()
of Distribution.ArchLinux:
result = "arch" in osReleaseID()
of Distribution.NixOS:
result = existsEnv("NIX_BUILD_TOP") or existsEnv("__NIXOS_SET_ENVIRONMENT_DONE")
# Check if this is a Nix build or NixOS environment
of Distribution.OpenSUSE:
result = "suse" in toLowerAscii(uname()) or "suse" in toLowerAscii(release())
of Distribution.GoboLinux:
result = "-Gobo " in uname()
of Distribution.Solaris:
let uname = toLowerAscii(uname())
result = ("sun" in uname) or ("solaris" in uname)
of Distribution.Haiku:
result = defined(haiku)
else:
result = detectOsWithAllCmd(d)
else:
result = false

template detectOs*(d: untyped): bool =
## Distro/OS detection. For convenience the
Expand Down
21 changes: 21 additions & 0 deletions tests/distros/tdistros_detect.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import distros

discard """
exitcode: 0
output: ""
"""

when defined(windows):
doAssert detectOs(Windows) == true
doAssert detectOs(Ubuntu) == false
doAssert detectOs(MacOSX) == false

when defined(linux):
doAssert detectOs(Ubuntu) == true
doAssert detectOs(Windows) == false
doAssert detectOs(MacOSX) == false

when defined(macosx):
doAssert detectOs(MacOSX) == true
doAssert detectOs(Windows) == false
doAssert detectOs(Ubuntu) == false