diff --git a/src/libstore/profiles.cc b/src/libstore/profiles.cc index 239047dd6..d8717ab8b 100644 --- a/src/libstore/profiles.cc +++ b/src/libstore/profiles.cc @@ -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() { diff --git a/src/libstore/profiles.hh b/src/libstore/profiles.hh index 193c0bf21..bed8bf527 100644 --- a/src/libstore/profiles.hh +++ b/src/libstore/profiles.hh @@ -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), diff --git a/src/nix-channel/nix-channel.cc b/src/nix-channel/nix-channel.cc index 26003f021..c7d951d0b 100644 --- a/src/nix-channel/nix-channel.cc +++ b/src/nix-channel/nix-channel.cc @@ -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 { diff --git a/src/nix-env/nix-env.cc b/src/nix-env/nix-env.cc index ad255a1e1..eb33020e0 100644 --- a/src/nix-env/nix-env.cc +++ b/src/nix-env/nix-env.cc @@ -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));