Fix compatibility with nix-2.3 not knowing --extra-experimental-features.

This commit is contained in:
Bryan Gardiner 2023-05-22 11:40:59 -07:00
parent 38736d6392
commit 35bd54cb63
No known key found for this signature in database
GPG key ID: 53EFBCA063E6183C
2 changed files with 41 additions and 7 deletions

View file

@ -2,6 +2,9 @@
## 0.2.3 (unreleased) ## 0.2.3 (unreleased)
- Fix compatibility with nix-2.3 where `nix --extra-experimental-features` isn't
a known flag yet. We have to switch on the version of Nix we've been given.
## 0.2.2 (2023-05-22) ## 0.2.2 (2023-05-22)
- Fixed crash when `nix-store --query --references` returns nothing (e.g. for a - Fixed crash when `nix-store --query --references` returns nothing (e.g. for a

41
src/nvd
View file

@ -53,6 +53,7 @@ signal(SIGPIPE, SIG_DFL) # Python handles SIGPIPE improperly by default.
NVD_VERSION = "0.2.3" NVD_VERSION = "0.2.3"
NIX_BIN_DIR = None NIX_BIN_DIR = None
USE_NIX_COMMAND_FEATURE = False # Whether to explicitly enable the nix-command feature.
USE_COLOUR = False USE_COLOUR = False
SGR_RESET = 0 SGR_RESET = 0
@ -440,11 +441,9 @@ def query_closure_disk_usage_bytes(target: Path) -> Optional[int]:
try: try:
stdout = subprocess.run( stdout = subprocess.run(
[ [make_nix_bin_path("nix")]
make_nix_bin_path("nix"), "path-info", + make_extra_experimental_features_args()
"--extra-experimental-features", "nix-command", + ["path-info", "--closure-size", target_str],
"--closure-size", target_str,
],
stdout=PIPE, stdout=PIPE,
text=True text=True
).stdout ).stdout
@ -491,6 +490,32 @@ def make_nix_bin_path(exe_name: str) -> str:
else: else:
return exe_name return exe_name
def make_extra_experimental_features_args() -> List[str]:
global USE_NIX_COMMAND_FEATURE
if USE_NIX_COMMAND_FEATURE:
return ["--extra-experimental-features", "nix-command"]
else:
return []
def query_nix_version() -> Version:
output = subprocess.run(
[make_nix_bin_path("nix-build"), "--version"],
stdout=PIPE,
text=True,
check=True,
).stdout
first_line = output.split("\n", 1)[0]
words = first_line.split(" ")
regex = re.compile(r"^[0-9]+\.[0-9]")
for word in words:
if regex.search(word):
return Version(word)
raise RuntimeError(
"Could not determine Nix version from 'nix-store --version' output: {output!r}")
def run_list(*, root, only_selected, name_patterns): def run_list(*, root, only_selected, name_patterns):
path = Path(root) path = Path(root)
@ -722,6 +747,7 @@ def parse_args():
def main(): def main():
global NIX_BIN_DIR global NIX_BIN_DIR
global USE_NIX_COMMAND_FEATURE
global USE_COLOUR global USE_COLOUR
global INST_ADDED global INST_ADDED
global INST_REMOVED global INST_REMOVED
@ -748,6 +774,11 @@ def main():
print("nvd: Subcommand required, see 'nvd --help'.") print("nvd: Subcommand required, see 'nvd --help'.")
sys.exit(1) sys.exit(1)
# Nix 2.4 and later need "experimental-features: nix-command" in order to
# use the "nix ..." style commands.
if Version("2.4") < query_nix_version():
USE_NIX_COMMAND_FEATURE = True
{ {
"list": run_list, "list": run_list,
"diff": run_diff, "diff": run_diff,