Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/libcmd/command.cc
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ void MixProfile::updateProfile(Store & store, const BuiltPaths & buildables)

MixDefaultProfile::MixDefaultProfile()
{
profile = getDefaultProfile().string();
profile = getDefaultProfile(settings.getProfileDirsOptions()).string();
}

static constexpr auto environmentVariablesCategory = "Options that change environment variables";
Expand Down
5 changes: 3 additions & 2 deletions src/libexpr/eval-settings.cc
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,9 @@ Strings EvalSettings::getDefaultNixPath()
};

add(std::filesystem::path{getNixDefExpr()} / "channels");
add(rootChannelsDir() / "nixpkgs", "nixpkgs");
add(rootChannelsDir());
auto profilesDirOpts = settings.getProfileDirsOptions();
add(rootChannelsDir(profilesDirOpts) / "nixpkgs", "nixpkgs");
add(rootChannelsDir(profilesDirOpts));

return res;
}
Expand Down
9 changes: 9 additions & 0 deletions src/libstore/globals.cc
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "nix/store/globals.hh"
#include "nix/store/profiles.hh"
#include "nix/util/config-impl.hh"
#include "nix/util/config-global.hh"
#include "nix/util/current-process.hh"
Expand Down Expand Up @@ -294,6 +295,14 @@ const ExternalBuilder * Settings::findExternalDerivationBuilderIfSupported(const
return nullptr;
}

ProfileDirsOptions Settings::getProfileDirsOptions() const
{
return {
.nixStateDir = nixStateDir,
.useXDGBaseDirectories = useXDGBaseDirectories,
};
}

std::string nixVersion = PACKAGE_VERSION;

NLOHMANN_JSON_SERIALIZE_ENUM(
Expand Down
7 changes: 7 additions & 0 deletions src/libstore/include/nix/store/globals.hh
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

namespace nix {

struct ProfileDirsOptions;

struct LogFileSettings : public virtual Config
{
/**
Expand Down Expand Up @@ -398,6 +400,11 @@ public:
* derivation, or else returns a null pointer.
*/
const ExternalBuilder * findExternalDerivationBuilderIfSupported(const Derivation & drv);

/**
* Get the options needed for profile directory functions.
*/
ProfileDirsOptions getProfileDirsOptions() const;
};

// FIXME: don't use a global variable.
Expand Down
16 changes: 11 additions & 5 deletions src/libstore/include/nix/store/profiles.hh
Original file line number Diff line number Diff line change
Expand Up @@ -209,32 +209,38 @@ void lockProfile(PathLocks & lock, const std::filesystem::path & profile);
*/
std::string optimisticLockProfile(const std::filesystem::path & profile);

struct ProfileDirsOptions
{
const std::filesystem::path & nixStateDir;
bool useXDGBaseDirectories;
};

/**
* Create and return the path to a directory suitable for storing the user’s
* profiles.
*/
std::filesystem::path profilesDir();
std::filesystem::path profilesDir(ProfileDirsOptions opts);

/**
* Return the path to the profile directory for root (but don't try creating it)
*/
std::filesystem::path rootProfilesDir();
std::filesystem::path rootProfilesDir(ProfileDirsOptions opts);

/**
* Create and return the path to the file used for storing the users's channels
*/
std::filesystem::path defaultChannelsDir();
std::filesystem::path defaultChannelsDir(ProfileDirsOptions opts);

/**
* Return the path to the channel directory for root (but don't try creating it)
*/
std::filesystem::path rootChannelsDir();
std::filesystem::path rootChannelsDir(ProfileDirsOptions opts);

/**
* Resolve the default profile (~/.nix-profile by default,
* $XDG_STATE_HOME/nix/profile if XDG Base Directory Support is enabled),
* and create if doesn't exist
*/
std::filesystem::path getDefaultProfile();
std::filesystem::path getDefaultProfile(ProfileDirsOptions opts);

} // namespace nix
23 changes: 12 additions & 11 deletions src/libstore/profiles.cc
Original file line number Diff line number Diff line change
Expand Up @@ -291,31 +291,32 @@ std::string optimisticLockProfile(const std::filesystem::path & profile)
return pathExists(profile) ? readLink(profile).string() : "";
}

std::filesystem::path profilesDir()
std::filesystem::path profilesDir(ProfileDirsOptions settings)
{
auto profileRoot = isRootUser() ? rootProfilesDir() : std::filesystem::path{createNixStateDir()} / "profiles";
auto profileRoot =
isRootUser() ? rootProfilesDir(settings) : std::filesystem::path{createNixStateDir()} / "profiles";
createDirs(profileRoot);
return profileRoot;
}

std::filesystem::path rootProfilesDir()
std::filesystem::path rootProfilesDir(ProfileDirsOptions settings)
{
return std::filesystem::path{settings.nixStateDir} / "profiles/per-user/root";
return settings.nixStateDir / "profiles/per-user/root";
}

std::filesystem::path getDefaultProfile()
std::filesystem::path getDefaultProfile(ProfileDirsOptions settings)
{
std::filesystem::path profileLink = settings.useXDGBaseDirectories
? std::filesystem::path{createNixStateDir()} / "profile"
: std::filesystem::path{getHome()} / ".nix-profile";
try {
auto profile = profilesDir() / "profile";
auto profile = profilesDir(settings) / "profile";
if (!pathExists(profileLink)) {
replaceSymlink(profile, profileLink);
}
// Backwards compatibility measure: Make root's profile available as
// `.../default` as it's what NixOS and most of the init scripts expect
auto globalProfileLink = std::filesystem::path{settings.nixStateDir} / "profiles" / "default";
auto globalProfileLink = settings.nixStateDir / "profiles" / "default";
if (isRootUser() && !pathExists(globalProfileLink)) {
replaceSymlink(profile, globalProfileLink);
}
Expand All @@ -328,14 +329,14 @@ std::filesystem::path getDefaultProfile()
}
}

std::filesystem::path defaultChannelsDir()
std::filesystem::path defaultChannelsDir(ProfileDirsOptions settings)
{
return profilesDir() / "channels";
return profilesDir(settings) / "channels";
}

std::filesystem::path rootChannelsDir()
std::filesystem::path rootChannelsDir(ProfileDirsOptions settings)
{
return rootProfilesDir() / "channels";
return rootProfilesDir(settings) / "channels";
}

} // namespace nix
2 changes: 1 addition & 1 deletion src/nix/nix-channel/nix-channel.cc
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ static int main_nix_channel(int argc, char ** argv)
nixDefExpr = getNixDefExpr();

// Figure out the name of the channels profile.
profile = profilesDir() + "/channels";
profile = profilesDir(settings.getProfileDirsOptions()) + "/channels";
createDirs(dirOf(profile));

enum { cNone, cAdd, cRemove, cList, cUpdate, cListGenerations, cRollback } cmd = cNone;
Expand Down
5 changes: 3 additions & 2 deletions src/nix/nix-collect-garbage/nix-collect-garbage.cc
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,11 @@ static int main_nix_collect_garbage(int argc, char ** argv)
});

if (removeOld) {
auto profilesDirOpts = settings.getProfileDirsOptions();
std::set<std::filesystem::path> dirsToClean = {
profilesDir(),
profilesDir(profilesDirOpts),
std::filesystem::path{settings.nixStateDir} / "profiles",
getDefaultProfile().parent_path(),
getDefaultProfile(profilesDirOpts).parent_path(),
};
for (auto & dir : dirsToClean)
removeOldGenerations(dir);
Expand Down
7 changes: 4 additions & 3 deletions src/nix/nix-env/nix-env.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1414,10 +1414,11 @@ static int main_nix_env(int argc, char ** argv)

if (!pathExists(nixExprPath)) {
try {
auto profilesDirOpts = settings.getProfileDirsOptions();
createDirs(nixExprPath);
replaceSymlink(defaultChannelsDir(), nixExprPath / "channels");
replaceSymlink(defaultChannelsDir(profilesDirOpts), nixExprPath / "channels");
if (!isRootUser())
replaceSymlink(rootChannelsDir(), nixExprPath / "channels_root");
replaceSymlink(rootChannelsDir(profilesDirOpts), nixExprPath / "channels_root");
} catch (std::filesystem::filesystem_error &) {
} catch (Error &) {
}
Expand Down Expand Up @@ -1524,7 +1525,7 @@ static int main_nix_env(int argc, char ** argv)
globals.profile = getEnv("NIX_PROFILE").value_or("");

if (globals.profile == "")
globals.profile = getDefaultProfile().string();
globals.profile = getDefaultProfile(settings.getProfileDirsOptions()).string();

op(globals, std::move(opFlags), std::move(opArgs));

Expand Down
Loading