From 1f66e90db1a9d6ca3472215afbb8fa96efac3b7a Mon Sep 17 00:00:00 2001 From: Qubasa Date: Mon, 5 May 2025 22:19:17 +0200 Subject: [PATCH] clan-cli: fix bubblewrap not finding bash when IN_NIX_SANDBOX=1 if prev environment doesn't have it in PATH --- pkgs/clan-cli/clan_cli/bwrap/__init__.py | 22 ++++++++++++++-------- pkgs/clan-cli/clan_cli/vars/generate.py | 10 ++++++++-- 2 files changed, 22 insertions(+), 10 deletions(-) diff --git a/pkgs/clan-cli/clan_cli/bwrap/__init__.py b/pkgs/clan-cli/clan_cli/bwrap/__init__.py index 4d855e767..430b993a1 100644 --- a/pkgs/clan-cli/clan_cli/bwrap/__init__.py +++ b/pkgs/clan-cli/clan_cli/bwrap/__init__.py @@ -1,4 +1,8 @@ -from clan_cli.cmd import run +import os +import shutil +from pathlib import Path + +from clan_cli.cmd import Log, RunOpts, run from clan_cli.nix import nix_shell _works: bool | None = None @@ -12,6 +16,11 @@ def bubblewrap_works() -> bool: def _bubblewrap_works() -> bool: + real_bash_path = Path("bash") + if os.environ.get("IN_NIX_SANDBOX"): + bash_executable_path = Path(str(shutil.which("bash"))) + real_bash_path = bash_executable_path.resolve() + # fmt: off cmd = nix_shell( [ @@ -30,13 +39,10 @@ def _bubblewrap_works() -> bool: "--gid", "1000", "--", # do nothing, just test if bash executes - "bash", "-c", ":" + str(real_bash_path), "-c", ":" ], ) + # fmt: on - try: - run(cmd) - except Exception: - return False - else: - return True + res = run(cmd, RunOpts(log=Log.BOTH, check=False)) + return res.returncode == 0 diff --git a/pkgs/clan-cli/clan_cli/vars/generate.py b/pkgs/clan-cli/clan_cli/vars/generate.py index 1a9902860..cadbdc4f7 100644 --- a/pkgs/clan-cli/clan_cli/vars/generate.py +++ b/pkgs/clan-cli/clan_cli/vars/generate.py @@ -1,6 +1,7 @@ import argparse import logging import os +import shutil import sys from dataclasses import dataclass, field from functools import cached_property @@ -86,6 +87,11 @@ class Generator: def bubblewrap_cmd(generator: str, tmpdir: Path) -> list[str]: test_store = nix_test_store() + real_bash_path = Path("bash") + if os.environ.get("IN_NIX_SANDBOX"): + bash_executable_path = Path(str(shutil.which("bash"))) + real_bash_path = bash_executable_path.resolve() + # fmt: off return nix_shell( [ @@ -109,8 +115,8 @@ def bubblewrap_cmd(generator: str, tmpdir: Path) -> list[str]: "--uid", "1000", "--gid", "1000", "--", - "bash", "-c", generator - ], + str(real_bash_path), "-c", generator + ] ) # fmt: on