Skip to content

Commit

Permalink
Make multi-user installer fully idempotent
Browse files Browse the repository at this point in the history
Fixes #7215, #7069 and #6617
  • Loading branch information
iFreilicht committed Jan 31, 2023
1 parent db90cf4 commit d689881
Showing 1 changed file with 33 additions and 14 deletions.
47 changes: 33 additions & 14 deletions scripts/install-multi-user.sh
Original file line number Diff line number Diff line change
Expand Up @@ -434,19 +434,30 @@ $(uninstall_directions)
EOF
fi

# TODO: I think it would be good for this step to accumulate more
# knowledge of older obsolete artifacts, if there are any.
# We could issue a "reminder" here that the user might want
# to clean them up?

problematic_profile_targets=()
for profile_target in "${PROFILE_TARGETS[@]}"; do
# TODO: I think it would be good to accumulate a list of all
# of the copies so that people don't hit this 2 or 3x in
# a row for different files.
if [ -e "$profile_target$PROFILE_BACKUP_SUFFIX" ]; then
# this backup process first released in Nix 2.1
failure <<EOF
# this backup process first released in Nix 2.1
if [ -e "$profile_target$PROFILE_BACKUP_SUFFIX" ] && ! contains_source_lines "$profile_target"; then
# Identical backup and original happens especially on macOS as it overwrites /etc/zshrc on every update
if cmp -s "$profile_target" "$profile_target$PROFILE_BACKUP_SUFFIX"; then
rm "$profile_target$PROFILE_BACKUP_SUFFIX"
else
problematic_profile_targets+=("$profile_target")
fi
fi
done

if [ "${PROFILE_TARGETS[@]}" ]; then
resolution_explanation=$(cat <<EOF
I back up shell profile/rc scripts before I add Nix to them.
However, at least one of them was already backed up, so you have to clean up manually.
EOF
)

for profile_target in "${problematic_profile_targets[@]}"; do
resolution_explanation+=$(cat <<EOF
I need to back up $profile_target to $profile_target$PROFILE_BACKUP_SUFFIX,
but the latter already exists.
Expand All @@ -463,8 +474,11 @@ Here's how to clean up the old backup file:
$profile_target$PROFILE_BACKUP_SUFFIX doesn't mention Nix, run:
mv $profile_target$PROFILE_BACKUP_SUFFIX $profile_target
EOF
fi
done
)
done

failure "$resolution_explanation"
fi

if is_os_linux && [ ! -e /run/systemd/system ]; then
warning <<EOF
Expand Down Expand Up @@ -851,6 +865,11 @@ end
EOF
}

contains_source_lines() {
# This works for both POSIX shells and fish
grep -q "# Nix" "$1" && grep -q "# End Nix" "$1"
}

configure_shell_profile() {
task "Setting up shell profiles: ${PROFILE_TARGETS[*]}"
for profile_target in "${PROFILE_TARGETS[@]}"; do
Expand All @@ -866,7 +885,7 @@ configure_shell_profile() {
fi
fi

if [ -e "$profile_target" ]; then
if [ -e "$profile_target" ] && ! contains_source_lines "$profile_target"; then
shell_source_lines \
| _sudo "extend your $profile_target with nix-daemon settings" \
tee -a "$profile_target"
Expand Down

0 comments on commit d689881

Please sign in to comment.