nix3-profile: remove indices

Based off of commit 3187bc9ac3dd193b9329ef68c73ac3cca794ed78

Upstream-PR: https://github.com/NixOS/nix/pull/9656
Co-authored-by: Eelco Dolstra <edolstra@gmail.com>
Change-Id: I8ac4a33314cd1cf9de95404c20f58e883460acc7
This commit is contained in:
Qyriad 2024-05-01 19:12:04 -06:00
parent f88423813f
commit e98fc952a8
6 changed files with 17 additions and 56 deletions

View file

@ -1,7 +1,7 @@
--- ---
synopsis: "`nix profile` now allows referring to elements by human-readable name" synopsis: "`nix profile` now allows referring to elements by human-readable name, and no longer accepts indices"
prs: 8678 prs: 8678
cls: 978 cls: 978
--- ---
[`nix profile`](@docroot@/command-ref/new-cli/nix3-profile.md) now uses names to refer to installed packages when running [`list`](@docroot@/command-ref/new-cli/nix3-profile-list.md), [`remove`](@docroot@/command-ref/new-cli/nix3-profile-remove.md) or [`upgrade`](@docroot@/command-ref/new-cli/nix3-profile-upgrade.md) as opposed to indices. Indices are deprecated and will be removed in a future version. [`nix profile`](@docroot@/command-ref/new-cli/nix3-profile.md) now uses names to refer to installed packages when running [`list`](@docroot@/command-ref/new-cli/nix3-profile-list.md), [`remove`](@docroot@/command-ref/new-cli/nix3-profile-remove.md) or [`upgrade`](@docroot@/command-ref/new-cli/nix3-profile-upgrade.md) as opposed to indices. Indices have been removed.

View file

@ -7,13 +7,11 @@ R""(
```console ```console
# nix profile list # nix profile list
Name: gdb Name: gdb
Index: 0
Flake attribute: legacyPackages.x86_64-linux.gdb Flake attribute: legacyPackages.x86_64-linux.gdb
Original flake URL: flake:nixpkgs Original flake URL: flake:nixpkgs
Locked flake URL: github:NixOS/nixpkgs/7b38b03d76ab71bdc8dc325e3f6338d984cc35ca Locked flake URL: github:NixOS/nixpkgs/7b38b03d76ab71bdc8dc325e3f6338d984cc35ca
Store paths: /nix/store/indzcw5wvlhx6vwk7k4iq29q15chvr3d-gdb-11.1 Store paths: /nix/store/indzcw5wvlhx6vwk7k4iq29q15chvr3d-gdb-11.1
Index: 1
Name: blender-bin Name: blender-bin
Flake attribute: packages.x86_64-linux.default Flake attribute: packages.x86_64-linux.default
Original flake URL: flake:blender-bin Original flake URL: flake:blender-bin
@ -40,9 +38,6 @@ information:
package in invocations of `nix profile remove` and `nix profile package in invocations of `nix profile remove` and `nix profile
upgrade`. upgrade`.
* `Index`: An integer that can be used to unambiguously identify the package in invocations of `nix profile remove` and `nix profile upgrade`.
(*Deprecated, will be removed in a future version in favor of `Name`.*)
* `Flake attribute`: The flake output attribute path that provides the * `Flake attribute`: The flake output attribute path that provides the
package (e.g. `packages.x86_64-linux.hello`). package (e.g. `packages.x86_64-linux.hello`).

View file

@ -8,13 +8,6 @@ R""(
# nix profile remove hello # nix profile remove hello
``` ```
* Remove a package by index
*(deprecated, will be removed in a future version)*:
```console
$ nix profile remove 3
```
* Remove a package by attribute path: * Remove a package by attribute path:
```console ```console

View file

@ -19,12 +19,6 @@ R""(
# nix profile upgrade packages.x86_64-linux.hello # nix profile upgrade packages.x86_64-linux.hello
``` ```
* Upgrade a specific package by index:
```console
# nix profile upgrade 0
```
# Description # Description
This command upgrades a previously installed package in a Nix profile, This command upgrades a previously installed package in a Nix profile,

View file

@ -183,18 +183,15 @@ public:
std::string pattern; std::string pattern;
std::regex reg; std::regex reg;
}; };
typedef std::variant<size_t, Path, RegexPattern> Matcher; using Matcher = std::variant<Path, RegexPattern>;
std::vector<Matcher> getMatchers(ref<Store> store) std::vector<Matcher> getMatchers(ref<Store> store)
{ {
std::vector<Matcher> res; std::vector<Matcher> res;
auto anyIndexMatchers = false;
for (auto & s : _matchers) { for (auto & s : _matchers) {
if (auto n = string2Int<size_t>(s)) { if (auto n = string2Int<size_t>(s)) {
res.push_back(*n); throw Error("'nix profile' no longer supports indices ('%d')", *n);
anyIndexMatchers = true;
} else if (store->isStorePath(s)) { } else if (store->isStorePath(s)) {
res.push_back(s); res.push_back(s);
} else { } else {
@ -202,23 +199,13 @@ public:
} }
} }
if (anyIndexMatchers) {
warn(
"Indices are deprecated and be removed in a future version!\n"
" Refer to packages by their `Name` printed by `nix profile list`.\n"
);
}
return res; return res;
} }
bool matches(const Store & store, const ProfileElement & element, size_t pos, const std::vector<Matcher> & matchers) bool matches(const Store & store, const ProfileElement & element, const std::vector<Matcher> & matchers)
{ {
for (auto & matcher : matchers) { for (auto & matcher : matchers) {
if (auto n = std::get_if<size_t>(&matcher)) { if (auto path = std::get_if<Path>(&matcher)) {
if (*n == pos) return true;
} else if (auto path = std::get_if<Path>(&matcher)) {
if (element.storePaths.count(store.parseStorePath(*path))) return true; if (element.storePaths.count(store.parseStorePath(*path))) return true;
} else if (auto regex = std::get_if<RegexPattern>(&matcher)) { } else if (auto regex = std::get_if<RegexPattern>(&matcher)) {
if (std::regex_match(element.name, regex->reg)) { if (std::regex_match(element.name, regex->reg)) {
@ -255,7 +242,7 @@ struct CmdProfileRemove : virtual EvalCommand, MixDefaultProfile, MixProfileElem
for (size_t i = 0; i < oldManifest.elements.size(); ++i) { for (size_t i = 0; i < oldManifest.elements.size(); ++i) {
auto & element(oldManifest.elements[i]); auto & element(oldManifest.elements[i]);
if (!matches(*store, element, i, matchers)) { if (!matches(*store, element, matchers)) {
newManifest.elements.push_back(std::move(element)); newManifest.elements.push_back(std::move(element));
} else { } else {
notice("removing '%s'", element.identifier()); notice("removing '%s'", element.identifier());
@ -269,9 +256,7 @@ struct CmdProfileRemove : virtual EvalCommand, MixDefaultProfile, MixProfileElem
if (removedCount == 0) { if (removedCount == 0) {
for (auto matcher: matchers) { for (auto matcher: matchers) {
if (const size_t * index = std::get_if<size_t>(&matcher)){ if (const Path * path = std::get_if<Path>(&matcher)) {
warn("'%d' is not a valid index", *index);
} else if (const Path * path = std::get_if<Path>(&matcher)){
warn("'%s' does not match any paths", *path); warn("'%s' does not match any paths", *path);
} else if (const RegexPattern * regex = std::get_if<RegexPattern>(&matcher)){ } else if (const RegexPattern * regex = std::get_if<RegexPattern>(&matcher)){
warn("'%s' does not match any packages", regex->pattern); warn("'%s' does not match any packages", regex->pattern);
@ -311,7 +296,7 @@ struct CmdProfileUpgrade : virtual SourceExprCommand, MixDefaultProfile, MixProf
for (size_t i = 0; i < manifest.elements.size(); ++i) { for (size_t i = 0; i < manifest.elements.size(); ++i) {
auto & element(manifest.elements[i]); auto & element(manifest.elements[i]);
if (!matches(*store, element, i, matchers)) { if (!matches(*store, element, matchers)) {
continue; continue;
} }
@ -390,9 +375,7 @@ struct CmdProfileUpgrade : virtual SourceExprCommand, MixDefaultProfile, MixProf
if (upgradedCount == 0) { if (upgradedCount == 0) {
if (matchedCount == 0) { if (matchedCount == 0) {
for (auto & matcher : matchers) { for (auto & matcher : matchers) {
if (const size_t * index = std::get_if<size_t>(&matcher)){ if (const Path * path = std::get_if<Path>(&matcher)){
warn("'%d' is not a valid index", *index);
} else if (const Path * path = std::get_if<Path>(&matcher)){
warn("'%s' does not match any paths", *path); warn("'%s' does not match any paths", *path);
} else if (const RegexPattern * regex = std::get_if<RegexPattern>(&matcher)) { } else if (const RegexPattern * regex = std::get_if<RegexPattern>(&matcher)) {
warn("'%s' does not match any packages", regex->pattern); warn("'%s' does not match any packages", regex->pattern);
@ -452,10 +435,6 @@ struct CmdProfileList : virtual EvalCommand, virtual StoreCommand, MixDefaultPro
element.name, element.name,
element.active ? "" : " " ANSI_RED "(inactive)" ANSI_NORMAL element.active ? "" : " " ANSI_RED "(inactive)" ANSI_NORMAL
); );
logger->cout(
"Index: " ANSI_BOLD "%s" ANSI_NORMAL "%S",
i
);
if (element.source) { if (element.source) {
logger->cout("Flake attribute: %s%s", element.source->attrPath, element.source->outputs.to_string()); logger->cout("Flake attribute: %s%s", element.source->attrPath, element.source->outputs.to_string());
logger->cout("Original flake URL: %s", element.source->originalRef.to_string()); logger->cout("Original flake URL: %s", element.source->originalRef.to_string());

View file

@ -49,7 +49,7 @@ cp ./config.nix $flake1Dir/
nix-env -f ./user-envs.nix -i foo-1.0 nix-env -f ./user-envs.nix -i foo-1.0
nix profile list | grep -A2 'Name:.*foo' | grep 'Store paths:.*foo-1.0' nix profile list | grep -A2 'Name:.*foo' | grep 'Store paths:.*foo-1.0'
nix profile install $flake1Dir -L nix profile install $flake1Dir -L
nix profile list | grep -A4 'Index:.*1' | grep 'Locked flake URL:.*narHash' nix profile list | grep -A4 'Name:.*flake1' | grep 'Locked flake URL:.*narHash'
[[ $($TEST_HOME/.nix-profile/bin/hello) = "Hello World" ]] [[ $($TEST_HOME/.nix-profile/bin/hello) = "Hello World" ]]
[ -e $TEST_HOME/.nix-profile/share/man ] [ -e $TEST_HOME/.nix-profile/share/man ]
(! [ -e $TEST_HOME/.nix-profile/include ]) (! [ -e $TEST_HOME/.nix-profile/include ])
@ -60,7 +60,7 @@ nix profile diff-closures | grep 'env-manifest.nix: ε → ∅'
# Test XDG Base Directories support # Test XDG Base Directories support
export NIX_CONFIG="use-xdg-base-directories = true" export NIX_CONFIG="use-xdg-base-directories = true"
nix profile remove 1 nix profile remove flake1
nix profile install $flake1Dir nix profile install $flake1Dir
[[ $($TEST_HOME/.local/state/nix/profile/bin/hello) = "Hello World" ]] [[ $($TEST_HOME/.local/state/nix/profile/bin/hello) = "Hello World" ]]
unset NIX_CONFIG unset NIX_CONFIG
@ -68,7 +68,7 @@ unset NIX_CONFIG
# Test upgrading a package. # Test upgrading a package.
printf NixOS > $flake1Dir/who printf NixOS > $flake1Dir/who
printf 2.0 > $flake1Dir/version printf 2.0 > $flake1Dir/version
nix profile upgrade 1 nix profile upgrade flake1
[[ $($TEST_HOME/.nix-profile/bin/hello) = "Hello NixOS" ]] [[ $($TEST_HOME/.nix-profile/bin/hello) = "Hello NixOS" ]]
nix profile history | grep "packages.$system.default: 1.0, 1.0-man -> 2.0, 2.0-man" nix profile history | grep "packages.$system.default: 1.0, 1.0-man -> 2.0, 2.0-man"
@ -89,7 +89,7 @@ nix profile diff-closures | grep 'Version 3 -> 4'
# Test installing a non-flake package. # Test installing a non-flake package.
nix profile install --file ./simple.nix '' nix profile install --file ./simple.nix ''
[[ $(cat $TEST_HOME/.nix-profile/hello) = "Hello World!" ]] [[ $(cat $TEST_HOME/.nix-profile/hello) = "Hello World!" ]]
nix profile remove 1 nix profile remove simple
nix profile install $(nix-build --no-out-link ./simple.nix) nix profile install $(nix-build --no-out-link ./simple.nix)
[[ $(cat $TEST_HOME/.nix-profile/hello) = "Hello World!" ]] [[ $(cat $TEST_HOME/.nix-profile/hello) = "Hello World!" ]]
@ -107,7 +107,7 @@ nix profile wipe-history
# Test upgrade to CA package. # Test upgrade to CA package.
printf true > $flake1Dir/ca.nix printf true > $flake1Dir/ca.nix
printf 3.0 > $flake1Dir/version printf 3.0 > $flake1Dir/version
nix profile upgrade 0 nix profile upgrade flake1
nix profile history | grep "packages.$system.default: 1.0, 1.0-man -> 3.0, 3.0-man" nix profile history | grep "packages.$system.default: 1.0, 1.0-man -> 3.0, 3.0-man"
# Test new install of CA package. # Test new install of CA package.
@ -139,7 +139,7 @@ nix profile install "$flake1Dir^man"
(! [ -e $TEST_HOME/.nix-profile/include ]) (! [ -e $TEST_HOME/.nix-profile/include ])
# test priority # test priority
nix profile remove 0 nix profile remove flake1
# Make another flake. # Make another flake.
flake2Dir=$TEST_ROOT/flake2 flake2Dir=$TEST_ROOT/flake2