Skip to content

Commit

Permalink
Get effective user in Nix commands
Browse files Browse the repository at this point in the history
‘geteuid’ gives us the user that the command is being run as,
including in setuid modes. By using geteuid to determind id, we can
avoid the ‘sudo -i’ hack when upgrading Nix. So now, upgrading Nix on
macOS is as simple as:

$ sudo nix-channel --update
$ sudo nix-env -u

$ sudo launchctl stop org.nixos.nix-daemon
$ sudo launchctl start org.nixos.nix-daemon
or
$ sudo systemctl restart nix-daemon
  • Loading branch information
matthewbauer committed Sep 5, 2018
1 parent 5e0a720 commit 74f6d87
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/libutil/util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,7 @@ static Lazy<Path> getHome2([]() {
std::vector<char> buf(16384);
struct passwd pwbuf;
struct passwd * pw;
if (getpwuid_r(getuid(), &pwbuf, buf.data(), buf.size(), &pw) != 0
if (getpwuid_r(geteuid(), &pwbuf, buf.data(), buf.size(), &pw) != 0
|| !pw || !pw->pw_dir || !pw->pw_dir[0])
throw Error("cannot determine user's home directory");
homeDir = pw->pw_dir;
Expand Down
2 changes: 1 addition & 1 deletion src/nix-channel/nix-channel.cc
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ int main(int argc, char ** argv)

// Figure out the name of the channels profile.
;
auto pw = getpwuid(getuid());
auto pw = getpwuid(geteuid());
std::string name = pw ? pw->pw_name : getEnv("USER", "");
if (name.empty())
throw Error("cannot figure out user name");
Expand Down

0 comments on commit 74f6d87

Please sign in to comment.