From f68a3023cd4879b774631adcf3a52cdb313a5e73 Mon Sep 17 00:00:00 2001 From: Lorenz Leutgeb Date: Wed, 25 Oct 2023 00:45:29 +0200 Subject: [PATCH] fix: Make `nix flake show --json` print flake ref Fixes https://github.com/NixOS/nix/issues/9230 Breaking change. --- src/nix/flake.cc | 3 ++- tests/functional/flakes/show.sh | 25 ++++++++++++++++++++----- 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/src/nix/flake.cc b/src/nix/flake.cc index 0116eff2e..a1836bd07 100644 --- a/src/nix/flake.cc +++ b/src/nix/flake.cc @@ -1315,7 +1315,8 @@ struct CmdFlakeShow : FlakeCommand, MixJSON auto cache = openEvalCache(*state, flake); - auto j = visit(*cache->getRoot(), {}, fmt(ANSI_BOLD "%s" ANSI_NORMAL, flake->flake.lockedRef), ""); + auto lockedRef = flake->flake.lockedRef.to_string(); + auto j = nlohmann::json::object({ {lockedRef, visit(*cache->getRoot(), {}, fmt(ANSI_BOLD "%s" ANSI_NORMAL, lockedRef), "")} }); if (json) logger->cout("%s", j.dump()); } diff --git a/tests/functional/flakes/show.sh b/tests/functional/flakes/show.sh index a3d300552..48b5b12bc 100644 --- a/tests/functional/flakes/show.sh +++ b/tests/functional/flakes/show.sh @@ -6,10 +6,25 @@ mkdir -p "$flakeDir" writeSimpleFlake "$flakeDir" cd "$flakeDir" +# Check the flake reference that was produced. +nix flake show --json > output.json +nix eval --impure --expr ' +with builtins; +let + flakeRef = parseFlakeRef (head (attrNames (fromJSON (readFile ./output.json)))); + narHash = if match ".*darwin" currentSystem != null + then "sha256-MhmXWT9U41eDFezA02mAj3PxBO21icT5VvvPSC1epDU=" + else "sha256-op9gWTEmDXergxx2PojSTrxVREG7NLcrhySSun3DQd0=" + ; +in +assert flakeRef.narHash == narHash; +assert flakeRef.type == "path"; +true +' # By default: Only show the packages content for the current system and no # legacyPackages at all -nix flake show --json > show-output.json +nix flake show --json | jq 'to_entries[0].value' > show-output.json nix eval --impure --expr ' let show_output = builtins.fromJSON (builtins.readFile ./show-output.json); in @@ -20,7 +35,7 @@ true ' # With `--all-systems`, show the packages for all systems -nix flake show --json --all-systems > show-output.json +nix flake show --json --all-systems | jq 'to_entries[0].value' > show-output.json nix eval --impure --expr ' let show_output = builtins.fromJSON (builtins.readFile ./show-output.json); in @@ -30,7 +45,7 @@ true ' # With `--legacy`, show the legacy packages -nix flake show --json --legacy > show-output.json +nix flake show --json --legacy | jq 'to_entries[0].value' > show-output.json nix eval --impure --expr ' let show_output = builtins.fromJSON (builtins.readFile ./show-output.json); in @@ -57,7 +72,7 @@ cat >flake.nix < show-output.json +nix flake show --json --all-systems | jq 'to_entries[0].value' > show-output.json nix eval --impure --expr ' let show_output = builtins.fromJSON (builtins.readFile ./show-output.json); in @@ -77,7 +92,7 @@ cat >flake.nix < show-output.json +nix flake show --json --legacy --all-systems | jq 'to_entries[0].value' > show-output.json nix eval --impure --expr ' let show_output = builtins.fromJSON (builtins.readFile ./show-output.json); in