From 68987761ef2028f6733aab943e2a3cdc0632cbb4 Mon Sep 17 00:00:00 2001 From: Uluc Sengil Date: Tue, 10 Jun 2025 14:14:43 +0200 Subject: [PATCH 1/2] add layers to catching IFDs in `nix flake show` --- src/nix/flake.cc | 40 +++++++++++++++++++++++++++++++--------- 1 file changed, 31 insertions(+), 9 deletions(-) diff --git a/src/nix/flake.cc b/src/nix/flake.cc index a7b72c7e1..3bbfe9b22 100644 --- a/src/nix/flake.cc +++ b/src/nix/flake.cc @@ -1153,10 +1153,15 @@ struct CmdFlakeShow : FlakeCommand, MixJSON if ((attrPathS[0] == "apps" || attrPathS[0] == "checks" || attrPathS[0] == "devShells" || attrPathS[0] == "legacyPackages" || attrPathS[0] == "packages") && (attrPathS.size() == 1 || attrPathS.size() == 2)) { - for (const auto & subAttr : visitor2->getAttrs()) { - if (hasContent(*visitor2, attrPath2, subAttr)) { - return true; + try { + for (const auto & subAttr : visitor2->getAttrs()) { + if (hasContent(*visitor2, attrPath2, subAttr)) { + return true; + } } + } catch (IFDError & e) { + // allow IFD errors here; as we handle them during `visit()` + return true; } return false; } @@ -1164,10 +1169,14 @@ struct CmdFlakeShow : FlakeCommand, MixJSON if ((attrPathS.size() == 1) && (attrPathS[0] == "formatter" || attrPathS[0] == "nixosConfigurations" || attrPathS[0] == "nixosModules" || attrPathS[0] == "overlays")) { - for (const auto & subAttr : visitor2->getAttrs()) { - if (hasContent(*visitor2, attrPath2, subAttr)) { - return true; + try { + for (const auto & subAttr : visitor2->getAttrs()) { + if (hasContent(*visitor2, attrPath2, subAttr)) { + return true; + } } + } catch (IFDError & e) { + return true; } return false; } @@ -1204,9 +1213,22 @@ struct CmdFlakeShow : FlakeCommand, MixJSON if (!json) logger->cout("%s", headerPrefix); std::vector attrs; - for (const auto & attr : visitor.getAttrs()) { - if (hasContent(visitor, attrPath, attr)) - attrs.push_back(attr); + try { + for (const auto & attr : visitor.getAttrs()) { + if (hasContent(visitor, attrPath, attr)) + attrs.push_back(attr); + } + if (!json) + logger->cout("%s", headerPrefix); + } catch (IFDError & e) { + if (!json) { + logger->cout( + fmt("%s " ANSI_WARNING "omitted due to use of import from derivation" ANSI_NORMAL, + headerPrefix)); + } else { + logger->warn(fmt( + "%s omitted due to use of import from derivation", concatStringsSep(".", attrPathS))); + } } for (const auto & [i, attr] : enumerate(attrs)) { From 5c329233977e24e913f084979bead5befe90dc3a Mon Sep 17 00:00:00 2001 From: Uluc Sengil Date: Wed, 25 Jun 2025 10:41:56 +0200 Subject: [PATCH 2/2] add regression tests --- tests/functional/flakes/common.sh | 2 ++ tests/functional/flakes/show.sh | 2 ++ 2 files changed, 4 insertions(+) diff --git a/tests/functional/flakes/common.sh b/tests/functional/flakes/common.sh index 422cab96c..2950b3898 100644 --- a/tests/functional/flakes/common.sh +++ b/tests/functional/flakes/common.sh @@ -94,6 +94,8 @@ writeIfdFlake() { { outputs = { self }: { packages.$system.default = import ./ifd.nix; + checks.$system = import ./ifd.nix; + formatter = import ./ifd.nix; }; } EOF diff --git a/tests/functional/flakes/show.sh b/tests/functional/flakes/show.sh index 7fcc6aca9..5407e0086 100755 --- a/tests/functional/flakes/show.sh +++ b/tests/functional/flakes/show.sh @@ -100,5 +100,7 @@ nix eval --impure --expr ' let show_output = builtins.fromJSON (builtins.readFile ./show-output.json); in assert show_output.packages.${builtins.currentSystem}.default == { }; +assert show_output.checks.${builtins.currentSystem} == { }; +assert show_output.formatter == { }; true '