aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/libstore/local-store.cc29
-rw-r--r--src/libstore/local-store.hh2
-rw-r--r--src/libstore/store-api.hh3
-rw-r--r--src/libutil/util.cc10
-rw-r--r--src/libutil/util.hh2
-rwxr-xr-xsrc/nix-channel/nix-channel.cc8
-rw-r--r--src/nix-daemon/nix-daemon.cc9
-rw-r--r--src/nix-env/nix-env.cc30
8 files changed, 67 insertions, 26 deletions
diff --git a/src/libstore/local-store.cc b/src/libstore/local-store.cc
index 859094e61..00f3a31a2 100644
--- a/src/libstore/local-store.cc
+++ b/src/libstore/local-store.cc
@@ -70,15 +70,17 @@ LocalStore::LocalStore(const Params & params)
createSymlink(profilesDir, gcRootsDir + "/profiles");
}
+ for (auto & perUserDir : {profilesDir + "/per-user", gcRootsDir + "/per-user"}) {
+ createDirs(perUserDir);
+ if (chmod(perUserDir.c_str(), 0755) == -1)
+ throw SysError("could not set permissions on '%s' to 755", perUserDir);
+ }
+
+ createUser(getUserName(), getuid());
+
/* Optionally, create directories and set permissions for a
multi-user install. */
if (getuid() == 0 && settings.buildUsersGroup != "") {
-
- Path perUserDir = profilesDir + "/per-user";
- createDirs(perUserDir);
- if (chmod(perUserDir.c_str(), 01777) == -1)
- throw SysError(format("could not set permissions on '%1%' to 1777") % perUserDir);
-
mode_t perm = 01775;
struct group * gr = getgrnam(settings.buildUsersGroup.get().c_str());
@@ -1432,4 +1434,19 @@ void LocalStore::signPathInfo(ValidPathInfo & info)
}
+void LocalStore::createUser(const std::string & userName, uid_t userId)
+{
+ for (auto & dir : {
+ fmt("%s/profiles/per-user/%s", stateDir, userName),
+ fmt("%s/gcroots/per-user/%s", stateDir, userName)
+ }) {
+ createDirs(dir);
+ if (chmod(dir.c_str(), 0755) == -1)
+ throw SysError("changing permissions of directory '%s'", dir);
+ if (chown(dir.c_str(), userId, 0) == -1)
+ throw SysError("changing owner of directory '%s'", dir);
+ }
+}
+
+
}
diff --git a/src/libstore/local-store.hh b/src/libstore/local-store.hh
index 3ae34c403..379a06af8 100644
--- a/src/libstore/local-store.hh
+++ b/src/libstore/local-store.hh
@@ -293,6 +293,8 @@ private:
Path getRealStoreDir() override { return realStoreDir; }
+ void createUser(const std::string & userName, uid_t userId) override;
+
friend class DerivationGoal;
friend class SubstitutionGoal;
};
diff --git a/src/libstore/store-api.hh b/src/libstore/store-api.hh
index 7fb568602..ba8990755 100644
--- a/src/libstore/store-api.hh
+++ b/src/libstore/store-api.hh
@@ -628,6 +628,9 @@ public:
return storePath;
}
+ virtual void createUser(const std::string & userName, uid_t userId)
+ { }
+
protected:
Stats stats;
diff --git a/src/libutil/util.cc b/src/libutil/util.cc
index 1b7449991..6f3bf7ae8 100644
--- a/src/libutil/util.cc
+++ b/src/libutil/util.cc
@@ -475,6 +475,16 @@ Path createTempDir(const Path & tmpRoot, const Path & prefix,
}
+std::string getUserName()
+{
+ auto pw = getpwuid(geteuid());
+ std::string name = pw ? pw->pw_name : getEnv("USER", "");
+ if (name.empty())
+ throw Error("cannot figure out user name");
+ return name;
+}
+
+
static Lazy<Path> getHome2([]() {
Path homeDir = getEnv("HOME");
if (homeDir.empty()) {
diff --git a/src/libutil/util.hh b/src/libutil/util.hh
index 07c3d28ff..f057fdb2c 100644
--- a/src/libutil/util.hh
+++ b/src/libutil/util.hh
@@ -126,6 +126,8 @@ void deletePath(const Path & path, unsigned long long & bytesFreed);
Path createTempDir(const Path & tmpRoot = "", const Path & prefix = "nix",
bool includePid = true, bool useGlobalCounter = true, mode_t mode = 0755);
+std::string getUserName();
+
/* Return $HOME or the user's home directory from /etc/passwd. */
Path getHome();
diff --git a/src/nix-channel/nix-channel.cc b/src/nix-channel/nix-channel.cc
index 06eb3d23b..70aa5c966 100755
--- a/src/nix-channel/nix-channel.cc
+++ b/src/nix-channel/nix-channel.cc
@@ -159,13 +159,7 @@ static int _main(int argc, char ** argv)
nixDefExpr = home + "/.nix-defexpr";
// Figure out the name of the channels profile.
- ;
- auto pw = getpwuid(geteuid());
- std::string name = pw ? pw->pw_name : getEnv("USER", "");
- if (name.empty())
- throw Error("cannot figure out user name");
- profile = settings.nixStateDir + "/profiles/per-user/" + name + "/channels";
- createDirs(dirOf(profile));
+ profile = fmt("%s/profiles/per-user/%s/channels", settings.nixStateDir, getUserName());
enum {
cNone,
diff --git a/src/nix-daemon/nix-daemon.cc b/src/nix-daemon/nix-daemon.cc
index e88aaf636..cd18489b0 100644
--- a/src/nix-daemon/nix-daemon.cc
+++ b/src/nix-daemon/nix-daemon.cc
@@ -742,7 +742,8 @@ static void performOp(TunnelLogger * logger, ref<Store> store,
}
-static void processConnection(bool trusted)
+static void processConnection(bool trusted,
+ const std::string & userName, uid_t userId)
{
MonitorFdHup monitor(from.fd);
@@ -793,6 +794,8 @@ static void processConnection(bool trusted)
params["path-info-cache-size"] = "0";
auto store = openStore(settings.storeUri, params);
+ store->createUser(userName, userId);
+
tunnelLogger->stopWork();
to.flush();
@@ -1053,7 +1056,7 @@ static void daemonLoop(char * * argv)
/* Handle the connection. */
from.fd = remote.get();
to.fd = remote.get();
- processConnection(trusted);
+ processConnection(trusted, user, peer.uid);
exit(0);
}, options);
@@ -1133,7 +1136,7 @@ static int _main(int argc, char * * argv)
}
}
} else {
- processConnection(true);
+ processConnection(true, "root", 0);
}
} else {
daemonLoop(argv);
diff --git a/src/nix-env/nix-env.cc b/src/nix-env/nix-env.cc
index 48686ce72..2aeb8ebb0 100644
--- a/src/nix-env/nix-env.cc
+++ b/src/nix-env/nix-env.cc
@@ -192,12 +192,6 @@ static void loadDerivations(EvalState & state, Path nixExprPath,
}
-static Path getDefNixExprPath()
-{
- return getHome() + "/.nix-defexpr";
-}
-
-
static long getPriority(EvalState & state, DrvInfo & drv)
{
return drv.queryMetaInt("priority", 0);
@@ -1327,9 +1321,20 @@ static int _main(int argc, char * * argv)
Globals globals;
globals.instSource.type = srcUnknown;
- globals.instSource.nixExprPath = getDefNixExprPath();
+ globals.instSource.nixExprPath = getHome() + "/.nix-defexpr";
globals.instSource.systemFilter = "*";
+ if (!pathExists(globals.instSource.nixExprPath)) {
+ createDirs(globals.instSource.nixExprPath);
+ replaceSymlink(
+ fmt("%s/profiles/per-user/%s/channels", settings.nixStateDir, getUserName()),
+ globals.instSource.nixExprPath + "/channels");
+ if (getuid() != 0)
+ replaceSymlink(
+ fmt("%s/profiles/per-user/root/channels", settings.nixStateDir),
+ globals.instSource.nixExprPath + "/channels_root");
+ }
+
globals.dryRun = false;
globals.preserveInstalled = false;
globals.removeAll = false;
@@ -1422,9 +1427,14 @@ static int _main(int argc, char * * argv)
if (globals.profile == "") {
Path profileLink = getHome() + "/.nix-profile";
- globals.profile = pathExists(profileLink)
- ? absPath(readLink(profileLink), dirOf(profileLink))
- : canonPath(settings.nixStateDir + "/profiles/default");
+ 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));
}
op(globals, opFlags, opArgs);