Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: NixOS/nix
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 956f82bb6c09
Choose a base ref
...
head repository: NixOS/nix
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: c89cc9a521e6
Choose a head ref
  • 4 commits
  • 9 files changed
  • 1 contributor

Commits on Oct 9, 2019

  1. Copy the full SHA
    274624b View commit details
  2. Copy the full SHA
    94fa661 View commit details
  3. Copy the full SHA
    97b2898 View commit details
  4. Go back to 755 permission on per-user directories

    700 is pointless since the store is world-readable anyway. And
    per-user/root/channels must be world-readable.
    edolstra committed Oct 9, 2019
    Copy the full SHA
    c89cc9a View commit details
Showing with 46 additions and 77 deletions.
  1. +0 −21 scripts/nix-profile-daemon.sh.in
  2. +2 −27 scripts/nix-profile.sh.in
  3. +9 −8 src/libstore/local-store.cc
  4. +10 −0 src/libutil/util.cc
  5. +2 −0 src/libutil/util.hh
  6. +1 −7 src/nix-channel/nix-channel.cc
  7. +20 −10 src/nix-env/nix-env.cc
  8. +2 −2 tests/nix-channel.sh
  9. +0 −2 tests/nix-profile.sh
21 changes: 0 additions & 21 deletions scripts/nix-profile-daemon.sh.in
Original file line number Diff line number Diff line change
@@ -5,27 +5,6 @@ __ETC_PROFILE_NIX_SOURCED=1
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
mkdir -p $HOME/.nix-defexpr
if [ "$USER" != root ]; then
ln -s @localstatedir@/nix/profiles/per-user/root/channels $HOME/.nix-defexpr/channels_root
fi
fi
fi


# Set $NIX_SSL_CERT_FILE so that Nixpkgs applications like curl work.
if [ ! -z "${NIX_SSL_CERT_FILE:-}" ]; then
: # Allow users to override the NIX_SSL_CERT_FILE
29 changes: 2 additions & 27 deletions scripts/nix-profile.sh.in
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
if [ -n "$HOME" ] && [ -n "$USER" ]; then
__savedpath="$PATH"
export PATH=@coreutils@

# Set up the per-user profile.
# This part should be kept in sync with nixpkgs:nixos/modules/programs/shell.nix
@@ -9,29 +7,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"
mkdir -m 0755 -p "$__nix_defexpr"
if [ "$USER" != root ] && [ ! -L "$__nix_defexpr"/channels_root ]; then
ln -s @localstatedir@/nix/profiles/per-user/root/channels "$__nix_defexpr"/channels_root
fi
unset __nix_defexpr
fi

# Append ~/.nix-defexpr/channels to $NIX_PATH so that <nixpkgs>
# paths work when the user has fetched the Nixpkgs channel.
export NIX_PATH=${NIX_PATH:+$NIX_PATH:}$HOME/.nix-defexpr/channels
@@ -59,6 +34,6 @@ if [ -n "$HOME" ] && [ -n "$USER" ]; then
export MANPATH="$NIX_LINK/share/man:$MANPATH"
fi

export PATH="$NIX_LINK/bin:$__savedpath"
unset __savedpath NIX_LINK NIX_USER_PROFILE_DIR
export PATH="$NIX_LINK/bin:$PATH"
unset NIX_LINK NIX_USER_PROFILE_DIR
fi
17 changes: 9 additions & 8 deletions src/libstore/local-store.cc
Original file line number Diff line number Diff line change
@@ -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);
}

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());
@@ -1440,7 +1441,7 @@ void LocalStore::createUser(const std::string & userName, uid_t userId)
fmt("%s/gcroots/per-user/%s", stateDir, userName)
}) {
createDirs(dir);
if (chmod(dir.c_str(), 0700) == -1)
if (chmod(dir.c_str(), 0755) == -1)
throw SysError("changing permissions of directory '%s'", dir);
if (chown(dir.c_str(), userId, -1) == -1)
throw SysError("changing owner of directory '%s'", dir);
10 changes: 10 additions & 0 deletions src/libutil/util.cc
Original file line number Diff line number Diff line change
@@ -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()) {
2 changes: 2 additions & 0 deletions src/libutil/util.hh
Original file line number Diff line number Diff line change
@@ -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();

8 changes: 1 addition & 7 deletions src/nix-channel/nix-channel.cc
Original file line number Diff line number Diff line change
@@ -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,
30 changes: 20 additions & 10 deletions src/nix-env/nix-env.cc
Original file line number Diff line number Diff line change
@@ -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);
4 changes: 2 additions & 2 deletions tests/nix-channel.sh
Original file line number Diff line number Diff line change
@@ -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
@@ -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
Original file line number Diff line number Diff line change
@@ -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 ]