Skip to content

Commit

Permalink
upgrade-nix: resolve profile symlinks
Browse files Browse the repository at this point in the history
The profile present in PATH is not necessarily the actual profile
location. User profiles are generally added as $HOME/.nix-profile
in which case the indirect profile link needs to be resolved first.

/home/user/.nix-profile -> /nix/var/nix/profiles/per-user/user/profile
/nix/var/nix/profiles/per-user/user/profile -> profile-15-link
/nix/var/nix/profiles/per-user/user/profile-14-link -> /nix/store/hyi4kkjh3bwi2z3wfljrkfymz9904h62-user-environment
/nix/var/nix/profiles/per-user/user/profile-15-link -> /nix/store/6njpl3qvihz46vj911pwx7hfcvwhifl9-user-environment

To upgrade nix here we want /nix/var/nix/profiles/per-user/user/profile-16-link
instead of /home/user/.nix-profile-1-link. The latter is not a gcroot
and would be garbage collected, resulting in a broken profile.

Fixes #2175
  • Loading branch information
LnL7 committed Aug 25, 2018
1 parent 4143977 commit d85bb48
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions src/nix/upgrade-nix.cc
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,18 @@ struct CmdUpgradeNix : MixDryRun, StoreCommand
if (hasPrefix(where, "/run/current-system"))
throw Error("Nix on NixOS must be upgraded via 'nixos-rebuild'");

Path profileDir;
Path userEnv;
Path profileDir = dirOf(where);

// Resolve profile to /nix/var/nix/profiles/<name> link.
while (baseNameOf(dirOf(canonPath(profileDir))) != "profiles")
profileDir = readLink(profileDir);

printInfo("found profile '%s'", profileDir);

Path userEnv = canonPath(profileDir, true);

if (baseNameOf(where) != "bin" ||
!hasSuffix(userEnv = canonPath(profileDir = dirOf(where), true), "user-environment"))
!hasSuffix(userEnv, "user-environment"))
throw Error("directory '%s' does not appear to be part of a Nix profile", where);

if (!store->isValidPath(userEnv))
Expand Down

0 comments on commit d85bb48

Please sign in to comment.