Skip to content

Commit

Permalink
nix-env --query: Respect --drv-path with --json
Browse files Browse the repository at this point in the history
```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"
  },
...
```
  • Loading branch information
Artturin committed Nov 4, 2023
1 parent a6e5879 commit 2b90cff
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 2 deletions.
2 changes: 2 additions & 0 deletions doc/manual/src/release-notes/rl-next.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

- Introduce a new built-in function [`builtins.convertHash`](@docroot@/language/builtins.md#builtins-convertHash).

- `nix-env --query` now respects `--drv-path` with `--json`.

- `nix-shell` shebang lines now support single-quoted arguments.

- `builtins.fetchTree` is now marked as stable.
9 changes: 7 additions & 2 deletions src/nix-env/nix-env.cc
Original file line number Diff line number Diff line change
Expand Up @@ -922,7 +922,7 @@ static VersionDiff compareVersionAgainstSet(
}


static void queryJSON(Globals & globals, std::vector<DrvInfo> & elems, bool printOutPath, bool printMeta)
static void queryJSON(Globals & globals, std::vector<DrvInfo> & elems, bool printOutPath, bool printDrvPath, bool printMeta)
{
using nlohmann::json;
json topObj = json::object();
Expand Down Expand Up @@ -953,6 +953,11 @@ static void queryJSON(Globals & globals, std::vector<DrvInfo> & 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();
Expand Down Expand Up @@ -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;
}
Expand Down
1 change: 1 addition & 0 deletions tests/functional/user-envs.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 2b90cff

Please sign in to comment.