From 02bff90e7bbc04244b3d566d82c945156cb0885b Mon Sep 17 00:00:00 2001 From: Max Date: Thu, 1 Sep 2022 22:02:38 +0200 Subject: [PATCH 1/6] nix flake show: don't evaluate derivations for foreign systems by default --- src/nix/flake.cc | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/src/nix/flake.cc b/src/nix/flake.cc index 3967f1102..5232534c6 100644 --- a/src/nix/flake.cc +++ b/src/nix/flake.cc @@ -957,6 +957,7 @@ struct CmdFlakeArchive : FlakeCommand, MixJSON, MixDryRun struct CmdFlakeShow : FlakeCommand, MixJSON { bool showLegacy = false; + bool showForeign = false; CmdFlakeShow() { @@ -965,6 +966,11 @@ struct CmdFlakeShow : FlakeCommand, MixJSON .description = "Show the contents of the `legacyPackages` output.", .handler = {&showLegacy, true} }); + addFlag({ + .longName = "foreign", + .description = "Show the contents of outputs for foreign systems.", + .handler = {&showForeign, true} + }); } std::string description() override @@ -985,6 +991,7 @@ struct CmdFlakeShow : FlakeCommand, MixJSON auto state = getEvalState(); auto flake = std::make_shared(lockFlake()); + auto localSystem = std::string(settings.thisSystem.get()); std::functioncout(fmt("%s " ANSI_WARNING "omitted" ANSI_NORMAL " (use '--foreign' to show)", headerPrefix)); + else { + logger->warn(fmt("%s omitted (use '--foreign' to show)", concatStringsSep(".", attrPathS))); + } + } else { + if (visitor.isDerivation()) + showDerivation(); + else + throw Error("expected a derivation"); + } } else if (attrPath.size() > 0 && attrPathS[0] == "hydraJobs") { From 6da4cc92d8c546939818b65ba4f1b4ce65d88d6e Mon Sep 17 00:00:00 2001 From: Max Date: Wed, 7 Sep 2022 20:28:30 +0200 Subject: [PATCH 2/6] showForeign -> showAllSystems --- src/nix/flake.cc | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/nix/flake.cc b/src/nix/flake.cc index 5232534c6..bb8be872b 100644 --- a/src/nix/flake.cc +++ b/src/nix/flake.cc @@ -957,7 +957,7 @@ struct CmdFlakeArchive : FlakeCommand, MixJSON, MixDryRun struct CmdFlakeShow : FlakeCommand, MixJSON { bool showLegacy = false; - bool showForeign = false; + bool showAllSystems = false; CmdFlakeShow() { @@ -967,9 +967,9 @@ struct CmdFlakeShow : FlakeCommand, MixJSON .handler = {&showLegacy, true} }); addFlag({ - .longName = "foreign", - .description = "Show the contents of outputs for foreign systems.", - .handler = {&showForeign, true} + .longName = "all-systems", + .description = "Show the contents of outputs for all systems.", + .handler = {&showAllSystems, true} }); } @@ -1082,11 +1082,11 @@ struct CmdFlakeShow : FlakeCommand, MixJSON || (attrPath.size() == 3 && (attrPathS[0] == "checks" || attrPathS[0] == "packages" || attrPathS[0] == "devShells")) ) { - if (!showForeign && std::string(attrPathS[1]) != localSystem) { + if (!showAllSystems && std::string(attrPathS[1]) != localSystem) { if (!json) - logger->cout(fmt("%s " ANSI_WARNING "omitted" ANSI_NORMAL " (use '--foreign' to show)", headerPrefix)); + logger->cout(fmt("%s " ANSI_WARNING "omitted" ANSI_NORMAL " (use '--all-systems' to show)", headerPrefix)); else { - logger->warn(fmt("%s omitted (use '--foreign' to show)", concatStringsSep(".", attrPathS))); + logger->warn(fmt("%s omitted (use '--all-systems' to show)", concatStringsSep(".", attrPathS))); } } else { if (visitor.isDerivation()) From 79c084cb598221f5910c429127e8da33cff6a206 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9ophane=20Hufschmitt?= Date: Fri, 27 Jan 2023 10:13:05 +0100 Subject: [PATCH 3/6] Add a test for `nix flake show` --- tests/flakes/common.sh | 4 ++++ tests/flakes/show.sh | 39 +++++++++++++++++++++++++++++++++++++++ tests/local.mk | 1 + 3 files changed, 44 insertions(+) create mode 100644 tests/flakes/show.sh diff --git a/tests/flakes/common.sh b/tests/flakes/common.sh index c333733c2..fdcdd4aef 100644 --- a/tests/flakes/common.sh +++ b/tests/flakes/common.sh @@ -20,6 +20,10 @@ writeSimpleFlake() { foo = import ./simple.nix; default = foo; }; + packages.someOtherSystem = rec { + foo = import ./simple.nix; + default = foo; + }; # To test "nix flake init". legacyPackages.x86_64-linux.hello = import ./simple.nix; diff --git a/tests/flakes/show.sh b/tests/flakes/show.sh new file mode 100644 index 000000000..995de8dc3 --- /dev/null +++ b/tests/flakes/show.sh @@ -0,0 +1,39 @@ +source ./common.sh + +flakeDir=$TEST_ROOT/flake +mkdir -p "$flakeDir" + +writeSimpleFlake "$flakeDir" +cd "$flakeDir" + + +# By default: Only show the packages content for the current system and no +# legacyPackages at all +nix flake show --json > show-output.json +nix eval --impure --expr ' +let show_output = builtins.fromJSON (builtins.readFile ./show-output.json); +in +assert show_output.packages.someOtherSystem.default == {}; +assert show_output.packages.${builtins.currentSystem}.default.name == "simple"; +assert show_output.legacyPackages.${builtins.currentSystem} == {}; +true +' + +# With `--all-systems`, show the packages for all systems +nix flake show --json --all-systems > show-output.json +nix eval --impure --expr ' +let show_output = builtins.fromJSON (builtins.readFile ./show-output.json); +in +assert show_output.packages.someOtherSystem.default.name == "simple"; +assert show_output.legacyPackages.${builtins.currentSystem} == {}; +true +' + +# With `--legacy`, show the legacy packages +nix flake show --json --legacy > show-output.json +nix eval --impure --expr ' +let show_output = builtins.fromJSON (builtins.readFile ./show-output.json); +in +assert show_output.legacyPackages.${builtins.currentSystem}.hello.name == "simple"; +true +' diff --git a/tests/local.mk b/tests/local.mk index 5ac1ede32..c8ee82e1a 100644 --- a/tests/local.mk +++ b/tests/local.mk @@ -113,6 +113,7 @@ nix_tests = \ store-ping.sh \ fetchClosure.sh \ completions.sh \ + flakes/show.sh \ impure-derivations.sh \ path-from-hash-part.sh \ toString-path.sh From 02e81cdf6276dbaaa5ecb720fb3244d5cf2c5e8e Mon Sep 17 00:00:00 2001 From: Max Date: Fri, 27 Jan 2023 23:59:48 +0100 Subject: [PATCH 4/6] apply showAllSystems to legacyPackages as well --- src/nix/flake.cc | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/nix/flake.cc b/src/nix/flake.cc index 2e4d2cf3a..d2f68c37c 100644 --- a/src/nix/flake.cc +++ b/src/nix/flake.cc @@ -1121,6 +1121,12 @@ struct CmdFlakeShow : FlakeCommand, MixJSON else { logger->warn(fmt("%s omitted (use '--legacy' to show)", concatStringsSep(".", attrPathS))); } + } else if (!showAllSystems && std::string(attrPathS[1]) != localSystem) { + if (!json) + logger->cout(fmt("%s " ANSI_WARNING "omitted" ANSI_NORMAL " (use '--all-systems' to show)", headerPrefix)); + else { + logger->warn(fmt("%s omitted (use '--all-systems' to show)", concatStringsSep(".", attrPathS))); + } } else { if (visitor.isDerivation()) showDerivation(); From bc6e65e26fd54f9c27a0344b86fc813820a7e9e5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9ophane=20Hufschmitt?= Date: Mon, 30 Jan 2023 10:44:10 +0100 Subject: [PATCH 5/6] Fix the flakes/show test MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Don't hardcode “x86_64-linux” as this won't work too nicely on other platforms --- tests/flakes/common.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/flakes/common.sh b/tests/flakes/common.sh index fdcdd4aef..9d79080cd 100644 --- a/tests/flakes/common.sh +++ b/tests/flakes/common.sh @@ -26,7 +26,7 @@ writeSimpleFlake() { }; # To test "nix flake init". - legacyPackages.x86_64-linux.hello = import ./simple.nix; + legacyPackages.$system.hello = import ./simple.nix; }; } EOF From 3ac9f1658a482b3bf94b4a78263ea8e1ebea5b13 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9ophane=20Hufschmitt?= Date: Mon, 30 Jan 2023 11:21:52 +0100 Subject: [PATCH 6/6] Fix the flakes init test Things leading to another... --- tests/flakes/init.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/flakes/init.sh b/tests/flakes/init.sh index 36cb9956a..2d4c77ba1 100644 --- a/tests/flakes/init.sh +++ b/tests/flakes/init.sh @@ -41,8 +41,8 @@ cat > $templatesDir/trivial/flake.nix <