Skip to content

Commit

Permalink
nix-env: Create ~/.nix-profile automatically
Browse files Browse the repository at this point in the history
  • Loading branch information
edolstra committed Oct 9, 2019
1 parent 26762ce commit 9348f92
Show file tree
Hide file tree
Showing 9 changed files with 31 additions and 42 deletions.
9 changes: 0 additions & 9 deletions scripts/nix-profile-daemon.sh.in
Expand Up @@ -6,15 +6,6 @@ export NIX_USER_PROFILE_DIR="@localstatedir@/nix/profiles/per-user/$USER"
export NIX_PROFILES="@localstatedir@/nix/profiles/default $HOME/.nix-profile"

if test -w $HOME; then
if ! test -L $HOME/.nix-profile; then
if test "$USER" != root; then
ln -s $NIX_USER_PROFILE_DIR/profile $HOME/.nix-profile
else
# Root installs in the system-wide profile by default.
ln -s @localstatedir@/nix/profiles/default $HOME/.nix-profile
fi
fi

# Set up a default Nix expression from which to install stuff.
if [ ! -e $HOME/.nix-defexpr -o -L $HOME/.nix-defexpr ]; then
rm -f $HOME/.nix-defexpr
Expand Down
12 changes: 0 additions & 12 deletions scripts/nix-profile.sh.in
Expand Up @@ -10,18 +10,6 @@ if [ -n "$HOME" ] && [ -n "$USER" ]; then
NIX_USER_PROFILE_DIR=@localstatedir@/nix/profiles/per-user/$USER

if [ -w "$HOME" ]; then
if ! [ -L "$NIX_LINK" ]; then
echo "Nix: creating $NIX_LINK" >&2
if [ "$USER" != root ]; then
if ! ln -s "$NIX_USER_PROFILE_DIR"/profile "$NIX_LINK"; then
echo "Nix: WARNING: could not create $NIX_LINK -> $NIX_USER_PROFILE_DIR/profile" >&2
fi
else
# Root installs in the system-wide profile by default.
ln -s @localstatedir@/nix/profiles/default "$NIX_LINK"
fi
fi

# Set up a default Nix expression from which to install stuff.
__nix_defexpr="$HOME"/.nix-defexpr
[ -L "$__nix_defexpr" ] && rm -f "$__nix_defexpr"
Expand Down
15 changes: 8 additions & 7 deletions src/libstore/local-store.cc
Expand Up @@ -70,16 +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);

This comment has been minimized.

Copy link
@domenkozar

domenkozar Feb 4, 2020

Member

This sometimes fails on macos:

/nix/var/nix/profiles/default/bin/nix-build ci.nix
  error: could not set permissions on '/nix/var/nix/profiles/per-user' to 755: Operation not permitted

Is this ran by the daemon?

}

createUser(getUserName(), getuid());

/* Optionally, create directories and set permissions for a
multi-user install. */
if (getuid() == 0 && settings.buildUsersGroup != "") {

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);
}

mode_t perm = 01775;

struct group * gr = getgrnam(settings.buildUsersGroup.get().c_str());
Expand Down
10 changes: 10 additions & 0 deletions src/libutil/util.cc
Expand Up @@ -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()) {
Expand Down
2 changes: 2 additions & 0 deletions src/libutil/util.hh
Expand Up @@ -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();

Expand Down
8 changes: 1 addition & 7 deletions src/nix-channel/nix-channel.cc
Expand Up @@ -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,
Expand Down
11 changes: 8 additions & 3 deletions src/nix-env/nix-env.cc
Expand Up @@ -1422,9 +1422,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);
Expand Down
4 changes: 2 additions & 2 deletions tests/nix-channel.sh
Expand Up @@ -36,7 +36,7 @@ grep -q 'item.*attrPath="foo".*name="dependencies"' $TEST_ROOT/meta.xml

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

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

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

2 changes: 0 additions & 2 deletions tests/nix-profile.sh
Expand Up @@ -7,5 +7,3 @@ rm -rf $TEST_HOME $TEST_ROOT/profile-var
mkdir -p $TEST_HOME
USER=$user $SHELL -e -c ". $TEST_ROOT/nix-profile.sh; set"
USER=$user $SHELL -e -c ". $TEST_ROOT/nix-profile.sh" # test idempotency

[ -L $TEST_HOME/.nix-profile ]

0 comments on commit 9348f92

Please sign in to comment.