aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorEelco Dolstra <edolstra@gmail.com>2019-10-21 16:07:19 +0200
committerEelco Dolstra <edolstra@gmail.com>2019-10-21 16:07:19 +0200
commita7aabd7cc785bbf34ad29101672677ced18a7fdd (patch)
treee5635720323ddfe9d2070370c91c6f3e4d566dbb /src
parenta07da2fd7a889c225847556c0d4bf88384995274 (diff)
Add getDefaultProfile() function
Diffstat (limited to 'src')
-rw-r--r--src/libstore/profiles.cc18
-rw-r--r--src/libstore/profiles.hh4
-rw-r--r--src/nix-env/nix-env.cc17
3 files changed, 24 insertions, 15 deletions
diff --git a/src/libstore/profiles.cc b/src/libstore/profiles.cc
index 4c6af567a..29f6f6c17 100644
--- a/src/libstore/profiles.cc
+++ b/src/libstore/profiles.cc
@@ -256,4 +256,22 @@ string optimisticLockProfile(const Path & profile)
}
+Path getDefaultProfile()
+{
+ Path profileLink = getHome() + "/.nix-profile";
+ try {
+ if (!pathExists(profileLink)) {
+ replaceSymlink(
+ getuid() == 0
+ ? settings.nixStateDir + "/profiles/default"
+ : fmt("%s/profiles/per-user/%s/profile", settings.nixStateDir, getUserName()),
+ profileLink);
+ }
+ return absPath(readLink(profileLink), dirOf(profileLink));
+ } catch (Error &) {
+ return profileLink;
+ }
+}
+
+
}
diff --git a/src/libstore/profiles.hh b/src/libstore/profiles.hh
index 5fa1533de..78645d8b6 100644
--- a/src/libstore/profiles.hh
+++ b/src/libstore/profiles.hh
@@ -64,4 +64,8 @@ void lockProfile(PathLocks & lock, const Path & profile);
rebuilt. */
string optimisticLockProfile(const Path & profile);
+/* Resolve ~/.nix-profile. If ~/.nix-profile doesn't exist yet, create
+ it. */
+Path getDefaultProfile();
+
}
diff --git a/src/nix-env/nix-env.cc b/src/nix-env/nix-env.cc
index 199dc92aa..5ac0eb87c 100644
--- a/src/nix-env/nix-env.cc
+++ b/src/nix-env/nix-env.cc
@@ -1427,21 +1427,8 @@ static int _main(int argc, char * * argv)
if (globals.profile == "")
globals.profile = getEnv("NIX_PROFILE", "");
- if (globals.profile == "") {
- Path profileLink = getHome() + "/.nix-profile";
- try {
- if (!pathExists(profileLink)) {
- replaceSymlink(
- getuid() == 0
- ? settings.nixStateDir + "/profiles/default"
- : fmt("%s/profiles/per-user/%s/profile", settings.nixStateDir, getUserName()),
- profileLink);
- }
- globals.profile = absPath(readLink(profileLink), dirOf(profileLink));
- } catch (Error &) {
- globals.profile = profileLink;
- }
- }
+ if (globals.profile == "")
+ globals.profile = getDefaultProfile();
op(globals, opFlags, opArgs);