diff options
author | Qyriad <qyriad@qyriad.me> | 2024-05-01 18:51:16 -0600 |
---|---|---|
committer | Qyriad <qyriad@qyriad.me> | 2024-05-02 12:02:28 -0600 |
commit | f88423813f042cf40d9207409cd05cf4b75d87a0 (patch) | |
tree | d02a8cdc30d1978f79b8f98ba199225551ece77e /src/libcmd | |
parent | 1425aa0b7cd0d3477589f75bea4fb9c74e057fed (diff) |
nix3-profile: allow using human-readable names to select packages
These names are parsed from the URL provided for that package
Based off of commit 257b768436a0e8ab7887f9b790c5b92a7fe51ef5
Upstream-PR: https://github.com/NixOS/nix/pull/8678
Co-authored-by: Felix Uhl <felix.uhl@outlook.com>
Change-Id: I76d5f9cfb11d3d2915b3dd1db21d7bb49e91f4fb
Diffstat (limited to 'src/libcmd')
-rw-r--r-- | src/libcmd/cmd-profiles.cc | 26 | ||||
-rw-r--r-- | src/libcmd/cmd-profiles.hh | 3 |
2 files changed, 29 insertions, 0 deletions
diff --git a/src/libcmd/cmd-profiles.cc b/src/libcmd/cmd-profiles.cc index 4d8ff7438..5fa7f902c 100644 --- a/src/libcmd/cmd-profiles.cc +++ b/src/libcmd/cmd-profiles.cc @@ -1,6 +1,9 @@ +#include <set> + #include "cmd-profiles.hh" #include "built-path.hh" #include "builtins/buildenv.hh" +#include "logging.hh" #include "names.hh" #include "store-api.hh" @@ -106,6 +109,8 @@ ProfileManifest::ProfileManifest(EvalState & state, const Path & profile) if (pathExists(manifestPath)) { auto json = nlohmann::json::parse(readFile(manifestPath)); + // Keep track of already found names so we can prevent duplicates. + std::set<std::string> foundNames; auto version = json.value("version", 0); std::string sUrl; @@ -139,6 +144,26 @@ ProfileManifest::ProfileManifest(EvalState & state, const Path & profile) e["attrPath"], e["outputs"].get<ExtendedOutputsSpec>()}; } + + std::string nameCandidate(element.identifier()); + + if (e.contains("name")) { + nameCandidate = e["name"]; + } else if (element.source) { + auto const url = parseURL(element.source->to_string()); + auto const name = getNameFromURL(url); + if (name) { + nameCandidate = *name; + } + } + + auto finalName = nameCandidate; + for (unsigned appendedIndex = 1; foundNames.contains(finalName); ++appendedIndex) { + finalName = nameCandidate + std::to_string(appendedIndex); + } + element.name = finalName; + foundNames.insert(element.name); + elements.emplace_back(std::move(element)); } } else if (pathExists(profile + "/manifest.nix")) { @@ -151,6 +176,7 @@ ProfileManifest::ProfileManifest(EvalState & state, const Path & profile) for (auto & drvInfo : drvInfos) { ProfileElement element; element.storePaths = {drvInfo.queryOutPath()}; + element.name = element.identifier(); elements.emplace_back(std::move(element)); } } diff --git a/src/libcmd/cmd-profiles.hh b/src/libcmd/cmd-profiles.hh index d03f9b1c2..7f2c8a76f 100644 --- a/src/libcmd/cmd-profiles.hh +++ b/src/libcmd/cmd-profiles.hh @@ -6,6 +6,8 @@ #include "flake/flakeref.hh" #include "get-drvs.hh" #include "types.hh" +#include "url.hh" +#include "url-name.hh" #include <string> #include <set> @@ -33,6 +35,7 @@ constexpr int DEFAULT_PRIORITY = 5; struct ProfileElement { StorePathSet storePaths; + std::string name; std::optional<ProfileElementSource> source; bool active = true; int priority = DEFAULT_PRIORITY; |