make profile querying logic more consistent

Change-Id: I00a4a8ba7f3a33aa191383f745f5a1395602e938
This commit is contained in:
Qyriad 2024-05-11 17:23:25 -06:00
parent 88d9b70f67
commit 7f35e7e704
4 changed files with 22 additions and 4 deletions

View file

@ -320,6 +320,17 @@ Path rootProfilesDir()
return settings.nixStateDir + "/profiles/per-user/root";
}
Path getDefaultProfileLink()
{
if (getuid() == 0) {
return settings.nixStateDir + "/profiles/default";
}
if (settings.useXDGBaseDirectories) {
return createNixStateDir() + "/profile";
}
return getHome() + "/.nix-profile";
}
Path getDefaultProfile()
{

View file

@ -227,6 +227,12 @@ Path defaultChannelsDir();
*/
Path rootChannelsDir();
/**
* Get the symlink that is used as the default profile, but do not resolve it.
* (see getDefaultProfile() for where that link is.)
*/
Path getDefaultProfileLink();
/**
* Resolve the default profile (~/.nix-profile by default,
* $XDG_STATE_HOME/nix/profile if XDG Base Directory Support is enabled),

View file

@ -169,7 +169,7 @@ static int main_nix_channel(int argc, char ** argv)
nixDefExpr = getNixDefExpr();
// Figure out the name of the channels profile.
profile = profilesDir() + "/channels";
profile = defaultChannelsDir();
createDirs(dirOf(profile));
enum {

View file

@ -1297,7 +1297,7 @@ static void opSwitchProfile(Globals & globals, Strings opFlags, Strings opArgs)
throw UsageError("exactly one argument expected");
Path profile = absPath(opArgs.front());
Path profileLink = settings.useXDGBaseDirectories ? createNixStateDir() + "/profile" : getHome() + "/.nix-profile";
Path profileLink = getDefaultProfileLink();
switchLink(profileLink, profile);
}
@ -1531,8 +1531,9 @@ static int main_nix_env(int argc, char * * argv)
if (globals.profile == "")
globals.profile = getEnv("NIX_PROFILE").value_or("");
if (globals.profile == "")
globals.profile = getDefaultProfile();
if (globals.profile == "") {
globals.profile = ensureDefaultProfile();
}
op(globals, std::move(opFlags), std::move(opArgs));