Skip to content

Commit f66108f

Browse files
committed
nix-env: Create ~/.nix-profile automatically
(cherry picked from commit 9348f92)
1 parent 2070d55 commit f66108f

File tree

9 files changed

+31
-42
lines changed

9 files changed

+31
-42
lines changed

scripts/nix-profile-daemon.sh.in

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,6 @@ export NIX_USER_PROFILE_DIR="@localstatedir@/nix/profiles/per-user/$USER"
66
export NIX_PROFILES="@localstatedir@/nix/profiles/default $HOME/.nix-profile"
77

88
if test -w $HOME; then
9-
if ! test -L $HOME/.nix-profile; then
10-
if test "$USER" != root; then
11-
ln -s $NIX_USER_PROFILE_DIR/profile $HOME/.nix-profile
12-
else
13-
# Root installs in the system-wide profile by default.
14-
ln -s @localstatedir@/nix/profiles/default $HOME/.nix-profile
15-
fi
16-
fi
17-
189
# Set up a default Nix expression from which to install stuff.
1910
if [ ! -e $HOME/.nix-defexpr -o -L $HOME/.nix-defexpr ]; then
2011
rm -f $HOME/.nix-defexpr

scripts/nix-profile.sh.in

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,6 @@ if [ -n "$HOME" ] && [ -n "$USER" ]; then
88
NIX_USER_PROFILE_DIR=@localstatedir@/nix/profiles/per-user/$USER
99

1010
if [ -w "$HOME" ]; then
11-
if ! [ -L "$NIX_LINK" ]; then
12-
echo "Nix: creating $NIX_LINK" >&2
13-
if [ "$USER" != root ]; then
14-
if ! ln -s "$NIX_USER_PROFILE_DIR"/profile "$NIX_LINK"; then
15-
echo "Nix: WARNING: could not create $NIX_LINK -> $NIX_USER_PROFILE_DIR/profile" >&2
16-
fi
17-
else
18-
# Root installs in the system-wide profile by default.
19-
ln -s @localstatedir@/nix/profiles/default "$NIX_LINK"
20-
fi
21-
fi
22-
2311
# Set up a default Nix expression from which to install stuff.
2412
__nix_defexpr="$HOME"/.nix-defexpr
2513
[ -L "$__nix_defexpr" ] && rm -f "$__nix_defexpr"

src/libstore/local-store.cc

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -70,16 +70,17 @@ LocalStore::LocalStore(const Params & params)
7070
createSymlink(profilesDir, gcRootsDir + "/profiles");
7171
}
7272

73+
for (auto & perUserDir : {profilesDir + "/per-user", gcRootsDir + "/per-user"}) {
74+
createDirs(perUserDir);
75+
if (chmod(perUserDir.c_str(), 0755) == -1)
76+
throw SysError("could not set permissions on '%s' to 755", perUserDir);
77+
}
78+
79+
createUser(getUserName(), getuid());
80+
7381
/* Optionally, create directories and set permissions for a
7482
multi-user install. */
7583
if (getuid() == 0 && settings.buildUsersGroup != "") {
76-
77-
for (auto & perUserDir : {profilesDir + "/per-user", gcRootsDir + "/per-user"}) {
78-
createDirs(perUserDir);
79-
if (chmod(perUserDir.c_str(), 0755) == -1)
80-
throw SysError("could not set permissions on '%s' to 755", perUserDir);
81-
}
82-
8384
mode_t perm = 01775;
8485

8586
struct group * gr = getgrnam(settings.buildUsersGroup.get().c_str());

src/libutil/util.cc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,16 @@ Path createTempDir(const Path & tmpRoot, const Path & prefix,
475475
}
476476

477477

478+
std::string getUserName()
479+
{
480+
auto pw = getpwuid(geteuid());
481+
std::string name = pw ? pw->pw_name : getEnv("USER", "");
482+
if (name.empty())
483+
throw Error("cannot figure out user name");
484+
return name;
485+
}
486+
487+
478488
static Lazy<Path> getHome2([]() {
479489
Path homeDir = getEnv("HOME");
480490
if (homeDir.empty()) {

src/libutil/util.hh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,8 @@ void deletePath(const Path & path, unsigned long long & bytesFreed);
126126
Path createTempDir(const Path & tmpRoot = "", const Path & prefix = "nix",
127127
bool includePid = true, bool useGlobalCounter = true, mode_t mode = 0755);
128128

129+
std::string getUserName();
130+
129131
/* Return $HOME or the user's home directory from /etc/passwd. */
130132
Path getHome();
131133

src/nix-channel/nix-channel.cc

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -159,13 +159,7 @@ static int _main(int argc, char ** argv)
159159
nixDefExpr = home + "/.nix-defexpr";
160160

161161
// Figure out the name of the channels profile.
162-
;
163-
auto pw = getpwuid(geteuid());
164-
std::string name = pw ? pw->pw_name : getEnv("USER", "");
165-
if (name.empty())
166-
throw Error("cannot figure out user name");
167-
profile = settings.nixStateDir + "/profiles/per-user/" + name + "/channels";
168-
createDirs(dirOf(profile));
162+
profile = fmt("%s/profiles/per-user/%s/channels", settings.nixStateDir, getUserName());
169163

170164
enum {
171165
cNone,

src/nix-env/nix-env.cc

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1425,9 +1425,14 @@ static int _main(int argc, char * * argv)
14251425

14261426
if (globals.profile == "") {
14271427
Path profileLink = getHome() + "/.nix-profile";
1428-
globals.profile = pathExists(profileLink)
1429-
? absPath(readLink(profileLink), dirOf(profileLink))
1430-
: canonPath(settings.nixStateDir + "/profiles/default");
1428+
if (!pathExists(profileLink)) {
1429+
replaceSymlink(
1430+
getuid() == 0
1431+
? settings.nixStateDir + "/profiles/default"
1432+
: fmt("%s/profiles/per-user/%s/profile", settings.nixStateDir, getUserName()),
1433+
profileLink);
1434+
}
1435+
globals.profile = absPath(readLink(profileLink), dirOf(profileLink));
14311436
}
14321437

14331438
op(globals, opFlags, opArgs);

tests/nix-channel.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ grep -q 'item.*attrPath="foo".*name="dependencies"' $TEST_ROOT/meta.xml
3636

3737
# Do an install.
3838
nix-env -i dependencies
39-
[ -e $TEST_ROOT/var/nix/profiles/default/foobar ]
39+
[ -e $TEST_HOME/.nix-profile/foobar ]
4040

4141
clearProfiles
4242
rm -f $TEST_HOME/.nix-channels
@@ -55,5 +55,5 @@ grep -q 'item.*attrPath="foo".*name="dependencies"' $TEST_ROOT/meta.xml
5555

5656
# Do an install.
5757
nix-env -i dependencies
58-
[ -e $TEST_ROOT/var/nix/profiles/default/foobar ]
58+
[ -e $TEST_HOME/.nix-profile/foobar ]
5959

tests/nix-profile.sh

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,3 @@ rm -rf $TEST_HOME $TEST_ROOT/profile-var
77
mkdir -p $TEST_HOME
88
USER=$user $SHELL -e -c ". $TEST_ROOT/nix-profile.sh; set"
99
USER=$user $SHELL -e -c ". $TEST_ROOT/nix-profile.sh" # test idempotency
10-
11-
[ -L $TEST_HOME/.nix-profile ]

0 commit comments

Comments
 (0)