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

Nix installer updates shell profiles for the wrong user #189043

Closed
samuela opened this issue Aug 31, 2022 · 9 comments
Closed

Nix installer updates shell profiles for the wrong user #189043

samuela opened this issue Aug 31, 2022 · 9 comments

Comments

@samuela
Copy link
Member

samuela commented Aug 31, 2022

Describe the bug

When running the multi-user installer, everything proceeds without issue with the exception of the following warning:

warning: $HOME ('/home/sam.ainsworth') is not owned by you, falling back to the one defined in the 'passwd' file ('/root')

This is incorrect, however:

sam.ainsworth@cs-satze7x4cv-gpu-1661895675:~$ whoami
sam.ainsworth
sam.ainsworth@cs-satze7x4cv-gpu-1661895675:~$ echo $HOME
/home/sam.ainsworth
sam.ainsworth@cs-satze7x4cv-gpu-1661895675:~$ ls -al /home
total 20
drwxr-xr-x  5 root          root          4096 Aug 23 14:57 .
drwxr-xr-x 24 root          root          4096 Aug 30 17:19 ..
drwx--x--- 13 root          root          4096 Aug 30 17:18 docker
drwxr-xr-x 12 sam.ainsworth sam.ainsworth 4096 Aug 30 16:51 sam.ainsworth
drwx------  3 root          root          4096 Aug 23 14:57 ubuntu

Steps To Reproduce

Steps to reproduce the behavior:

    curl -L https://nixos.org/nix/install > /tmp/install-nix.sh
    # This is the hash at the time of writing (2022-08-30)
    { echo "df7446dab00d10117074f3bebb817d48  /tmp/install-nix.sh" | md5sum --check -; } && { yes | sh /tmp/install-nix.sh --daemon; }

Expected behavior

The installer to set up shell profiles for the correct user.

Screenshots

n/a

Additional context

n/a

Notify maintainers

Metadata

Please run nix-shell -p nix-info --run "nix-info -m" and paste the result.

 - system: `"x86_64-linux"`
 - host os: `Linux 5.4.0-1086-gcp, Ubuntu, 18.04.6 LTS (Bionic Beaver), nobuild`
 - multi-user?: `yes`
 - sandbox: `yes`
 - version: `nix-env (Nix) 2.11.0`
 - channels(root): `"nixpkgs"`
 - nixpkgs: `/nix/var/nix/profiles/per-user/root/channels/nixpkgs`

(can't figure out how to resolve NixOS/nix#3435 yet. may need to do a complete reinstall.)

@abathur
Copy link
Member

abathur commented Aug 31, 2022

It looks like this error message is in Nix itself, so this issue should probably get transferred there by someone with the power. The message was added fairly recently: NixOS/nix#6880

Perhaps @virusdave has a sense of what may be going on here, and whether it implies the PR is or isn't working as intended?

@virusdave
Copy link
Contributor

virusdave commented Aug 31, 2022

Interesting - so the issue at high level is that something is executing a core nix function having setuid to root but left the $HOME environment variable in place. This isn't inherently wrong, but prior to the change associated with the (original) warning message, it could have some surprising consequences.

I'd be happy to look over install-nix.sh to see what exactly it's doing that would cause this, and make sure it's innocuous (or fix it if not), but i don't see it in this repo and i'm not sure where it is located.

@abathur
Copy link
Member

abathur commented Aug 31, 2022

I'm guessing that this is in https://github.com/NixOS/nix/blob/ddb82ffda993d237d62d59578f7808a9d98c77fe/scripts/install-multi-user.sh#L866-L889 (though I suppose it could be other Nix invocations in the file).

@samuela Does this location look right to you?

@virusdave
Copy link
Contributor

I'm guessing that this is in https://github.com/NixOS/nix/blob/ddb82ffda993d237d62d59578f7808a9d98c77fe/scripts/install-multi-user.sh#L866-L889 (though I suppose it could be other Nix invocations in the file).

@samuela Does this location look right to you?

Yup, found it in that other repo! Thanks.

@samuela , do you happen to have any context logs from around the warning, to help me narrow down which of the script actions is happening when the warning is produced?

Thanks!

@samuela
Copy link
Member Author

samuela commented Aug 31, 2022

@samuela , do you happen to have any context logs from around the warning, to help me narrow down which of the script actions is happening when the warning is produced?

Yeah, I imagine that I would still have the logs somewhere. Where are the logs located?

@samuela Does this location look right to you?

I'm not 100% sure, but here is the context I see when running the installer:

...
---- sudo execution ------------------------------------------------------------
I am executing:

    $ sudo /nix/store/nmq5zcd93qb1yskx42rs910ff0247nn2-nix-2.11.0/bin/nix-store --load-db

to load data for the first time in to the Nix Database

warning: $HOME ('/home/sam.ainsworth') is not owned by you, falling back to the one defined in the 'passwd' file ('/root')
      Just finished getting the nix database ready.

~~> Setting up shell profiles: /etc/bashrc /etc/profile.d/nix.sh /etc/zshrc /etc/bash.bashrc /etc/zsh/zshrc

---- sudo execution ------------------------------------------------------------
...

@virusdave
Copy link
Contributor

Ha, that's exactly where i was suspecting, since it's the only obvious spot where a low-level nix command is being invoked without the HOME="$ROOT_HOME" guard like this

Thanks for confirming :)

The message should innocuous in this case, as $HOME (and values derived from it, like profile locations) aren't relevant to this operation. The simplest thing to do here is to add the HOME guard to the script for this operation in the same way it's done elsewhere. I'll submit a PR for this.

virusdave added a commit to virusdave/nix that referenced this issue Aug 31, 2022
A [recent-ish change](NixOS#6676) logs a warning when a potentially counterintuitive situation happens.

This now causes the multi-user installer to [emit a warning](NixOS/nixpkgs#189043) when it's doing
the "seed the Nix database" step via a low-level `nix-store --load-db` invocation.

`nix-store` functionality implementations don't actually use profiles or channels or homedir as far as i can tell.  So why are we 
hitting this code at all?  

Well, the current command approach for functionality here builds a [fat `nix` binary](https://github.com/NixOS/nix/blob/master/src/nix/local.mk#L23-L26) which has _all_ the functionality of
previous individual binaries (nix-env, nix-store, etc) bundled in, then [uses the invocation name](https://github.com/NixOS/nix/blob/master/src/nix/main.cc#L274-L277) to select the
set of commands to expose.  `nix` itself has this behavior, even when just trying to parse the (sub)command and arguments:

```
dave @ davembp2
$ nix
error: no subcommand specified
Try 'nix --help' for more information.

dave @ davembp2
$ sudo nix
warning: $HOME ('/Users/dave') is not owned by you, falling back to the one defined in the 'passwd' file
error: no subcommand specified
Try 'nix --help' for more information.

dave @ davembp2
$ HOME=~root sudo nix
error: no subcommand specified
Try 'nix --help' for more information.
```

This behavior can also be seen pretty easily with an arbitrary `nix-store` invocation:
```
dave @ davembp2 
$ nix-store --realize

dave @ davembp2 
$ sudo nix-store --realize  # what installer is doing now
warning: $HOME ('/Users/dave') is not owned by you, falling back to the one defined in the 'passwd' file

dave @ davembp2
$ sudo HOME=~root nix-store --realize  # what this PR effectively does

dave @ davembp2
$ 
```
@samuela
Copy link
Member Author

samuela commented Sep 1, 2022

Awesome, thanks @virusdave! It's exciting to see all the progress that's been happening on the installer recently

The message should innocuous in this case, as $HOME (and values derived from it, like profile locations) aren't relevant to this operation. The simplest thing to do here is to add the HOME guard to the script for this operation in the same way it's done elsewhere. I'll submit a PR for this.

I'm not seeing anything Nix-related in either my ~/.bashrc or ~/.profile... Is there any way this could still be messing something up there or is that likely to be a separate issue?

@virusdave
Copy link
Contributor

I'm not seeing anything Nix-related in either my ~/.bashrc or ~/.profile... Is there any way this could still be messing something up there or is that likely to be a separate issue?

That is almost certainly an unrelated issue, if you're seeing a problem. The guard I added in the PR doesn't change the behavior of the installer, it just prevents the warning by passing explicitly an configuration which was being inferred (and warned about) previously.

Minion3665 pushed a commit to Minion3665/nix that referenced this issue Feb 23, 2023
A [recent-ish change](NixOS#6676) logs a warning when a potentially counterintuitive situation happens.

This now causes the multi-user installer to [emit a warning](NixOS/nixpkgs#189043) when it's doing
the "seed the Nix database" step via a low-level `nix-store --load-db` invocation.

`nix-store` functionality implementations don't actually use profiles or channels or homedir as far as i can tell.  So why are we 
hitting this code at all?  

Well, the current command approach for functionality here builds a [fat `nix` binary](https://github.com/NixOS/nix/blob/master/src/nix/local.mk#L23-L26) which has _all_ the functionality of
previous individual binaries (nix-env, nix-store, etc) bundled in, then [uses the invocation name](https://github.com/NixOS/nix/blob/master/src/nix/main.cc#L274-L277) to select the
set of commands to expose.  `nix` itself has this behavior, even when just trying to parse the (sub)command and arguments:

```
dave @ davembp2
$ nix
error: no subcommand specified
Try 'nix --help' for more information.

dave @ davembp2
$ sudo nix
warning: $HOME ('/Users/dave') is not owned by you, falling back to the one defined in the 'passwd' file
error: no subcommand specified
Try 'nix --help' for more information.

dave @ davembp2
$ HOME=~root sudo nix
error: no subcommand specified
Try 'nix --help' for more information.
```

This behavior can also be seen pretty easily with an arbitrary `nix-store` invocation:
```
dave @ davembp2 
$ nix-store --realize

dave @ davembp2 
$ sudo nix-store --realize  # what installer is doing now
warning: $HOME ('/Users/dave') is not owned by you, falling back to the one defined in the 'passwd' file

dave @ davembp2
$ sudo HOME=~root nix-store --realize  # what this PR effectively does

dave @ davembp2
$ 
```
@samuela
Copy link
Member Author

samuela commented Mar 7, 2023

should be fixed by NixOS/nix#6980

@samuela samuela closed this as completed Mar 7, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants