nix3-profile: allow using human-readable names to select packages

These names are parsed from the URL provided for that package

Based off of commit 257b768436a0e8ab7887f9b790c5b92a7fe51ef5

Upstream-PR: https://github.com/NixOS/nix/pull/8678
Co-authored-by: Felix Uhl <felix.uhl@outlook.com>
Change-Id: I76d5f9cfb11d3d2915b3dd1db21d7bb49e91f4fb
This commit is contained in:
Qyriad 2024-05-01 18:51:16 -06:00
parent 1425aa0b7c
commit f88423813f
8 changed files with 186 additions and 71 deletions

View file

@ -0,0 +1,7 @@
---
synopsis: "`nix profile` now allows referring to elements by human-readable name"
prs: 8678
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.

View file

@ -1,6 +1,9 @@
#include <set>
#include "cmd-profiles.hh" #include "cmd-profiles.hh"
#include "built-path.hh" #include "built-path.hh"
#include "builtins/buildenv.hh" #include "builtins/buildenv.hh"
#include "logging.hh"
#include "names.hh" #include "names.hh"
#include "store-api.hh" #include "store-api.hh"
@ -106,6 +109,8 @@ ProfileManifest::ProfileManifest(EvalState & state, const Path & profile)
if (pathExists(manifestPath)) { if (pathExists(manifestPath)) {
auto json = nlohmann::json::parse(readFile(manifestPath)); auto json = nlohmann::json::parse(readFile(manifestPath));
// Keep track of already found names so we can prevent duplicates.
std::set<std::string> foundNames;
auto version = json.value("version", 0); auto version = json.value("version", 0);
std::string sUrl; std::string sUrl;
@ -139,6 +144,26 @@ ProfileManifest::ProfileManifest(EvalState & state, const Path & profile)
e["attrPath"], e["attrPath"],
e["outputs"].get<ExtendedOutputsSpec>()}; e["outputs"].get<ExtendedOutputsSpec>()};
} }
std::string nameCandidate(element.identifier());
if (e.contains("name")) {
nameCandidate = e["name"];
} else if (element.source) {
auto const url = parseURL(element.source->to_string());
auto const name = getNameFromURL(url);
if (name) {
nameCandidate = *name;
}
}
auto finalName = nameCandidate;
for (unsigned appendedIndex = 1; foundNames.contains(finalName); ++appendedIndex) {
finalName = nameCandidate + std::to_string(appendedIndex);
}
element.name = finalName;
foundNames.insert(element.name);
elements.emplace_back(std::move(element)); elements.emplace_back(std::move(element));
} }
} else if (pathExists(profile + "/manifest.nix")) { } else if (pathExists(profile + "/manifest.nix")) {
@ -151,6 +176,7 @@ ProfileManifest::ProfileManifest(EvalState & state, const Path & profile)
for (auto & drvInfo : drvInfos) { for (auto & drvInfo : drvInfos) {
ProfileElement element; ProfileElement element;
element.storePaths = {drvInfo.queryOutPath()}; element.storePaths = {drvInfo.queryOutPath()};
element.name = element.identifier();
elements.emplace_back(std::move(element)); elements.emplace_back(std::move(element));
} }
} }

View file

@ -6,6 +6,8 @@
#include "flake/flakeref.hh" #include "flake/flakeref.hh"
#include "get-drvs.hh" #include "get-drvs.hh"
#include "types.hh" #include "types.hh"
#include "url.hh"
#include "url-name.hh"
#include <string> #include <string>
#include <set> #include <set>
@ -33,6 +35,7 @@ constexpr int DEFAULT_PRIORITY = 5;
struct ProfileElement struct ProfileElement
{ {
StorePathSet storePaths; StorePathSet storePaths;
std::string name;
std::optional<ProfileElementSource> source; std::optional<ProfileElementSource> source;
bool active = true; bool active = true;
int priority = DEFAULT_PRIORITY; int priority = DEFAULT_PRIORITY;

View file

@ -6,6 +6,7 @@ R""(
```console ```console
# nix profile list # nix profile list
Name: gdb
Index: 0 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
@ -13,6 +14,7 @@ R""(
Store paths: /nix/store/indzcw5wvlhx6vwk7k4iq29q15chvr3d-gdb-11.1 Store paths: /nix/store/indzcw5wvlhx6vwk7k4iq29q15chvr3d-gdb-11.1
Index: 1 Index: 1
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
Locked flake URL: github:edolstra/nix-warez/91f2ffee657bf834e4475865ae336e2379282d34?dir=blender Locked flake URL: github:edolstra/nix-warez/91f2ffee657bf834e4475865ae336e2379282d34?dir=blender
@ -26,7 +28,7 @@ R""(
# nix build github:edolstra/nix-warez/91f2ffee657bf834e4475865ae336e2379282d34?dir=blender#packages.x86_64-linux.default # nix build github:edolstra/nix-warez/91f2ffee657bf834e4475865ae336e2379282d34?dir=blender#packages.x86_64-linux.default
``` ```
will build the package with index 1 shown above. will build the package with name blender-bin shown above.
# Description # Description
@ -34,10 +36,13 @@ This command shows what packages are currently installed in a
profile. For each installed package, it shows the following profile. For each installed package, it shows the following
information: information:
* `Index`: An integer that can be used to unambiguously identify the * `Name`: A unique name used to unambiguously identify the
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

@ -2,10 +2,17 @@ R""(
# Examples # Examples
* Remove a package by position: * Remove a package by name:
```console ```console
# nix profile remove 3 # 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:

View file

@ -9,18 +9,19 @@ R""(
# nix profile upgrade '.*' # nix profile upgrade '.*'
``` ```
* Upgrade a specific package: * Upgrade a specific package by name:
```console
# nix profile upgrade hello
```
```console ```console
# nix profile upgrade packages.x86_64-linux.hello # nix profile upgrade packages.x86_64-linux.hello
``` ```
* Upgrade a specific profile element by number: * Upgrade a specific package by index:
```console ```console
# nix profile list
0 flake:nixpkgs#legacyPackages.x86_64-linux.spotify …
# nix profile upgrade 0 # nix profile upgrade 0
``` ```

View file

@ -189,14 +189,25 @@ public:
{ {
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); res.push_back(*n);
else if (store->isStorePath(s)) anyIndexMatchers = true;
} else if (store->isStorePath(s)) {
res.push_back(s); res.push_back(s);
else } else {
res.push_back(RegexPattern{s,std::regex(s, std::regex::extended | std::regex::icase)}); res.push_back(RegexPattern{s,std::regex(s, std::regex::extended | std::regex::icase)});
} }
}
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;
} }
@ -206,14 +217,15 @@ public:
for (auto & matcher : matchers) { for (auto & matcher : matchers) {
if (auto n = std::get_if<size_t>(&matcher)) { if (auto n = std::get_if<size_t>(&matcher)) {
if (*n == pos) return true; if (*n == pos) return true;
} else if (auto path = std::get_if<Path>(&matcher)) { } 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 (element.source if (std::regex_match(element.name, regex->reg)) {
&& std::regex_match(element.source->attrPath, regex->reg))
return true; return true;
} }
} }
}
return false; return false;
} }
@ -294,18 +306,42 @@ struct CmdProfileUpgrade : virtual SourceExprCommand, MixDefaultProfile, MixProf
Installables installables; Installables installables;
std::vector<size_t> indices; std::vector<size_t> indices;
auto matchedCount = 0;
auto upgradedCount = 0; auto upgradedCount = 0;
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 (element.source if (!matches(*store, element, i, matchers)) {
&& !element.source->originalRef.input.isLocked() continue;
&& matches(*store, element, i, matchers)) }
{
matchedCount += 1;
if (!element.source) {
warn(
"Found package '%s', but it was not installed from a flake, so it can't be checked for upgrades",
element.identifier()
);
continue;
}
if (element.source->originalRef.input.isLocked()) {
warn(
"Found package '%s', but it was installed from a locked flake reference so it can't be upgraded",
element.identifier()
);
continue;
}
upgradedCount++; upgradedCount++;
Activity act(*logger, lvlChatty, actUnknown, Activity act(
fmt("checking '%s' for updates", element.source->attrPath)); *logger,
lvlChatty,
actUnknown,
fmt("checking '%s' for updates", element.source->attrPath),
Logger::Fields{element.source->attrPath}
);
auto installable = make_ref<InstallableFlake>( auto installable = make_ref<InstallableFlake>(
this, this,
@ -315,19 +351,29 @@ struct CmdProfileUpgrade : virtual SourceExprCommand, MixDefaultProfile, MixProf
element.source->outputs, element.source->outputs,
Strings{element.source->attrPath}, Strings{element.source->attrPath},
Strings{}, Strings{},
lockFlags); lockFlags
);
auto derivedPaths = installable->toDerivedPaths(); auto derivedPaths = installable->toDerivedPaths();
if (derivedPaths.empty()) continue; if (derivedPaths.empty()) {
continue;
}
auto * infop = dynamic_cast<ExtraPathInfoFlake *>(&*derivedPaths[0].info); auto * infop = dynamic_cast<ExtraPathInfoFlake *>(&*derivedPaths[0].info);
// `InstallableFlake` should use `ExtraPathInfoFlake`. // `InstallableFlake` should use `ExtraPathInfoFlake`.
assert(infop); assert(infop);
auto & info = *infop; auto & info = *infop;
if (element.source->lockedRef == info.flake.lockedRef) continue; if (element.source->lockedRef == info.flake.lockedRef) {
continue;
}
printInfo("upgrading '%s' from flake '%s' to '%s'", printInfo(
element.source->attrPath, element.source->lockedRef, info.flake.lockedRef); "upgrading '%s' from flake '%s' to '%s'",
element.source->attrPath,
element.source->lockedRef,
info.flake.lockedRef
);
element.source = ProfileElementSource { element.source = ProfileElementSource {
.originalRef = installable->flakeRef, .originalRef = installable->flakeRef,
@ -338,10 +384,11 @@ struct CmdProfileUpgrade : virtual SourceExprCommand, MixDefaultProfile, MixProf
installables.push_back(installable); installables.push_back(installable);
indices.push_back(i); indices.push_back(i);
}
} }
if (upgradedCount == 0) { if (upgradedCount == 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 size_t * index = std::get_if<size_t>(&matcher)){
warn("'%d' is not a valid index", *index); warn("'%d' is not a valid index", *index);
@ -351,6 +398,9 @@ struct CmdProfileUpgrade : virtual SourceExprCommand, MixDefaultProfile, MixProf
warn("'%s' does not match any packages", regex->pattern); warn("'%s' does not match any packages", regex->pattern);
} }
} }
} else {
warn("Found some packages but none of them could be upgraded");
}
warn ("Use 'nix profile list' to see the current profile."); warn ("Use 'nix profile list' to see the current profile.");
} }
@ -394,10 +444,18 @@ struct CmdProfileList : virtual EvalCommand, virtual StoreCommand, MixDefaultPro
} else { } else {
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 (i) logger->cout(""); if (i) {
logger->cout("Index: " ANSI_BOLD "%s" ANSI_NORMAL "%s", logger->cout("");
i, }
element.active ? "" : " " ANSI_RED "(inactive)" ANSI_NORMAL); logger->cout(
"Name: " ANSI_BOLD "%s" ANSI_NORMAL "%s",
element.name,
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

@ -47,7 +47,7 @@ cp ./config.nix $flake1Dir/
# Test upgrading from nix-env. # Test upgrading from nix-env.
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 'Index:.*0' | 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 'Index:.*1' | grep 'Locked flake URL:.*narHash'
[[ $($TEST_HOME/.nix-profile/bin/hello) = "Hello World" ]] [[ $($TEST_HOME/.nix-profile/bin/hello) = "Hello World" ]]
@ -81,7 +81,7 @@ nix profile rollback
# Test uninstall. # Test uninstall.
[ -e $TEST_HOME/.nix-profile/bin/foo ] [ -e $TEST_HOME/.nix-profile/bin/foo ]
nix profile remove 0 nix profile remove "foo"
(! [ -e $TEST_HOME/.nix-profile/bin/foo ]) (! [ -e $TEST_HOME/.nix-profile/bin/foo ])
nix profile history | grep 'foo: 1.0 -> ∅' nix profile history | grep 'foo: 1.0 -> ∅'
nix profile diff-closures | grep 'Version 3 -> 4' nix profile diff-closures | grep 'Version 3 -> 4'
@ -93,6 +93,13 @@ nix profile remove 1
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!" ]]
# Test packages with same name from different sources
mkdir $TEST_ROOT/simple-too
cp ./simple.nix ./config.nix simple.builder.sh $TEST_ROOT/simple-too
nix profile install --file $TEST_ROOT/simple-too/simple.nix ''
nix profile list | grep -A4 'Name:.*simple' | grep 'Name:.*simple1'
nix profile remove simple1
# Test wipe-history. # Test wipe-history.
nix profile wipe-history nix profile wipe-history
[[ $(nix profile history | grep Version | wc -l) -eq 1 ]] [[ $(nix profile history | grep Version | wc -l) -eq 1 ]]
@ -104,7 +111,7 @@ nix profile upgrade 0
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.
nix profile remove 0 nix profile remove flake1
printf 4.0 > $flake1Dir/version printf 4.0 > $flake1Dir/version
printf Utrecht > $flake1Dir/who printf Utrecht > $flake1Dir/who
nix profile install $flake1Dir nix profile install $flake1Dir
@ -112,19 +119,20 @@ nix profile install $flake1Dir
[[ $(nix path-info --json $(realpath $TEST_HOME/.nix-profile/bin/hello) | jq -r .[].ca) =~ fixed:r:sha256: ]] [[ $(nix path-info --json $(realpath $TEST_HOME/.nix-profile/bin/hello) | jq -r .[].ca) =~ fixed:r:sha256: ]]
# Override the outputs. # Override the outputs.
nix profile remove 0 1 nix profile remove simple flake1
nix profile install "$flake1Dir^*" nix profile install "$flake1Dir^*"
[[ $($TEST_HOME/.nix-profile/bin/hello) = "Hello Utrecht" ]] [[ $($TEST_HOME/.nix-profile/bin/hello) = "Hello Utrecht" ]]
[ -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 ]
printf Nix > $flake1Dir/who printf Nix > $flake1Dir/who
nix profile upgrade 0 nix profile list
nix profile upgrade flake1
[[ $($TEST_HOME/.nix-profile/bin/hello) = "Hello Nix" ]] [[ $($TEST_HOME/.nix-profile/bin/hello) = "Hello Nix" ]]
[ -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 ]
nix profile remove 0 nix profile remove flake1
nix profile install "$flake1Dir^man" nix profile install "$flake1Dir^man"
(! [ -e $TEST_HOME/.nix-profile/bin/hello ]) (! [ -e $TEST_HOME/.nix-profile/bin/hello ])
[ -e $TEST_HOME/.nix-profile/share/man ] [ -e $TEST_HOME/.nix-profile/share/man ]