Skip to content

Commit

Permalink
ensure XDG_RUNTIME_DIR before calling nix-prefetch
Browse files Browse the repository at this point in the history
  • Loading branch information
Mic92 committed Jan 9, 2021
1 parent 05dccf1 commit 9e09664
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
14 changes: 12 additions & 2 deletions nix_update/update.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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()


Expand Down
9 changes: 7 additions & 2 deletions nix_update/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)))
Expand All @@ -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]:
Expand Down

0 comments on commit 9e09664

Please sign in to comment.