From a334e6c0ecbe432b276e247cccc4f7fdfb37f50e Mon Sep 17 00:00:00 2001 From: Bryan Gardiner Date: Wed, 12 Oct 2022 17:14:45 -0700 Subject: [PATCH] Add --nix-bin-dir option for specifying Nix binaries to use (issue #9). --- CHANGELOG.md | 3 +++ src/nvd | 42 +++++++++++++++++++++++++++++++++--------- src/nvd.1 | 20 ++++++++++++++++++-- 3 files changed, 54 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 383efc0..16e6206 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,9 @@ - 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) - Added a flake.nix, thanks @dadada_. diff --git a/src/nvd b/src/nvd index 7dc1621..8b9e951 100755 --- a/src/nvd +++ b/src/nvd @@ -53,6 +53,7 @@ signal(SIGPIPE, SIG_DFL) # Python handles SIGPIPE improperly by default. NVD_VERSION = "0.1.3" +NIX_BIN_DIR = None USE_COLOUR = False SGR_RESET = 0 @@ -229,7 +230,7 @@ class PackageManifest: @staticmethod def parse_tree(root: Path) -> "PackageManifest": direct_deps: List[str] = subprocess.run( - ["nix-store", "--query", "--references", str(root)], + [make_nix_bin_path("nix-store"), "--query", "--references", str(root)], stdout=PIPE, text=True ).stdout.rstrip("\n").split("\n") @@ -437,7 +438,11 @@ def query_closure_disk_usage_bytes(target: Path) -> Optional[int]: try: 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, text=True ).stdout @@ -477,6 +482,13 @@ def render_bytes(bytes_size: int) -> str: else: 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): path = Path(root) @@ -491,7 +503,7 @@ def run_list(*, root, only_selected, name_patterns): manifest = PackageManifest.parse_tree(path / "sw") closure_paths: List[str] = subprocess.run( - ["nix-store", "-qR", str(path)], + [make_nix_bin_path("nix-store"), "-qR", str(path)], stdout=PIPE, text=True, ).stdout.rstrip("\n").split("\n") @@ -549,12 +561,16 @@ def run_diff(*, root1, root2): manifest_pair = PackageManifestPair(left_manifest, right_manifest) - left_closure_paths: List[str] = \ - subprocess.run(["nix-store", "-qR", str(left_resolved)], stdout=PIPE, text=True) \ - .stdout.rstrip("\n").split("\n") - right_closure_paths: List[str] = \ - subprocess.run(["nix-store", "-qR", (right_resolved)], stdout=PIPE, text=True) \ - .stdout.rstrip("\n").split("\n") + left_closure_paths: List[str] = subprocess.run( + [make_nix_bin_path("nix-store"), "-qR", str(left_resolved)], + stdout=PIPE, + text=True, + ).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. left_closure_map: Dict[str, List[str]] = closure_paths_to_map(left_closure_paths) @@ -665,6 +681,11 @@ def parse_args(): default="auto", 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") diff_parser = subparsers.add_parser( @@ -698,6 +719,7 @@ def parse_args(): return parser.parse_args() def main(): + global NIX_BIN_DIR global USE_COLOUR global INST_ADDED global INST_REMOVED @@ -707,8 +729,10 @@ def main(): args = parse_args() 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()) del args.action + del args.nix_bin_dir del args.color if USE_COLOUR: diff --git a/src/nvd.1 b/src/nvd.1 index 345b3bf..bce95c8 100644 --- a/src/nvd.1 +++ b/src/nvd.1 @@ -5,11 +5,11 @@ nvd \- Nix/NixOS package version diff tool .P .B nvd [ -h | --help ] .P -.B nvd [ --color=(auto|always|never) ] diff +.B nvd [ GLOBAL OPTIONS ] diff .I root1 .I root2 .P -.B nvd [ --color=(auto|always|never) ] list +.B nvd [ GLOBAL OPTIONS ] list .RS .B [ -r|--root .I store-path @@ -21,6 +21,14 @@ nvd \- Nix/NixOS package version diff tool .I name-pattern .B ]* .RE +.P +.B GLOBAL OPTIONS: +.P +.RS +.B [ --color=(auto|always|never) ] +.br +.B [ --nix-bin-dir= ] +.RE .SH DESCRIPTION .P .B nvd @@ -113,6 +121,14 @@ forces colour to always be used, and passing .B never forces colour to never be used. .TP +--nix-bin-dir= +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 For the .B list