1
0
Fork 0
mirror of https://github.com/nix-community/home-manager.git synced 2025-12-12 03:51:07 +01:00

home-environment: prevent overwriting existing files

This should reduce the risk of overwriting an existing file in the
user's home directory. A file will only be replaced if it is a link
pointing to a home-manager tree inside the Nix store.

If an existing file is detected an error is written indicating the
file's path and the activation will terminate before any mutation
occurs.

Fixes #6
This commit is contained in:
Robert Helgesson 2017-05-06 00:44:00 +02:00
parent 7e58b6bb35
commit 88ec7145ba
No known key found for this signature in database
GPG key ID: C3DB11069E65DC86
2 changed files with 81 additions and 21 deletions

View file

@ -246,6 +246,37 @@ in
# script's "check" and the "write" phases.
home.activation.writeBoundary = dagEntryAnywhere "";
# This verifies that the links we are about to create will not
# overwrite an existing file.
home.activation.checkLinkTargets = dagEntryBefore ["writeBoundary"] (
let
pattern = "-home-manager-files/";
check = pkgs.writeText "check" ''
newGenFiles="$1"
shift
for sourcePath in "$@" ; do
relativePath="''${sourcePath#$newGenFiles/}"
targetPath="$HOME/$relativePath"
if [[ -e "$targetPath" \
&& ! "$(readlink -e "$targetPath")" =~ "${pattern}" ]] ; then
echo -e "Existing file '$targetPath' is in the way"
collision=1
fi
done
if [[ -v collision ]] ; then
echo -e "Please move the above files and try again"
exit 1
fi
'';
in
''
newGenFiles="$(readlink -e "$newGenPath/home-files")"
find "$newGenFiles" -type f -print0 -or -type l -print0 \
| xargs -0 bash ${check} "$newGenFiles"
''
);
home.activation.linkGeneration = dagEntryAfter ["writeBoundary"] (
let
link = pkgs.writeText "link" ''