Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't read HOME from environment #6622

Closed
wants to merge 1 commit into from
Closed

Conversation

mkenigs
Copy link
Contributor

@mkenigs mkenigs commented Jun 6, 2022

On darwin, sudo does not clear HOME, so HOME will still be set to a
user's home directory rather than root's.

util::getHome reads that value of HOME and is used in a number of
different places, which causes unexpected behavior in quite a few ways

See #1548 for some issues with
nix-channel

nix profile will symlink ~/.nix-profile to the default system profile

nix will create ~/.cache owned by root:
$ rm -r ~/.cache
$ sudo nix --extra-experimental-features "nix-command flakes" run nixpkgs#hello
Hello, world!
$ ls -ld ~/.cache
drwxr-xr-x 3 root staff 96 Jun 6 17:14 /Users/matthew/.cache

I'm guessing similar problems occur elsewhere getHome is used

On darwin, sudo does not clear HOME, so HOME will still be set to a
user's home directory rather than root's.

util::getHome reads that value of HOME and is used in a number of
different places, which causes unexpected behavior in quite a few ways

See NixOS#1548 for some issues with
nix-channel

nix profile will symlink ~/.nix-profile to the default system profile

nix will create ~/.cache owned by root:
$ rm -r ~/.cache
$ sudo nix --extra-experimental-features "nix-command flakes" run nixpkgs#hello
Hello, world!
$ ls -ld ~/.cache
drwxr-xr-x 3 root staff 96 Jun  6 17:14 /Users/matthew/.cache

I'm guessing similar problems occur elsewhere getHome is used
@mkenigs
Copy link
Contributor Author

mkenigs commented Jun 6, 2022

It is possible to just use sudo -H instead, so there is a workaround. Unless anyone is relying on overriding via HOME though it seems like it would be nicer for plain sudo to work

@edolstra
Copy link
Member

edolstra commented Jun 7, 2022

This will cause Nix to fail if the current user has no password entry, which could happen e.g. in a sandbox or user namespace. So I'm not sure this is a good idea.

@mkenigs
Copy link
Contributor Author

mkenigs commented Jun 7, 2022

Would these be some better options?

  1. use HOME if it is owned by current user, else fail
    • I think this is closest to preserving current behavior, but it would prevent users from doing something that's pretty clearly undesired like creating ~/.cache owned by root
    • Users would still have to use sudo -H on darwin
  2. use HOME if it is owned by current user, else check for passwd entry, else fail
    • I don't love that this means the same command will behave differently depending on who invoked it. For option 1 you'll at least get an error explaining why it's behaving differently
    • Users could use plain sudo
  3. check for passwd entry, if it doesn't exist use HOME if owned by current user, else fail
    • will still behave differently depending on the invoking user, but only if a user doesn't have a passwd entry

@virusdave
Copy link
Contributor

Ha, i was just about to start writing a similar patch. Personally i'd vote for option (3), and this was what i was about to implement.

virusdave added a commit to virusdave/nix that referenced this pull request Jun 15, 2022
…ME` second if necessary

Useful because a default `sudo` on darwin doesn't clear `$HOME`, so things like `sudo nix-channel --list`
will surprisingly return the USER'S channels, rather than `root`'s.

Other counterintuitive outcomes can be seen in this PR description:
  NixOS#6622
virusdave added a commit to virusdave/nix that referenced this pull request Jun 15, 2022
…ME` second if necessary

Useful because a default `sudo` on darwin doesn't clear `$HOME`, so things like `sudo nix-channel --list`
will surprisingly return the USER'S channels, rather than `root`'s.

Other counterintuitive outcomes can be seen in this PR description:
  NixOS#6622
virusdave added a commit to virusdave/nix that referenced this pull request Jun 15, 2022
…ME` second if necessary

Useful because a default `sudo` on darwin doesn't clear `$HOME`, so things like `sudo nix-channel --list`
will surprisingly return the USER'S channels, rather than `root`'s.

Other counterintuitive outcomes can be seen in this PR description:
  NixOS#6622
virusdave added a commit to virusdave/nix that referenced this pull request Jun 15, 2022
…ME` second if necessary

Useful because a default `sudo` on darwin doesn't clear `$HOME`, so things like `sudo nix-channel --list`
will surprisingly return the USER'S channels, rather than `root`'s.

Other counterintuitive outcomes can be seen in this PR description:
  NixOS#6622
virusdave added a commit to virusdave/nix that referenced this pull request Jun 15, 2022
…ME` second if necessary

Useful because a default `sudo` on darwin doesn't clear `$HOME`, so things like `sudo nix-channel --list`
will surprisingly return the USER'S channels, rather than `root`'s.

Other counterintuitive outcomes can be seen in this PR description:
  NixOS#6622
virusdave added a commit to virusdave/nix that referenced this pull request Jun 15, 2022
…ME` second if necessary

Useful because a default `sudo` on darwin doesn't clear `$HOME`, so things like `sudo nix-channel --list`
will surprisingly return the USER'S channels, rather than `root`'s.

Other counterintuitive outcomes can be seen in this PR description:
  NixOS#6622
virusdave added a commit to virusdave/nix that referenced this pull request Jun 16, 2022
Useful because a default `sudo` on darwin doesn't clear `$HOME`, so things like `sudo nix-channel --list`
will surprisingly return the USER'S channels, rather than `root`'s.

Other counterintuitive outcomes can be seen in this PR description:
  NixOS#6622
virusdave added a commit to virusdave/nix that referenced this pull request Jun 16, 2022
Useful because a default `sudo` on darwin doesn't clear `$HOME`, so things like `sudo nix-channel --list`
will surprisingly return the USER'S channels, rather than `root`'s.

Other counterintuitive outcomes can be seen in this PR description:
  NixOS#6622
virusdave added a commit to virusdave/nix that referenced this pull request Jun 16, 2022
Useful because a default `sudo` on darwin doesn't clear `$HOME`, so things like `sudo nix-channel --list`
will surprisingly return the USER'S channels, rather than `root`'s.

Other counterintuitive outcomes can be seen in this PR description:
  NixOS#6622
virusdave added a commit to virusdave/nix that referenced this pull request Jun 16, 2022
Useful because a default `sudo` on darwin doesn't clear `$HOME`, so things like `sudo nix-channel --list`
will surprisingly return the USER'S channels, rather than `root`'s.

Other counterintuitive outcomes can be seen in this PR description:
  NixOS#6622
virusdave added a commit to virusdave/nix that referenced this pull request Jun 17, 2022
Useful because a default `sudo` on darwin doesn't clear `$HOME`, so things like `sudo nix-channel --list`
will surprisingly return the USER'S channels, rather than `root`'s.

Other counterintuitive outcomes can be seen in this PR description:
  NixOS#6622
virusdave added a commit to virusdave/nix that referenced this pull request Jun 17, 2022
Useful because a default `sudo` on darwin doesn't clear `$HOME`, so things like `sudo nix-channel --list`
will surprisingly return the USER'S channels, rather than `root`'s.

Other counterintuitive outcomes can be seen in this PR description:
  NixOS#6622
virusdave added a commit to virusdave/nix that referenced this pull request Jun 17, 2022
Useful because a default `sudo` on darwin doesn't clear `$HOME`, so things like `sudo nix-channel --list`
will surprisingly return the USER'S channels, rather than `root`'s.

Other counterintuitive outcomes can be seen in this PR description:
  NixOS#6622
virusdave added a commit to virusdave/nix that referenced this pull request Jun 17, 2022
Useful because a default `sudo` on darwin doesn't clear `$HOME`, so things like `sudo nix-channel --list`
will surprisingly return the USER'S channels, rather than `root`'s.

Other counterintuitive outcomes can be seen in this PR description:
  NixOS#6622
virusdave added a commit to virusdave/nix that referenced this pull request Jun 17, 2022
Useful because a default `sudo` on darwin doesn't clear `$HOME`, so things like `sudo nix-channel --list`
will surprisingly return the USER'S channels, rather than `root`'s.

Other counterintuitive outcomes can be seen in this PR description:
  NixOS#6622
virusdave added a commit to virusdave/nix that referenced this pull request Jun 17, 2022
Useful because a default `sudo` on darwin doesn't clear `$HOME`, so things like `sudo nix-channel --list`
will surprisingly return the USER'S channels, rather than `root`'s.

Other counterintuitive outcomes can be seen in this PR description:
  NixOS#6622
virusdave added a commit to virusdave/nix that referenced this pull request Jun 17, 2022
Useful because a default `sudo` on darwin doesn't clear `$HOME`, so things like `sudo nix-channel --list`
will surprisingly return the USER'S channels, rather than `root`'s.

Other counterintuitive outcomes can be seen in this PR description:
  NixOS#6622
virusdave added a commit to virusdave/nix that referenced this pull request Jun 17, 2022
Useful because a default `sudo` on darwin doesn't clear `$HOME`, so things like `sudo nix-channel --list`
will surprisingly return the USER'S channels, rather than `root`'s.

Other counterintuitive outcomes can be seen in this PR description:
  NixOS#6622
virusdave added a commit to virusdave/nix that referenced this pull request Jun 17, 2022
Useful because a default `sudo` on darwin doesn't clear `$HOME`, so things like `sudo nix-channel --list`
will surprisingly return the USER'S channels, rather than `root`'s.

Other counterintuitive outcomes can be seen in this PR description:
  NixOS#6622
@mkenigs
Copy link
Contributor Author

mkenigs commented Jun 20, 2022

Closing as #6676 was merged

@mkenigs mkenigs closed this Jun 20, 2022
virusdave added a commit to virusdave/nix that referenced this pull request Aug 7, 2022
Useful because a default `sudo` on darwin doesn't clear `$HOME`, so things like `sudo nix-channel --list`
will surprisingly return the USER'S channels, rather than `root`'s.

Other counterintuitive outcomes can be seen in this PR description:
  NixOS#6622
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants