From a903f85f84b78a28490f3aa9615ba87d070d01d1 Mon Sep 17 00:00:00 2001 From: Artturin Date: Tue, 31 Oct 2023 01:36:13 +0200 Subject: [PATCH] `nix-env --query`: fix `--json` ignoring `--drv-path` ```json { "AMB-plugins": { "drvPath": "/nix/store/l99cb7h2hy8dg005arsjbd9kx0w05d3h-AMB-plugins-0.8.1.drv", "name": "AMB-plugins-0.8.1", "outputName": "out", "outputs": { "out": null }, "pname": "AMB-plugins", "system": "x86_64-linux", "version": "0.8.1" }, "ArchiSteamFarm": { "drvPath": "/nix/store/nhplgyjj34fz6hjmnyih25gxscfh8s7b-ArchiSteamFarm-5.4.12.5.drv", "name": "ArchiSteamFarm-5.4.12.5", "outputName": "out", "outputs": { "out": null }, "pname": "ArchiSteamFarm", "system": "x86_64-linux", "version": "5.4.12.5" }, ... ``` --- doc/manual/src/release-notes/rl-next.md | 2 ++ src/nix-env/nix-env.cc | 9 +++++++-- tests/functional/user-envs.sh | 1 + 3 files changed, 10 insertions(+), 2 deletions(-) diff --git a/doc/manual/src/release-notes/rl-next.md b/doc/manual/src/release-notes/rl-next.md index 276252c37..2163a5392 100644 --- a/doc/manual/src/release-notes/rl-next.md +++ b/doc/manual/src/release-notes/rl-next.md @@ -12,6 +12,8 @@ - Introduce a new built-in function [`builtins.convertHash`](@docroot@/language/builtins.md#builtins-convertHash). +- Fixed a bug where `nix-env --query` ignored `--drv-path` when `--json` was set. + - `nix-shell` shebang lines now support single-quoted arguments. - `builtins.fetchTree` is now marked as stable. diff --git a/src/nix-env/nix-env.cc b/src/nix-env/nix-env.cc index 01742daa8..558d0d6cd 100644 --- a/src/nix-env/nix-env.cc +++ b/src/nix-env/nix-env.cc @@ -922,7 +922,7 @@ static VersionDiff compareVersionAgainstSet( } -static void queryJSON(Globals & globals, std::vector & elems, bool printOutPath, bool printMeta) +static void queryJSON(Globals & globals, std::vector & elems, bool printOutPath, bool printDrvPath, bool printMeta) { using nlohmann::json; json topObj = json::object(); @@ -953,6 +953,11 @@ static void queryJSON(Globals & globals, std::vector & elems, bool prin } } + if (printDrvPath) { + auto drvPath = i.queryDrvPath(); + if (drvPath) pkgObj["drvPath"] = globals.state->store->printStorePath(*drvPath); + } + if (printMeta) { json &metaObj = pkgObj["meta"]; metaObj = json::object(); @@ -1079,7 +1084,7 @@ static void opQuery(Globals & globals, Strings opFlags, Strings opArgs) /* Print the desired columns, or XML output. */ if (jsonOutput) { - queryJSON(globals, elems, printOutPath, printMeta); + queryJSON(globals, elems, printOutPath, printDrvPath, printMeta); cout << '\n'; return; } diff --git a/tests/functional/user-envs.sh b/tests/functional/user-envs.sh index d1260ba04..dcd6b1b97 100644 --- a/tests/functional/user-envs.sh +++ b/tests/functional/user-envs.sh @@ -26,6 +26,7 @@ nix-env -f ./user-envs.nix -qa --json --out-path | jq -e '.[] | select(.name == .outputName == "out", (.outputs.out | test("'$NIX_STORE_DIR'.*-0\\.1")) ] | all' +nix-env -f ./user-envs.nix -qa --json --drv-path | jq -e '.[] | select(.name == "bar-0.1") | (.drvPath | test("'$NIX_STORE_DIR'.*-0\\.1\\.drv"))' # Query descriptions. nix-env -f ./user-envs.nix -qa '*' --description | grepQuiet silly