1
0
Fork 0
mirror of https://github.com/nix-community/home-manager synced 2024-11-26 21:19:45 +01:00

files: clean up

Specifically, replace the `link` and `clean` scripts by inline Bash
functions. This avoids a number of Bash invocations and simplifies the
code a little. This should also make it slightly easier to implement
support for arbitrary file locations.
This commit is contained in:
Robert Helgesson 2021-07-26 23:57:04 +02:00
parent f6f6990fc8
commit 8a1297444b
No known key found for this signature in database
GPG key ID: 36BDAA14C2797E89

View file

@ -106,7 +106,8 @@ in
$VERBOSE_ECHO "Skipping collision check for $targetPath"
elif [[ -e "$targetPath" \
&& ! "$(readlink "$targetPath")" == $homeFilePattern ]] ; then
if [[ ! -L "$targetPath" && -n "$HOME_MANAGER_BACKUP_EXT" ]] ; then
if [[ ! -L "$targetPath" \
&& -v HOME_MANAGER_BACKUP_EXT && -n "$HOME_MANAGER_BACKUP_EXT" ]] ; then
backup="$targetPath.$HOME_MANAGER_BACKUP_EXT"
if [[ -e "$backup" ]]; then
errorEcho "Existing file '$backup' would be clobbered by backing up '$targetPath'"
@ -160,34 +161,16 @@ in
# and a failure during the intermediate state FA ∩ FB will not
# result in lost links because this set of links are in both the
# source and target generation.
home.activation.linkGeneration = hm.dag.entryAfter ["writeBoundary"] (
let
link = pkgs.writeShellScript "link" ''
newGenFiles="$1"
shift
for sourcePath in "$@" ; do
relativePath="''${sourcePath#$newGenFiles/}"
targetPath="$HOME/$relativePath"
if [[ -e "$targetPath" && ! -L "$targetPath" && -n "$HOME_MANAGER_BACKUP_EXT" ]] ; then
backup="$targetPath.$HOME_MANAGER_BACKUP_EXT"
$DRY_RUN_CMD mv $VERBOSE_ARG "$targetPath" "$backup" || errorEcho "Moving '$targetPath' failed!"
fi
$DRY_RUN_CMD mkdir -p $VERBOSE_ARG "$(dirname "$targetPath")"
$DRY_RUN_CMD ln -nsf $VERBOSE_ARG "$sourcePath" "$targetPath"
done
'';
cleanup = pkgs.writeShellScript "cleanup" ''
. ${./lib-bash/color-echo.sh}
home.activation.linkGeneration = hm.dag.entryAfter ["writeBoundary"] (''
function clean() {
# A symbolic link whose target path matches this pattern will be
# considered part of a Home Manager generation.
local homeFilePattern
homeFilePattern="$(readlink -e ${escapeShellArg builtins.storeDir})/*-home-manager-files/*"
newGenFiles="$1"
shift 1
for relativePath in "$@" ; do
targetPath="$HOME/$relativePath"
local newGenFiles="$1"
local relativePath="$2"
local targetPath="$HOME/$relativePath"
if [[ -e "$newGenFiles/$relativePath" ]] ; then
$VERBOSE_ECHO "Checking $targetPath: exists"
elif [[ ! "$(readlink "$targetPath")" == $homeFilePattern ]] ; then
@ -211,17 +194,6 @@ in
popd > /dev/null
fi
fi
done
'';
in
''
function linkNewGen() {
echo "Creating home file links in $HOME"
local newGenFiles
newGenFiles="$(readlink -e "$newGenPath/home-files")"
find "$newGenFiles" \( -type f -or -type l \) \
-exec bash ${link} "$newGenFiles" {} +
}
function cleanOldGen() {
@ -239,7 +211,35 @@ in
# generation. The find command below will print the
# relative path of the entry.
find "$oldGenFiles" '(' -type f -or -type l ')' -printf '%P\0' \
| xargs -0 bash ${cleanup} "$newGenFiles"
| while IFS= read -r -d "" relativePath ; do \
clean "$newGenFiles" "$relativePath"; \
done
}
function link() {
local newGenFiles="$1"
local sourcePath="$2"
local relativePath="''${sourcePath#$newGenFiles/}"
local targetPath="$HOME/$relativePath"
if [[ -e "$targetPath" && ! -L "$targetPath" \
&& -v HOME_MANAGER_BACKUP_EXT && -n "$HOME_MANAGER_BACKUP_EXT" ]] ; then
local backup="$targetPath.$HOME_MANAGER_BACKUP_EXT"
$DRY_RUN_CMD mv $VERBOSE_ARG "$targetPath" "$backup" \
|| errorEcho "Moving '$targetPath' failed!"
fi
$DRY_RUN_CMD mkdir -p $VERBOSE_ARG "$(dirname "$targetPath")"
$DRY_RUN_CMD ln -nsf $VERBOSE_ARG "$sourcePath" "$targetPath"
}
function linkNewGen() {
echo "Creating home file links in $HOME"
local newGenFiles
newGenFiles="$(readlink -e "$newGenPath/home-files")"
find "$newGenFiles" \( -type f -or -type l \) -print0 \
| while IFS= read -r -d "" sourcePath ; do \
link "$newGenFiles" "$sourcePath"; \
done
}
cleanOldGen
@ -253,8 +253,9 @@ in
fi
linkNewGen
''
);
unset -f clean cleanOldGen link linkNewGen
'');
home.activation.checkFilesChanged = hm.dag.entryBefore ["linkGeneration"] (
let