Skip to content

Commit

Permalink
profile install: fail on conflicting name
Browse files Browse the repository at this point in the history
  • Loading branch information
bobvanderlinden committed Feb 22, 2024
1 parent d3d0cb2 commit fdf8487
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
16 changes: 15 additions & 1 deletion src/nix/profile.cc
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,13 @@ struct ProfileManifest
addElement(name, std::move(element));
}

bool insertElement(ProfileElement element)
{
auto name = getNameFromElement(element);
auto [iterator, inserted] = elements.insert({name, std::move(element)});
return inserted;
}

nlohmann::json toJSON(Store & store) const
{
auto es = nlohmann::json::object();
Expand Down Expand Up @@ -393,7 +400,14 @@ struct CmdProfileInstall : InstallablesCommand, MixDefaultProfile

element.updateStorePaths(getEvalStore(), store, res);

manifest.addElement(std::move(element));
if (!manifest.insertElement(element)) {
std::string name = getNameFromElement(element);
throw Error(
"Package '%s' is already in the profile.\n"
"Remove the package first using 'nix profile remove %s'.",
name
);
}
}

try {
Expand Down
1 change: 1 addition & 0 deletions tests/functional/nix-profile.sh
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ printf World2 > $flake2Dir/who

nix profile install $flake1Dir
[[ $($TEST_HOME/.nix-profile/bin/hello) = "Hello World" ]]
expect 1 nix profile install $flake1Dir
expect 1 nix profile install $flake2Dir
diff -u <(
nix --offline profile install $flake2Dir 2>&1 1> /dev/null \
Expand Down

0 comments on commit fdf8487

Please sign in to comment.