nix profile: suggest removal using entry name

When a file conflict arises during a package install a suggestion is
made to remove the old entry. This was previously done using the
installable URLs of the old entry. These URLs are quite verbose and
often do not equal the URL of the existing entry.

This change uses the recently introduced profile entry name for the
suggestion, resulting in a simpler output.

The improvement is easily seen in the change to the functional test.
This commit is contained in:
Bob van der Linden 2024-02-21 21:55:51 +01:00
parent 774e7ca584
commit 4ae5091716
No known key found for this signature in database
2 changed files with 7 additions and 7 deletions

View file

@ -400,13 +400,13 @@ struct CmdProfileInstall : InstallablesCommand, MixDefaultProfile
// See https://github.com/NixOS/nix/compare/3efa476c5439f8f6c1968a6ba20a31d1239c2f04..1fe5d172ece51a619e879c4b86f603d9495cc102 // See https://github.com/NixOS/nix/compare/3efa476c5439f8f6c1968a6ba20a31d1239c2f04..1fe5d172ece51a619e879c4b86f603d9495cc102
auto findRefByFilePath = [&]<typename Iterator>(Iterator begin, Iterator end) { auto findRefByFilePath = [&]<typename Iterator>(Iterator begin, Iterator end) {
for (auto it = begin; it != end; it++) { for (auto it = begin; it != end; it++) {
auto & profileElement = it->second; auto & [name, profileElement] = *it;
for (auto & storePath : profileElement.storePaths) { for (auto & storePath : profileElement.storePaths) {
if (conflictError.fileA.starts_with(store->printStorePath(storePath))) { if (conflictError.fileA.starts_with(store->printStorePath(storePath))) {
return std::pair(conflictError.fileA, profileElement.toInstallables(*store)); return std::tuple(conflictError.fileA, name, profileElement.toInstallables(*store));
} }
if (conflictError.fileB.starts_with(store->printStorePath(storePath))) { if (conflictError.fileB.starts_with(store->printStorePath(storePath))) {
return std::pair(conflictError.fileB, profileElement.toInstallables(*store)); return std::tuple(conflictError.fileB, name, profileElement.toInstallables(*store));
} }
} }
} }
@ -415,9 +415,9 @@ struct CmdProfileInstall : InstallablesCommand, MixDefaultProfile
// There are 2 conflicting files. We need to find out which one is from the already installed package and // There are 2 conflicting files. We need to find out which one is from the already installed package and
// which one is the package that is the new package that is being installed. // which one is the package that is the new package that is being installed.
// The first matching package is the one that was already installed (original). // The first matching package is the one that was already installed (original).
auto [originalConflictingFilePath, originalConflictingRefs] = findRefByFilePath(manifest.elements.begin(), manifest.elements.end()); auto [originalConflictingFilePath, originalEntryName, originalConflictingRefs] = findRefByFilePath(manifest.elements.begin(), manifest.elements.end());
// The last matching package is the one that was going to be installed (new). // The last matching package is the one that was going to be installed (new).
auto [newConflictingFilePath, newConflictingRefs] = findRefByFilePath(manifest.elements.rbegin(), manifest.elements.rend()); auto [newConflictingFilePath, newEntryName, newConflictingRefs] = findRefByFilePath(manifest.elements.rbegin(), manifest.elements.rend());
throw Error( throw Error(
"An existing package already provides the following file:\n" "An existing package already provides the following file:\n"
@ -443,7 +443,7 @@ struct CmdProfileInstall : InstallablesCommand, MixDefaultProfile
" nix profile install %4% --priority %7%\n", " nix profile install %4% --priority %7%\n",
originalConflictingFilePath, originalConflictingFilePath,
newConflictingFilePath, newConflictingFilePath,
concatStringsSep(" ", originalConflictingRefs), originalEntryName,
concatStringsSep(" ", newConflictingRefs), concatStringsSep(" ", newConflictingRefs),
conflictError.priority, conflictError.priority,
conflictError.priority - 1, conflictError.priority - 1,

View file

@ -166,7 +166,7 @@ error: An existing package already provides the following file:
To remove the existing package: To remove the existing package:
nix profile remove path:${flake1Dir}#packages.${system}.default nix profile remove flake1
The new package can also be installed next to the existing one by assigning a different priority. The new package can also be installed next to the existing one by assigning a different priority.
The conflicting packages have a priority of 5. The conflicting packages have a priority of 5.