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

Set HOME var to root's home when running nix-store as root #6980

Merged
merged 1 commit into from
Sep 5, 2022

Conversation

virusdave
Copy link
Contributor

A recent-ish change logs a warning when a potentially counterintuitive situation happens.

This now causes the multi-user installer to emit a warning 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 which has all the functionality of
previous individual binaries (nix-env, nix-store, etc) bundled in, then uses the invocation name 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
$ 

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

samuela commented Sep 1, 2022

Fixes NixOS/nixpkgs#189043

Copy link
Member

@thufschmitt thufschmitt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Just wondering whether it would make sense to have _sudo run sudo -H to generalize this. But that can be done independently anyways)

@thufschmitt thufschmitt merged commit 089f584 into NixOS:master Sep 5, 2022
Minion3665 pushed a commit to Minion3665/nix that referenced this pull request Feb 23, 2023
Set `HOME` var to `root`'s home when running `nix-store` as `root`
@nixos-discourse
Copy link

This pull request has been mentioned on NixOS Discourse. There might be relevant details there:

https://discourse.nixos.org/t/nix-install-script-not-allowed-to-set-env-variables/26104/2

@Ericson2314
Copy link
Member

Agreed sudo -H or even sudo -i across the board would be more appropriate.

@Ericson2314
Copy link
Member

Ericson2314 commented Mar 7, 2023

@virusdave Check out
#5638 (comment) for why this is happening in the first place.

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.

5 participants