vars+facts: use bwrap only if supported

This commit is contained in:
DavHau
2025-03-09 13:52:15 +07:00
parent d266e749a8
commit db2e2e974c
5 changed files with 65 additions and 4 deletions

View File

@@ -0,0 +1,42 @@
from clan_cli.cmd import run
from clan_cli.nix import nix_shell
_works: bool | None = None
def bubblewrap_works() -> bool:
global _works
if _works is None:
_works = _bubblewrap_works()
return _works
def _bubblewrap_works() -> bool:
# fmt: off
cmd = nix_shell(
[
"nixpkgs#bash",
"nixpkgs#bubblewrap",
],
[
"bwrap",
"--unshare-all",
"--tmpfs", "/",
"--ro-bind", "/nix/store", "/nix/store",
"--dev", "/dev",
"--chdir", "/",
"--bind", "/proc", "/proc",
"--uid", "1000",
"--gid", "1000",
"--",
# do nothing, just test if bash executes
"bash", "-c", ":"
],
)
# fmt: on
try:
run(cmd)
except Exception:
return False
else:
return True

View File

@@ -0,0 +1,16 @@
import sys
import pytest
from clan_cli.bwrap import bubblewrap_works
@pytest.mark.skipif(sys.platform != "linux", reason="bubblewrap only works on linux")
def test_bubblewrap_works_on_linux() -> None:
assert bubblewrap_works() is True
@pytest.mark.skipif(
sys.platform == "linux", reason="bubblewrap does not work on non-linux"
)
def test_bubblewrap_detection_non_linux() -> None:
assert bubblewrap_works() is False