mirror of
https://github.com/LnL7/nix-darwin.git
synced 2024-12-14 11:57:34 +00:00
parent
d6414dbcce
commit
33846d2ac6
1 changed files with 27 additions and 29 deletions
|
@ -26,11 +26,13 @@ showSyntax() {
|
||||||
if command sudo --help | grep -- --preserve-env= >/dev/null; then
|
if command sudo --help | grep -- --preserve-env= >/dev/null; then
|
||||||
# We use `env` before our command to ensure the preserved PATH gets checked
|
# We use `env` before our command to ensure the preserved PATH gets checked
|
||||||
# when trying to resolve the command to execute
|
# when trying to resolve the command to execute
|
||||||
sudo="command sudo -H --preserve-env=PATH --preserve-env=SSH_CONNECTION"
|
sudo_base="command sudo -H --preserve-env=PATH --preserve-env=SSH_CONNECTION"
|
||||||
|
sudo="$sudo_base env"
|
||||||
else
|
else
|
||||||
sudo="command sudo -H"
|
sudo_base="command sudo -H"
|
||||||
|
sudo="$sudo_base"
|
||||||
fi
|
fi
|
||||||
sudo() { $sudo env "$@"; }
|
sudo() { $sudo "$@"; }
|
||||||
|
|
||||||
# Parse the command line.
|
# Parse the command line.
|
||||||
origArgs=("$@")
|
origArgs=("$@")
|
||||||
|
@ -146,28 +148,6 @@ done
|
||||||
|
|
||||||
if [ -z "$action" ]; then showSyntax; fi
|
if [ -z "$action" ]; then showSyntax; fi
|
||||||
|
|
||||||
# We currently need to invoke `activate-user` as a non-root user,
|
|
||||||
# but need to be root for other actions such as switching profiles and
|
|
||||||
# running `activate`. To avoid prompting for a password multiple times,
|
|
||||||
# if we need to perform one of these actions we preemptively re-invoke
|
|
||||||
# ourselves as root but remember the user who called us so that we can
|
|
||||||
# later drop down to that user when invoking `activate-user`.
|
|
||||||
if [ "$USER" = root ]; then
|
|
||||||
if [[ -z "$NIX_DARWIN_PRIMARY_USER" ]]; then
|
|
||||||
echo "$0: must be invoked as a non-root user"
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
else
|
|
||||||
case $action in
|
|
||||||
edit|switch|activate|rollback|list)
|
|
||||||
sudo NIX_DARWIN_PRIMARY_USER="$USER" @out@/bin/darwin-rebuild "${origArgs[@]}"
|
|
||||||
exit $?
|
|
||||||
;;
|
|
||||||
esac
|
|
||||||
fi
|
|
||||||
|
|
||||||
# From here on, if we are in the case list above, we are running as root.
|
|
||||||
|
|
||||||
flakeFlags=(--extra-experimental-features 'nix-command flakes')
|
flakeFlags=(--extra-experimental-features 'nix-command flakes')
|
||||||
|
|
||||||
# Use /etc/nix-darwin/flake.nix if it exists. It can be a symlink to the
|
# Use /etc/nix-darwin/flake.nix if it exists. It can be a symlink to the
|
||||||
|
@ -219,7 +199,11 @@ if [ "$action" = switch ] || [ "$action" = build ] || [ "$action" = check ] || [
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if [ "$action" = list ] || [ "$action" = rollback ]; then
|
if [ "$action" = list ] || [ "$action" = rollback ]; then
|
||||||
nix-env -p "$profile" "${extraProfileFlags[@]}"
|
if [ "$USER" != root ] && [ ! -w $(dirname "$profile") ]; then
|
||||||
|
sudo nix-env -p "$profile" "${extraProfileFlags[@]}"
|
||||||
|
else
|
||||||
|
nix-env -p "$profile" "${extraProfileFlags[@]}"
|
||||||
|
fi
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if [ "$action" = rollback ]; then
|
if [ "$action" = rollback ]; then
|
||||||
|
@ -233,12 +217,26 @@ fi
|
||||||
if [ -z "$systemConfig" ]; then exit 0; fi
|
if [ -z "$systemConfig" ]; then exit 0; fi
|
||||||
|
|
||||||
if [ "$action" = switch ]; then
|
if [ "$action" = switch ]; then
|
||||||
nix-env -p "$profile" --set "$systemConfig"
|
if [ "$USER" != root ] && [ ! -w $(dirname "$profile") ]; then
|
||||||
|
sudo nix-env -p "$profile" --set "$systemConfig"
|
||||||
|
else
|
||||||
|
nix-env -p "$profile" --set "$systemConfig"
|
||||||
|
fi
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if [ "$action" = switch ] || [ "$action" = activate ] || [ "$action" = rollback ]; then
|
if [ "$action" = switch ] || [ "$action" = activate ] || [ "$action" = rollback ]; then
|
||||||
$sudo -u "$NIX_DARWIN_PRIMARY_USER" "$systemConfig/activate-user"
|
if [ "$USER" != root ]; then
|
||||||
"$systemConfig/activate"
|
# Running `$systemConfig/activate-user` may cause subsequent sudo invocations to prompt
|
||||||
|
# for the password even if we previously ran sudo in the same session, since `brew` resets
|
||||||
|
# the sudo timestamp: see https://github.com/Homebrew/brew/pull/17694.
|
||||||
|
# Indeed, by this point we've already run `nix-env --set` as sudo. So to avoid prompting a
|
||||||
|
# second time, we become root *before* running activate-user and then drop down to the user
|
||||||
|
# to invoke it. This way, the call to `activate` doesn't require a password.
|
||||||
|
sudo @shell@ -c "$sudo_base -u $USER $systemConfig/activate-user && $systemConfig/activate"
|
||||||
|
else
|
||||||
|
"$systemConfig/activate-user"
|
||||||
|
"$systemConfig/activate"
|
||||||
|
fi
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if [ "$action" = changelog ]; then
|
if [ "$action" = changelog ]; then
|
||||||
|
|
Loading…
Reference in a new issue