Migrate the old profiles to the new location

Make sure that we don’t just create the new profiles directory, but that
we also migrate every existing profile to it.
This commit is contained in:
Théophane Hufschmitt 2022-04-13 10:26:50 +02:00
parent a5919f4754
commit 0601050755

View file

@ -293,17 +293,29 @@ Path getDefaultProfile()
Path profileLink = getHome() + "/.nix-profile"; Path profileLink = getHome() + "/.nix-profile";
try { try {
// Migrate from the “old-style” profiles stored under `/nix/var`: // Migrate from the “old-style” profiles stored under `/nix/var`:
// If the link exists and points to the old location, rewrite it to the // If the link exists and points to the old location, then:
// new one (otherwise keep-it as-it-is as it might have been // - Rewrite it to point to the new location
// intentionnally changed, in which case we shouldnt touch it) // - For every generation of the old default profile, create a symlink
// from the new directory to it (so that all the previous profiles
// and generations are still available).
auto legacyProfile = getuid() == 0 auto legacyProfile = getuid() == 0
? settings.nixStateDir + "/profiles/default" ? settings.nixStateDir + "/profiles/default"
: fmt("%s/profiles/per-user/%s/profile", settings.nixStateDir, getUserName()); : fmt("%s/profiles/per-user/%s/profile", settings.nixStateDir, getUserName());
if (!pathExists(profileLink) || auto newProfile = profilesDir() + "/profile";
(isLink(profileLink) && if (!pathExists(profileLink)
readLink(profileLink) == legacyProfile) || (isLink(profileLink)
) { && readLink(profileLink) == legacyProfile)) {
replaceSymlink(profilesDir() + "/profile", profileLink); warn("Migrating the default profile");
replaceSymlink(newProfile, profileLink);
replaceSymlink(legacyProfile, newProfile);
if (pathExists(legacyProfile)) {
for (auto & oldGen : findGenerations(legacyProfile).first) {
replaceSymlink(
oldGen.path,
dirOf(newProfile) + "/"
+ std::string(baseNameOf(oldGen.path)));
}
}
} }
return absPath(readLink(profileLink), dirOf(profileLink)); return absPath(readLink(profileLink), dirOf(profileLink));
} catch (Error &) { } catch (Error &) {