Skip to content

nix-darwin: login as the user when activating#587

Closed
kalbasit wants to merge 1 commit intonix-community:masterfrom
kalbasit:home-manager_login-as-user-before-activating
Closed

nix-darwin: login as the user when activating#587
kalbasit wants to merge 1 commit intonix-community:masterfrom
kalbasit:home-manager_login-as-user-before-activating

Conversation

@kalbasit
Copy link
Copy Markdown
Member

@kalbasit kalbasit commented Feb 21, 2019

Log in as the user when activating a home-manager configuration on Nix-Darwin. Otherwise, the $HOME is not set correctly and might inadvertently setup the HOME for the current user invoking the switch, although that might also fail with a permission denied error.

This pr depends on nix-darwin/nix-darwin#128

cc @ElvishJerricco

@kalbasit kalbasit changed the title [WIP] nix-darwin: login as the user when activating nix-darwin: login as the user when activating Feb 25, 2019
@kalbasit kalbasit marked this pull request as ready for review February 25, 2019 03:00
@rycee
Copy link
Copy Markdown
Member

rycee commented Mar 10, 2019

Rebased to master in 7ec1538 🙂

@rycee rycee closed this Mar 10, 2019
@kalbasit kalbasit deleted the home-manager_login-as-user-before-activating branch March 10, 2019 06:33
toonn added a commit to toonn/home-manager that referenced this pull request Jun 20, 2022
In nix-community#587, kalbasit introduce the `-i` flag so the sudo invocation would
run in an environment with `HOME` set to the correct value for the
target user. This was necessary to be able to set up multiple users
without interfering with the invoking user's `HOME`.

In nix-community#807, I switched to `-s` instead because I managed to get an invalid
shell set for my user by switching `useUserPackages` from `true` to
`false` which changes the location where packages are installed and
`~/.nix-profile/bin/<my-shell>` was no longer valid. This was based on
the assumption that `SHELL` would be set to some sensible value by Home
Manager at this point. This turned out to be false as reported in nix-community#2900.

In 0ced6d6 (this commit's parent at this time), I explicitly set
`SHELL` to `${pkgs.bash}` so it is definitely set to a good shell when
invoking the activation script.

However, nix-community#807 broke activation for multiple users, the original
motivation for `-i`, as reported in nix-community#2856. I fixed this in nix-community#2857 by
additionally passing `--set-home`.

Further discussion with rycee in nix-community#3040 made me realize that the
activation script already has a good Nix store bash shebang. So all the
problems have been caused, not by the shell used for the activation
script but by sudo trying to use a different shell at all. `-i` uses the
shell set in the `passwd` file for the target user, but this can become
invalid as happened to me. `-s` uses either `SHELL` if it's defined or
the invoking user's shell as set in the `passwd` file. By explicitly
setting this to a shell provided by Nix we make sure we're not trying to
launch a non-existent shell. However, we're clearly already running in
an existing shell and because of `--set-home` we can activate other
users properly so there's not actually any need to try to have sudo
start a different shell first, it just adds an extra process that then
goes on to run the activation script with a good bash because of the
shebang.

Dropping `-s` altogether and keeping `--set-home` should avoid all of
these issues.
rycee pushed a commit to toonn/home-manager that referenced this pull request Sep 19, 2022
In nix-community#587, kalbasit introduce the `-i` flag so the sudo invocation would
run in an environment with `HOME` set to the correct value for the
target user. This was necessary to be able to set up multiple users
without interfering with the invoking user's `HOME`.

In nix-community#807, I switched to `-s` instead because I managed to get an
invalid shell set for my user by switching `useUserPackages` from
`true` to `false` which changes the location where packages are
installed and `~/.nix-profile/bin/<my-shell>` was no longer valid.
This was based on the assumption that `SHELL` would be set to some
sensible value by Home Manager at this point. This turned out to be
false as reported in nix-community#2900.

In 0ced6d6 (this commit's parent at this time), I explicitly set
`SHELL` to `${pkgs.bash}` so it is definitely set to a good shell when
invoking the activation script.

However, nix-community#807 broke activation for multiple users, the original
motivation for `-i`, as reported in nix-community#2856. I fixed this in nix-community#2857 by
additionally passing `--set-home`.

Further discussion with rycee in nix-community#3040 made me realize that the
activation script already has a good Nix store bash shebang. So all
the problems have been caused, not by the shell used for the
activation script but by sudo trying to use a different shell at all.
`-i` uses the shell set in the `passwd` file for the target user, but
this can become invalid as happened to me. `-s` uses either `SHELL` if
it's defined or the invoking user's shell as set in the `passwd` file.
By explicitly setting this to a shell provided by Nix we make sure
we're not trying to launch a non-existent shell. However, we're
clearly already running in an existing shell and because of
`--set-home` we can activate other users properly so there's not
actually any need to try to have sudo start a different shell first,
it just adds an extra process that then goes on to run the activation
script with a good bash because of the shebang.

Dropping `-s` altogether and keeping `--set-home` should avoid all of
these issues.
austinharris pushed a commit to austinharris/home-manager that referenced this pull request Dec 23, 2022
In nix-community#587, kalbasit introduce the `-i` flag so the sudo invocation would
run in an environment with `HOME` set to the correct value for the
target user. This was necessary to be able to set up multiple users
without interfering with the invoking user's `HOME`.

In nix-community#807, I switched to `-s` instead because I managed to get an
invalid shell set for my user by switching `useUserPackages` from
`true` to `false` which changes the location where packages are
installed and `~/.nix-profile/bin/<my-shell>` was no longer valid.
This was based on the assumption that `SHELL` would be set to some
sensible value by Home Manager at this point. This turned out to be
false as reported in nix-community#2900.

In 0ced6d6 (this commit's parent at this time), I explicitly set
`SHELL` to `${pkgs.bash}` so it is definitely set to a good shell when
invoking the activation script.

However, nix-community#807 broke activation for multiple users, the original
motivation for `-i`, as reported in nix-community#2856. I fixed this in nix-community#2857 by
additionally passing `--set-home`.

Further discussion with rycee in nix-community#3040 made me realize that the
activation script already has a good Nix store bash shebang. So all
the problems have been caused, not by the shell used for the
activation script but by sudo trying to use a different shell at all.
`-i` uses the shell set in the `passwd` file for the target user, but
this can become invalid as happened to me. `-s` uses either `SHELL` if
it's defined or the invoking user's shell as set in the `passwd` file.
By explicitly setting this to a shell provided by Nix we make sure
we're not trying to launch a non-existent shell. However, we're
clearly already running in an existing shell and because of
`--set-home` we can activate other users properly so there's not
actually any need to try to have sudo start a different shell first,
it just adds an extra process that then goes on to run the activation
script with a good bash because of the shebang.

Dropping `-s` altogether and keeping `--set-home` should avoid all of
these issues.
spacekookie pushed a commit to spacekookie/home-manager that referenced this pull request Feb 10, 2023
In nix-community#587, kalbasit introduce the `-i` flag so the sudo invocation would
run in an environment with `HOME` set to the correct value for the
target user. This was necessary to be able to set up multiple users
without interfering with the invoking user's `HOME`.

In nix-community#807, I switched to `-s` instead because I managed to get an
invalid shell set for my user by switching `useUserPackages` from
`true` to `false` which changes the location where packages are
installed and `~/.nix-profile/bin/<my-shell>` was no longer valid.
This was based on the assumption that `SHELL` would be set to some
sensible value by Home Manager at this point. This turned out to be
false as reported in nix-community#2900.

In 0ced6d6 (this commit's parent at this time), I explicitly set
`SHELL` to `${pkgs.bash}` so it is definitely set to a good shell when
invoking the activation script.

However, nix-community#807 broke activation for multiple users, the original
motivation for `-i`, as reported in nix-community#2856. I fixed this in nix-community#2857 by
additionally passing `--set-home`.

Further discussion with rycee in nix-community#3040 made me realize that the
activation script already has a good Nix store bash shebang. So all
the problems have been caused, not by the shell used for the
activation script but by sudo trying to use a different shell at all.
`-i` uses the shell set in the `passwd` file for the target user, but
this can become invalid as happened to me. `-s` uses either `SHELL` if
it's defined or the invoking user's shell as set in the `passwd` file.
By explicitly setting this to a shell provided by Nix we make sure
we're not trying to launch a non-existent shell. However, we're
clearly already running in an existing shell and because of
`--set-home` we can activate other users properly so there's not
actually any need to try to have sudo start a different shell first,
it just adds an extra process that then goes on to run the activation
script with a good bash because of the shebang.

Dropping `-s` altogether and keeping `--set-home` should avoid all of
these issues.
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.

2 participants