import distros
for distro in Distribution:
if detectOs(distro): echo distro
Error: undeclared field: 'distro' found 'distro' of kind 'forVar'
Module distros source shows a Distribution.d enum is expected:
template detectOs*(d: untyped): bool =
## Distro/OS detection. For convenience the
## required ``Distribution.`` qualifier is added to the
## enum value.
detectOsImpl(Distribution.d)
Exporting detectOsImpl would allow:
for distro in Distribution:
if detectOsImpl(distro): echo distro
Any suggestions?
This is, unfortunately, not possible at the moment with the way the template is written. You can work around this, however, with the following macro:
import macros
import distros
macro findOS*(): Distribution =
var body = newNimNode(nnkStmtList)
# Mac is later in the enum than Posix, and
# Mac is technically posix, so this is needed
# to properly detect a Mac
body.add(
quote do:
if detectOS(MacOSX):
return Distribution.MacOSX
)
for distro in Distribution:
let distLit = ident($distro)
body.add(
quote do:
if detectOS(`distLit`):
return Distribution.`distLit`
)
result = quote do:
(proc (): Distribution = `body`)()
echo findOS()
Actually, the above won't work due to the general nature of Posix and Linux. This may be better:
import macros
import distros
macro findOS*(): Distribution =
var body = newNimNode(nnkStmtList)
for distro in Distribution:
if distro in {Linux, Posix}:
continue
let distLit = ident($distro)
body.add(
quote do:
if detectOS(`distLit`):
return Distribution.`distLit`
)
# We want to check these last because they are more general
body.add(
quote do:
if detectOS(Linux):
return Distribution.Linux
if detectOS(Posix):
return Distribution.Posix
)
result = quote do:
(proc (): Distribution = `body`)()
echo findOS()
But it's actually a tricky problem that might not be able to be solved with an iteration of Distribution, which is probably why it's not in the stdlib.
Thanks for the workaround.
As an aside, it turns out
for distro in Distribution:
if detectOsImpl(distro): echo distro
outputs two strings:
Posix
Linux
The macro ouputs the first.
What is the benefit of only accessing detectOsImpl through a template?
But it's actually a tricky problem that might not be able to be solved with an iteration of Distribution, which is probably why it's not in the stdlib.
distros is for Nimble support, for nothing else. Detecting the distro is fundamentally flawed anyway, test for features, not for distros or versions.
Second macro version outputs
Ubuntu
which is the correct answer!