Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 20 additions & 3 deletions src/nix/profile.cc
Original file line number Diff line number Diff line change
Expand Up @@ -561,10 +561,24 @@ struct AllMatcher final : public Matcher

AllMatcher all;

class MixProfileElementMatchers : virtual Args, virtual StoreCommand
class MixProfileElementMatchers : virtual Args, virtual StoreCommand, public virtual MixDefaultProfile
{
std::vector<ref<Matcher>> _matchers;

void completeProfileElements(AddCompletions & completions, std::string_view prefix)
{
auto * evalCmd = dynamic_cast<EvalCommand *>(this);
if (!evalCmd)
return;

auto evalState = evalCmd->getEvalState();
ProfileManifest manifest(*evalState, *profile);

for (auto & [name, element] : manifest.elements)
if (name.starts_with(prefix))
completions.add(name, element.identifier());
}

public:

MixProfileElementMatchers()
Expand Down Expand Up @@ -595,6 +609,9 @@ class MixProfileElementMatchers : virtual Args, virtual StoreCommand
_matchers.push_back(make_ref<NameMatcher>(arg));
}
}
}},
.completer = {[this](AddCompletions & completions, size_t, std::string_view prefix) {
completeProfileElements(completions, prefix);
}}});
}

Expand Down Expand Up @@ -635,7 +652,7 @@ class MixProfileElementMatchers : virtual Args, virtual StoreCommand
}
};

struct CmdProfileRemove : virtual EvalCommand, MixDefaultProfile, MixProfileElementMatchers
struct CmdProfileRemove : virtual EvalCommand, MixProfileElementMatchers
{
std::string description() override
{
Expand Down Expand Up @@ -675,7 +692,7 @@ struct CmdProfileRemove : virtual EvalCommand, MixDefaultProfile, MixProfileElem
}
};

struct CmdProfileUpgrade : virtual SourceExprCommand, MixDefaultProfile, MixProfileElementMatchers
struct CmdProfileUpgrade : virtual SourceExprCommand, MixProfileElementMatchers
{
std::string description() override
{
Expand Down
19 changes: 19 additions & 0 deletions tests/functional/nix-profile.sh
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,25 @@ unset NIX_CONFIG
# Test conflicting package add.
nix profile add "$flake1Dir" 2>&1 | grep "warning: 'flake1' is already added"

# Test tab completion of profile elements
# The profile should have 'foo' and 'flake1' installed at this point
completion_output=$(NIX_GET_COMPLETIONS=3 nix profile remove '' 2>&1)
echo "$completion_output" | grep -q "^normal$"
echo "$completion_output" | grep -q "^flake1"
echo "$completion_output" | grep -q "^foo"

# Test prefix matching - should only complete 'flake1' when prefix is 'fl'
completion_output=$(NIX_GET_COMPLETIONS=3 nix profile remove 'fl' 2>&1)
echo "$completion_output" | grep -q "^normal$"
echo "$completion_output" | grep -q "^flake1"
echo "$completion_output" | grepQuietInverse "^foo"

# Test completion with upgrade command
completion_output=$(NIX_GET_COMPLETIONS=3 nix profile upgrade '' 2>&1)
echo "$completion_output" | grep -q "^normal$"
echo "$completion_output" | grep -q "^flake1"
echo "$completion_output" | grep -q "^foo"

# Test upgrading a package.
printf NixOS > "$flake1Dir"/who
printf 2.0 > "$flake1Dir"/version
Expand Down
Loading