Merge pull request #9366 from NixOS/backport-9187-to-2.18-maintenance

[2.18-maintenance] Backport #9187 apple-virt
This commit is contained in:
Robert Hensing 2023-11-16 19:44:32 +01:00 committed by GitHub
commit 8e72a529da
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 32 additions and 0 deletions

View file

@ -24,6 +24,9 @@
#include "config-impl.hh" #include "config-impl.hh"
#ifdef __APPLE__
#include <sys/sysctl.h>
#endif
namespace nix { namespace nix {
@ -154,6 +157,29 @@ unsigned int Settings::getDefaultCores()
return concurrency; return concurrency;
} }
#if __APPLE__
static bool hasVirt() {
int hasVMM;
int hvSupport;
size_t size;
size = sizeof(hasVMM);
if (sysctlbyname("kern.hv_vmm_present", &hasVMM, &size, NULL, 0) == 0) {
if (hasVMM)
return false;
}
// whether the kernel and hardware supports virt
size = sizeof(hvSupport);
if (sysctlbyname("kern.hv_support", &hvSupport, &size, NULL, 0) == 0) {
return hvSupport == 1;
} else {
return false;
}
}
#endif
StringSet Settings::getDefaultSystemFeatures() StringSet Settings::getDefaultSystemFeatures()
{ {
/* For backwards compatibility, accept some "features" that are /* For backwards compatibility, accept some "features" that are
@ -170,6 +196,11 @@ StringSet Settings::getDefaultSystemFeatures()
features.insert("kvm"); features.insert("kvm");
#endif #endif
#if __APPLE__
if (hasVirt())
features.insert("apple-virt");
#endif
return features; return features;
} }

View file

@ -708,6 +708,7 @@ public:
`kvm` feature. `kvm` feature.
This setting by default includes `kvm` if `/dev/kvm` is accessible, This setting by default includes `kvm` if `/dev/kvm` is accessible,
`apple-virt` if hardware virtualization is available on macOS,
and the pseudo-features `nixos-test`, `benchmark` and `big-parallel` and the pseudo-features `nixos-test`, `benchmark` and `big-parallel`
that are used in Nixpkgs to route builds to specific machines. that are used in Nixpkgs to route builds to specific machines.
)", {}, false}; )", {}, false};