nix-update: make compatible with nixUnstable

This commit is contained in:
Jörg Thalheim 2020-08-16 12:27:27 +01:00
parent 1efc6f4175
commit cd588261a7
No known key found for this signature in database
GPG Key ID: 003F2096411B5F92
3 changed files with 43 additions and 20 deletions

View File

@ -7,7 +7,7 @@ from typing import NoReturn, Optional
from .eval import Package
from .options import Options
from .update import update
from .utils import run
from .utils import run, is_nix_flakes
def die(msg: str) -> NoReturn:
@ -48,14 +48,13 @@ def parse_args() -> Options:
def nix_shell(options: Options) -> None:
with tempfile.NamedTemporaryFile(mode="w") as f:
f.write(
f"""
with import {options.import_path}; mkShell {{ buildInputs = [ {options.attribute} ]; }}
"""
)
f.flush()
run(["nix-shell", f.name], stdout=None, check=False)
import_path = os.path.realpath(options.import_path)
expr = f"with import {import_path} {{}}; mkShell {{ buildInputs = [ {options.attribute} ]; }}"
with tempfile.TemporaryDirectory() as d:
path = os.path.join(d, "default.nix")
with open(path, "w") as f:
f.write(expr)
run(["nix-shell", path], stdout=None, check=False)
def git_commit(git_dir: str, attribute: str, package: Package) -> None:
@ -115,24 +114,26 @@ def validate_git_dir(import_path: str) -> str:
def nix_run(options: Options) -> None:
if is_nix_flakes():
cmd = ["nix", "shell", "--experimental-features", "nix-command"]
else:
cmd = ["nix", "run"]
run(
["nix", "run", "-f", options.import_path, options.attribute],
stdout=None,
check=False,
cmd + ["-f", options.import_path, options.attribute], stdout=None, check=False,
)
def nix_build(options: Options) -> None:
cmd = ["nix", "build"]
if is_nix_flakes():
cmd += ["--experimental-features", "nix-command"]
run(
["nix", "build", "-f", options.import_path, options.attribute],
stdout=None,
check=False,
cmd + ["-f", options.import_path, options.attribute], stdout=None, check=False,
)
def main() -> None:
options = parse_args()
if not os.path.exists(options.import_path):
die(f"path {options.import_path} does not exists")

View File

@ -4,7 +4,7 @@ from typing import List, Optional
from .errors import UpdateError
from .options import Options
from .utils import run
from .utils import run, is_nix_flakes
@dataclass
@ -48,9 +48,21 @@ def eval_expression(import_path: str, attr: str) -> str:
def eval_attr(opts: Options) -> Package:
res = run(
["nix", "eval", "--json", eval_expression(opts.import_path, opts.attribute)]
)
expr = eval_expression(opts.import_path, opts.attribute)
if is_nix_flakes():
cmd = [
"nix",
"eval",
"--json",
"--impure",
"--experimental-features",
"nix-command",
"--expr",
expr,
]
else:
cmd = ["nix", "eval", "--json", expr]
res = run(cmd)
out = json.loads(res.stdout)
package = Package(**out)
if package.old_version == "":

View File

@ -3,6 +3,7 @@ import subprocess
import sys
from pathlib import Path
from typing import IO, Any, Callable, List, Optional, Union
from functools import lru_cache
HAS_TTY = sys.stdout.isatty()
ROOT = Path(os.path.dirname(os.path.realpath(__file__)))
@ -22,6 +23,15 @@ warn = color_text(31, file=sys.stderr)
info = color_text(32)
@lru_cache(maxsize=None)
def is_nix_flakes() -> bool:
try:
subprocess.run(["nix", "flake", "--help"], stdout=subprocess.DEVNULL)
except subprocess.CalledProcessError:
return False
return True
def run(
command: List[str],
cwd: Optional[Union[Path, str]] = None,