From e5d9130a5bb1ee3f2926afff17be4c031ca404a0 Mon Sep 17 00:00:00 2001 From: Bob van der Linden Date: Mon, 26 Feb 2024 21:45:41 +0100 Subject: [PATCH] Fix extraction of name for defaultPackage URLs --- src/libexpr/flake/url-name.cc | 14 ++++++-------- tests/unit/libexpr/flake/url-name.cc | 1 + 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/src/libexpr/flake/url-name.cc b/src/libexpr/flake/url-name.cc index 753f197d555..d62b345522a 100644 --- a/src/libexpr/flake/url-name.cc +++ b/src/libexpr/flake/url-name.cc @@ -5,13 +5,12 @@ namespace nix { static const std::string attributeNamePattern("[a-zA-Z0-9_-]+"); -static const std::regex lastAttributeRegex("(?:" + attributeNamePattern + "\\.)*(?!default)(" + attributeNamePattern +")(\\^.*)?"); +static const std::regex lastAttributeRegex("^((?:" + attributeNamePattern + "\\.)*)(" + attributeNamePattern +")(\\^.*)?$"); static const std::string pathSegmentPattern("[a-zA-Z0-9_-]+"); static const std::regex lastPathSegmentRegex(".*/(" + pathSegmentPattern +")"); static const std::regex secondPathSegmentRegex("(?:" + pathSegmentPattern + ")/(" + pathSegmentPattern +")(?:/.*)?"); static const std::regex gitProviderRegex("github|gitlab|sourcehut"); static const std::regex gitSchemeRegex("git($|\\+.*)"); -static const std::regex defaultOutputRegex(".*\\.default($|\\^.*)"); std::optional getNameFromURL(const ParsedURL & url) { @@ -22,8 +21,11 @@ std::optional getNameFromURL(const ParsedURL & url) return url.query.at("dir"); /* If the fragment isn't a "default" and contains two attribute elements, use the last one */ - if (std::regex_match(url.fragment, match, lastAttributeRegex)) - return match.str(1); + if (std::regex_match(url.fragment, match, lastAttributeRegex) + && match.str(1) != "defaultPackage." + && match.str(2) != "default") { + return match.str(2); + } /* If this is a github/gitlab/sourcehut flake, use the repo name */ if (std::regex_match(url.scheme, gitProviderRegex) && std::regex_match(url.path, match, secondPathSegmentRegex)) @@ -33,10 +35,6 @@ std::optional getNameFromURL(const ParsedURL & url) if (std::regex_match(url.scheme, gitSchemeRegex) && std::regex_match(url.path, match, lastPathSegmentRegex)) return match.str(1); - /* If everything failed but there is a non-default fragment, use it in full */ - if (!url.fragment.empty() && !std::regex_match(url.fragment, defaultOutputRegex)) - return url.fragment; - /* If there is no fragment, take the last element of the path */ if (std::regex_match(url.path, match, lastPathSegmentRegex)) return match.str(1); diff --git a/tests/unit/libexpr/flake/url-name.cc b/tests/unit/libexpr/flake/url-name.cc index 85387b323e3..15bc6b11165 100644 --- a/tests/unit/libexpr/flake/url-name.cc +++ b/tests/unit/libexpr/flake/url-name.cc @@ -14,6 +14,7 @@ namespace nix { ASSERT_EQ(getNameFromURL(parseURL("path:./repos/myflake#nonStandardAttr.mylaptop")), "mylaptop"); ASSERT_EQ(getNameFromURL(parseURL("path:./nixpkgs#packages.x86_64-linux.complex^bin,man")), "complex"); ASSERT_EQ(getNameFromURL(parseURL("path:./myproj#packages.x86_64-linux.default^*")), "myproj"); + ASSERT_EQ(getNameFromURL(parseURL("path:./myproj#defaultPackage.x86_64-linux")), "myproj"); ASSERT_EQ(getNameFromURL(parseURL("github:NixOS/nixpkgs#packages.x86_64-linux.hello")), "hello"); ASSERT_EQ(getNameFromURL(parseURL("github:NixOS/nixpkgs#hello")), "hello");