diff --git a/nix_update/update.py b/nix_update/update.py index f4e64cd..073a2f6 100644 --- a/nix_update/update.py +++ b/nix_update/update.py @@ -1,6 +1,7 @@ import fileinput -from typing import List +from typing import List, Optional, Dict import subprocess +import tempfile from .errors import UpdateError from .eval import Package, eval_attr @@ -66,7 +67,16 @@ def replace_hash(filename: str, current: str, target: str) -> None: def nix_prefetch(cmd: List[str]) -> str: - res = run(["nix-prefetch"] + cmd) + extra_env: Dict[str, str] = {} + tempdir: Optional[tempfile.TemporaryDirectory[str]] = None + if extra_env.get("XDG_RUNTIME_DIR") is None: + tempdir = tempfile.TemporaryDirectory() + extra_env["XDG_RUNTIME_DIR"] = tempdir.name + try: + res = run(["nix-prefetch"] + cmd, extra_env=extra_env) + finally: + if tempdir: + tempdir.cleanup() return res.stdout.strip() diff --git a/nix_update/utils.py b/nix_update/utils.py index 6a74da0..4a734d3 100644 --- a/nix_update/utils.py +++ b/nix_update/utils.py @@ -3,7 +3,7 @@ import subprocess import sys from pathlib import Path -from typing import IO, Any, Callable, List, Optional, Union +from typing import IO, Any, Callable, List, Optional, Union, Dict HAS_TTY = sys.stdout.isatty() ROOT = Path(os.path.dirname(os.path.realpath(__file__))) @@ -28,9 +28,14 @@ def run( cwd: Optional[Union[Path, str]] = None, stdout: Union[None, int, IO[Any]] = subprocess.PIPE, check: bool = True, + extra_env: Dict[str, str] = {}, ) -> "subprocess.CompletedProcess[str]": info("$ " + " ".join(command)) - return subprocess.run(command, cwd=cwd, check=check, text=True, stdout=stdout) + env = os.environ.copy() + env.update(extra_env) + return subprocess.run( + command, cwd=cwd, check=check, text=True, stdout=stdout, env=env + ) def extract_version(version: str, version_regex: str) -> Optional[str]: