-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8678 from iFreilicht/profile-names-instead-of-index
`nix profile`: Allow referring to elements by human-readable name
- Loading branch information
Showing
12 changed files
with
281 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
synopsis: "`nix profile` now allows referring to elements by human-readable name" | ||
prs: 8678 | ||
--- | ||
|
||
[`nix profile`](@docroot@/command-ref/new-cli/nix3-profile.md) now uses names to refer to installed packages when running [`list`](@docroot@/command-ref/new-cli/nix3-profile-list.md), [`remove`](@docroot@/command-ref/new-cli/nix3-profile-remove.md) or [`upgrade`](@docroot@/command-ref/new-cli/nix3-profile-upgrade.md) as opposed to indices. Indices are deprecated and will be removed in a future version. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
#include "url-name.hh" | ||
#include <regex> | ||
#include <iostream> | ||
|
||
namespace nix { | ||
|
||
static const std::string attributeNamePattern("[a-z0-9_-]+"); | ||
static const std::regex lastAttributeRegex("(?:" + attributeNamePattern + "\\.)*(?!default)(" + 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<std::string> getNameFromURL(const ParsedURL & url) | ||
{ | ||
std::smatch match; | ||
|
||
/* If there is a dir= argument, use its value */ | ||
if (url.query.count("dir") > 0) | ||
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 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)) | ||
return match.str(1); | ||
|
||
/* If it is a regular git flake, use the directory name */ | ||
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); | ||
|
||
/* If even that didn't work, the URL does not contain enough info to determine a useful name */ | ||
return {}; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
#include "url.hh" | ||
#include "url-parts.hh" | ||
#include "util.hh" | ||
#include "split.hh" | ||
|
||
namespace nix { | ||
|
||
/** | ||
* Try to extract a reasonably unique and meaningful, human-readable | ||
* name of a flake output from a parsed URL. | ||
* When nullopt is returned, the callsite should use information available | ||
* to it outside of the URL to determine a useful name. | ||
* This is a heuristic approach intended for user interfaces. | ||
* @return nullopt if the extracted name is not useful to identify a | ||
* flake output, for example because it is empty or "default". | ||
* Otherwise returns the extracted name. | ||
*/ | ||
std::optional<std::string> getNameFromURL(const ParsedURL & url); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.