profile install: skip and warn on installing package twice

This commit is contained in:
Bob van der Linden 2024-02-26 21:09:17 +01:00
parent d28a240aa6
commit 14adff1711
No known key found for this signature in database
2 changed files with 23 additions and 1 deletions

View file

@ -395,7 +395,26 @@ struct CmdProfileInstall : InstallablesCommand, MixDefaultProfile
element.updateStorePaths(getEvalStore(), store, res);
manifest.addElement(std::move(element));
auto elementName = getNameFromElement(element);
// Check if the element already exists.
auto existingPair = manifest.elements.find(elementName);
if (existingPair != manifest.elements.end()) {
auto existingElement = existingPair->second;
auto existingSource = existingElement.source;
auto elementSource = element.source;
if (existingSource
&& elementSource
&& existingElement.priority == element.priority
&& existingSource->originalRef == elementSource->originalRef
&& existingSource->attrPath == elementSource->attrPath
) {
warn("'%s' is already installed", elementName);
continue;
}
}
manifest.addElement(elementName, std::move(element));
}
try {

View file

@ -64,6 +64,9 @@ nix profile install $flake1Dir
[[ $($TEST_HOME/.local/state/nix/profile/bin/hello) = "Hello World" ]]
unset NIX_CONFIG
# Test conflicting package install.
nix profile install $flake1Dir 2>&1 | grep "warning: 'flake1' is already installed"
# Test upgrading a package.
printf NixOS > $flake1Dir/who
printf 2.0 > $flake1Dir/version