Skip to content

Commit

Permalink
Merge remote-tracking branch 'iFreilicht/profile-names-instead-of-index'
Browse files Browse the repository at this point in the history
  • Loading branch information
max-privatevoid committed Jul 10, 2023
2 parents 2eaca25 + 3a40929 commit a4a636c
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 30 deletions.
8 changes: 4 additions & 4 deletions src/nix/profile-list.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ R""(

```console
# nix profile list
Index: 0
Name: gdb
Flake attribute: legacyPackages.x86_64-linux.gdb
Original flake URL: flake:nixpkgs
Locked flake URL: github:NixOS/nixpkgs/7b38b03d76ab71bdc8dc325e3f6338d984cc35ca
Store paths: /nix/store/indzcw5wvlhx6vwk7k4iq29q15chvr3d-gdb-11.1

Index: 1
Name: blender-bin
Flake attribute: packages.x86_64-linux.default
Original flake URL: flake:blender-bin
Locked flake URL: github:edolstra/nix-warez/91f2ffee657bf834e4475865ae336e2379282d34?dir=blender
Expand All @@ -26,15 +26,15 @@ R""(
# 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

This command shows what packages are currently installed in a
profile. For each installed package, it shows the following
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
upgrade`.

Expand Down
10 changes: 2 additions & 8 deletions src/nix/profile-remove.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,10 @@ R""(

# Examples

* Remove a package by position:
* Remove a package by name:

```console
# nix profile remove 3
```

* Remove a package by attribute path:

```console
# nix profile remove packages.x86_64-linux.hello
# nix profile remove hello
```

* Remove all packages:
Expand Down
11 changes: 1 addition & 10 deletions src/nix/profile-upgrade.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,12 @@ R""(
# nix profile upgrade '.*'
```

* Upgrade a specific package:
* Upgrade a specific package by name:

```console
# 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

This command upgrades a previously installed package in a Nix profile,
Expand Down
39 changes: 36 additions & 3 deletions src/nix/profile.cc
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ const int defaultPriority = 5;
struct ProfileElement
{
StorePathSet storePaths;
std::string name;
std::optional<ProfileElementSource> source;
bool active = true;
int priority = defaultPriority;
Expand Down Expand Up @@ -116,10 +117,13 @@ struct ProfileManifest

if (pathExists(manifestPath)) {
auto json = nlohmann::json::parse(readFile(manifestPath));
std::set<std::string> foundNames;

auto version = json.value("version", 0);
std::string sUrl;
std::string sOriginalUrl;
std::regex matchPackagesPrefix("((legacyP)|(p))ackages(\\.[a-z0-9_-]+)?\\.");
std::regex matchFlakeUrlPrefix("(.*?):(.*\\/)?");
switch (version) {
case 1:
sUrl = "uri";
Expand Down Expand Up @@ -149,6 +153,34 @@ struct ProfileManifest
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));
}
}
Expand All @@ -163,6 +195,7 @@ struct ProfileManifest
for (auto & drvInfo : drvInfos) {
ProfileElement element;
element.storePaths = {drvInfo.queryOutPath()};
element.name = element.identifier();
elements.emplace_back(std::move(element));
}
}
Expand Down Expand Up @@ -474,7 +507,7 @@ class MixProfileElementMatchers : virtual Args
if (element.storePaths.count(store.parseStorePath(*path))) return true;
} else if (auto regex = std::get_if<RegexPattern>(&matcher)) {
if (element.source
&& std::regex_match(element.source->attrPath, regex->reg))
&& std::regex_match(element.name, regex->reg))
return true;
}
}
Expand Down Expand Up @@ -659,8 +692,8 @@ struct CmdProfileList : virtual EvalCommand, virtual StoreCommand, MixDefaultPro
for (size_t i = 0; i < manifest.elements.size(); ++i) {
auto & element(manifest.elements[i]);
if (i) logger->cout("");
logger->cout("Index: " ANSI_BOLD "%s" ANSI_NORMAL "%s",
i,
logger->cout("Name: " ANSI_BOLD "%s" ANSI_NORMAL "%s",
element.name,
element.active ? "" : " " ANSI_RED "(inactive)" ANSI_NORMAL);
if (element.source) {
logger->cout("Flake attribute: %s%s", element.source->attrPath, element.source->outputs.to_string());
Expand Down
10 changes: 5 additions & 5 deletions tests/nix-profile.sh
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ cp ./config.nix $flake1Dir/

# Test upgrading from nix-env.
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 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" ]]
[ -e $TEST_HOME/.nix-profile/share/man ]
(! [ -e $TEST_HOME/.nix-profile/include ])
Expand All @@ -60,15 +60,15 @@ nix profile diff-closures | grep 'env-manifest.nix: ε → ∅'
# Test XDG Base Directories support

export NIX_CONFIG="use-xdg-base-directories = true"
nix profile remove 1
nix profile remove flake1
nix profile install $flake1Dir
[[ $($TEST_HOME/.local/state/nix/profile/bin/hello) = "Hello World" ]]
unset NIX_CONFIG

# Test upgrading a package.
printf NixOS > $flake1Dir/who
printf 2.0 > $flake1Dir/version
nix profile upgrade 1
nix profile upgrade flake1
[[ $($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"

Expand Down Expand Up @@ -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"

# Test new install of CA package.
nix profile remove 0
nix profile remove flake1
printf 4.0 > $flake1Dir/version
printf Utrecht > $flake1Dir/who
nix profile install $flake1Dir
Expand Down

0 comments on commit a4a636c

Please sign in to comment.