Merge remote-tracking branch 'iFreilicht/profile-names-instead-of-index'

https://github.com/NixOS/nix/pull/8678
This commit is contained in:
Max Headroom 2023-07-10 18:13:40 +02:00
commit a4a636c69d
5 changed files with 48 additions and 30 deletions

View file

@ -6,13 +6,13 @@ R""(
```console ```console
# nix profile list # nix profile list
Index: 0 Name: gdb
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
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 +26,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 `blender-bin` shown above.
# Description # Description
@ -34,7 +34,7 @@ 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`.

View file

@ -2,16 +2,10 @@ 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 attribute path:
```console
# nix profile remove packages.x86_64-linux.hello
``` ```
* Remove all packages: * Remove all packages:

View file

@ -9,21 +9,12 @@ R""(
# nix profile upgrade '.*' # nix profile upgrade '.*'
``` ```
* Upgrade a specific package: * Upgrade a specific package by name:
```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:
```console
# nix profile list
0 flake:nixpkgs#legacyPackages.x86_64-linux.spotify …
# 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

@ -43,6 +43,7 @@ const int defaultPriority = 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 = defaultPriority; int priority = defaultPriority;
@ -116,10 +117,13 @@ struct ProfileManifest
if (pathExists(manifestPath)) { if (pathExists(manifestPath)) {
auto json = nlohmann::json::parse(readFile(manifestPath)); auto json = nlohmann::json::parse(readFile(manifestPath));
std::set<std::string> foundNames;
auto version = json.value("version", 0); auto version = json.value("version", 0);
std::string sUrl; std::string sUrl;
std::string sOriginalUrl; std::string sOriginalUrl;
std::regex matchPackagesPrefix("((legacyP)|(p))ackages(\\.[a-z0-9_-]+)?\\.");
std::regex matchFlakeUrlPrefix("(.*?):(.*\\/)?");
switch (version) { switch (version) {
case 1: case 1:
sUrl = "uri"; sUrl = "uri";
@ -149,6 +153,34 @@ struct ProfileManifest
e["outputs"].get<ExtendedOutputsSpec>() e["outputs"].get<ExtendedOutputsSpec>()
}; };
} }
std::string nameCandidate;
if (e.contains("name")) {
nameCandidate = e["name"];
}
else {
/* Heuristically determine a decent name for the element. */
if (element.source) {
nameCandidate = std::regex_replace(static_cast<std::string>(e["attrPath"]), matchPackagesPrefix, "");
if (nameCandidate == "default") {
auto originalRef = element.source->originalRef;
nameCandidate = originalRef.input.getName();
if (nameCandidate == "source") {
nameCandidate = std::regex_replace(originalRef.to_string(), matchFlakeUrlPrefix , "");
}
}
}
else {
nameCandidate = element.identifier();
}
}
std::string finalName = nameCandidate;
for (int i = 1; foundNames.contains(finalName); ++i) {
finalName = nameCandidate + std::to_string(i);
}
element.name = finalName;
foundNames.insert(element.name);
elements.emplace_back(std::move(element)); elements.emplace_back(std::move(element));
} }
} }
@ -163,6 +195,7 @@ struct ProfileManifest
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));
} }
} }
@ -474,7 +507,7 @@ public:
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 (element.source
&& std::regex_match(element.source->attrPath, regex->reg)) && std::regex_match(element.name, regex->reg))
return true; return true;
} }
} }
@ -659,8 +692,8 @@ struct CmdProfileList : virtual EvalCommand, virtual StoreCommand, MixDefaultPro
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("");
logger->cout("Index: " ANSI_BOLD "%s" ANSI_NORMAL "%s", logger->cout("Name: " ANSI_BOLD "%s" ANSI_NORMAL "%s",
i, element.name,
element.active ? "" : " " ANSI_RED "(inactive)" ANSI_NORMAL); element.active ? "" : " " ANSI_RED "(inactive)" ANSI_NORMAL);
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());

View file

@ -47,9 +47,9 @@ 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 '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"
@ -104,7 +104,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