Add --nix-bin-dir option for specifying Nix binaries to use (issue #9).

This commit is contained in:
Bryan Gardiner 2022-10-12 17:14:45 -07:00
parent 082958c205
commit a334e6c0ec
No known key found for this signature in database
GPG key ID: 53EFBCA063E6183C
3 changed files with 54 additions and 11 deletions

View file

@ -4,6 +4,9 @@
- Add display of the change in closure disk size (issue #8). - Add display of the change in closure disk size (issue #8).
- Add a `--nix-bin-dir` option for allowing easier control over which Nix
binaries are used (issue #9).
## 0.1.2 (2021-11-05) ## 0.1.2 (2021-11-05)
- Added a flake.nix, thanks @dadada_. - Added a flake.nix, thanks @dadada_.

42
src/nvd
View file

@ -53,6 +53,7 @@ signal(SIGPIPE, SIG_DFL) # Python handles SIGPIPE improperly by default.
NVD_VERSION = "0.1.3" NVD_VERSION = "0.1.3"
NIX_BIN_DIR = None
USE_COLOUR = False USE_COLOUR = False
SGR_RESET = 0 SGR_RESET = 0
@ -229,7 +230,7 @@ class PackageManifest:
@staticmethod @staticmethod
def parse_tree(root: Path) -> "PackageManifest": def parse_tree(root: Path) -> "PackageManifest":
direct_deps: List[str] = subprocess.run( direct_deps: List[str] = subprocess.run(
["nix-store", "--query", "--references", str(root)], [make_nix_bin_path("nix-store"), "--query", "--references", str(root)],
stdout=PIPE, stdout=PIPE,
text=True text=True
).stdout.rstrip("\n").split("\n") ).stdout.rstrip("\n").split("\n")
@ -437,7 +438,11 @@ def query_closure_disk_usage_bytes(target: Path) -> Optional[int]:
try: try:
stdout = subprocess.run( stdout = subprocess.run(
["nix", "path-info", "--extra-experimental-features", "nix-command", "--closure-size", target_str], [
make_nix_bin_path("nix"), "path-info",
"--extra-experimental-features", "nix-command",
"--closure-size", target_str,
],
stdout=PIPE, stdout=PIPE,
text=True text=True
).stdout ).stdout
@ -477,6 +482,13 @@ def render_bytes(bytes_size: int) -> str:
else: else:
return f"{scaled_size:+,.1f}{scaled_suffix}" # KiB or higher. return f"{scaled_size:+,.1f}{scaled_suffix}" # KiB or higher.
def make_nix_bin_path(exe_name: str) -> str:
global NIX_BIN_DIR
if NIX_BIN_DIR:
return os.path.join(NIX_BIN_DIR, exe_name)
else:
return exe_name
def run_list(*, root, only_selected, name_patterns): def run_list(*, root, only_selected, name_patterns):
path = Path(root) path = Path(root)
@ -491,7 +503,7 @@ def run_list(*, root, only_selected, name_patterns):
manifest = PackageManifest.parse_tree(path / "sw") manifest = PackageManifest.parse_tree(path / "sw")
closure_paths: List[str] = subprocess.run( closure_paths: List[str] = subprocess.run(
["nix-store", "-qR", str(path)], [make_nix_bin_path("nix-store"), "-qR", str(path)],
stdout=PIPE, stdout=PIPE,
text=True, text=True,
).stdout.rstrip("\n").split("\n") ).stdout.rstrip("\n").split("\n")
@ -549,12 +561,16 @@ def run_diff(*, root1, root2):
manifest_pair = PackageManifestPair(left_manifest, right_manifest) manifest_pair = PackageManifestPair(left_manifest, right_manifest)
left_closure_paths: List[str] = \ left_closure_paths: List[str] = subprocess.run(
subprocess.run(["nix-store", "-qR", str(left_resolved)], stdout=PIPE, text=True) \ [make_nix_bin_path("nix-store"), "-qR", str(left_resolved)],
.stdout.rstrip("\n").split("\n") stdout=PIPE,
right_closure_paths: List[str] = \ text=True,
subprocess.run(["nix-store", "-qR", (right_resolved)], stdout=PIPE, text=True) \ ).stdout.rstrip("\n").split("\n")
.stdout.rstrip("\n").split("\n") right_closure_paths: List[str] = subprocess.run(
[make_nix_bin_path("nix-store"), "-qR", (right_resolved)],
stdout=PIPE,
text=True,
).stdout.rstrip("\n").split("\n")
# Maps from pname to lists of versions. # Maps from pname to lists of versions.
left_closure_map: Dict[str, List[str]] = closure_paths_to_map(left_closure_paths) left_closure_map: Dict[str, List[str]] = closure_paths_to_map(left_closure_paths)
@ -665,6 +681,11 @@ def parse_args():
default="auto", default="auto",
help="Controls use of colour; one of 'auto', 'never', 'always'.") help="Controls use of colour; one of 'auto', 'never', 'always'.")
parser.add_argument(
"--nix-bin-dir",
default=None,
help="Optional directory containing nix binaries, overrides path lookup.")
subparsers = parser.add_subparsers(dest="action") subparsers = parser.add_subparsers(dest="action")
diff_parser = subparsers.add_parser( diff_parser = subparsers.add_parser(
@ -698,6 +719,7 @@ def parse_args():
return parser.parse_args() return parser.parse_args()
def main(): def main():
global NIX_BIN_DIR
global USE_COLOUR global USE_COLOUR
global INST_ADDED global INST_ADDED
global INST_REMOVED global INST_REMOVED
@ -707,8 +729,10 @@ def main():
args = parse_args() args = parse_args()
action = args.action action = args.action
NIX_BIN_DIR = args.nix_bin_dir or None
USE_COLOUR = args.color == "always" or (args.color == "auto" and sys.stdout.isatty()) USE_COLOUR = args.color == "always" or (args.color == "auto" and sys.stdout.isatty())
del args.action del args.action
del args.nix_bin_dir
del args.color del args.color
if USE_COLOUR: if USE_COLOUR:

View file

@ -5,11 +5,11 @@ nvd \- Nix/NixOS package version diff tool
.P .P
.B nvd [ -h | --help ] .B nvd [ -h | --help ]
.P .P
.B nvd [ --color=(auto|always|never) ] diff .B nvd [ GLOBAL OPTIONS ] diff
.I root1 .I root1
.I root2 .I root2
.P .P
.B nvd [ --color=(auto|always|never) ] list .B nvd [ GLOBAL OPTIONS ] list
.RS .RS
.B [ -r|--root .B [ -r|--root
.I store-path .I store-path
@ -21,6 +21,14 @@ nvd \- Nix/NixOS package version diff tool
.I name-pattern .I name-pattern
.B ]* .B ]*
.RE .RE
.P
.B GLOBAL OPTIONS:
.P
.RS
.B [ --color=(auto|always|never) ]
.br
.B [ --nix-bin-dir=<path> ]
.RE
.SH DESCRIPTION .SH DESCRIPTION
.P .P
.B nvd .B nvd
@ -113,6 +121,14 @@ forces colour to always be used, and passing
.B never .B never
forces colour to never be used. forces colour to never be used.
.TP .TP
--nix-bin-dir=<path>
An optional path to a directory containing
.B nix,
.B nix-store,
etc. binaries to use. If provided, all invocations of Nix binaries will use
this directory. If empty or not provided, then invocations of Nix binaries will
use regular path lookup. This is the default.
.TP
-r|--root <store-path> -r|--root <store-path>
For the For the
.B list .B list