files: fix use of onChange with directory source

Previously, the comparison would not handle directory comparison
correctly, always finding that the source and target differed. This
would trigger the `onChange` script on each activation.

Fixes #2004

(cherry picked from commit 07ad6a4f76)
This commit is contained in:
Robert Helgesson 2021-05-20 22:12:45 +02:00
parent ffd4d9955b
commit 2f997178fa
No known key found for this signature in database
GPG key ID: 36BDAA14C2797E89
2 changed files with 25 additions and 1 deletions

View file

@ -39,6 +39,17 @@ in
};
config = {
assertions = [(
let
conflicts = mapAttrsToList (n: v: n)
(filterAttrs (n: v: v.recursive && v.onChange != "") cfg);
in {
assertion = conflicts == [];
message = ''
Cannot use 'home.file.<name>.onChange' when 'home.file.<name>.recursive' is enabled:
${concatStringsSep ", " conflicts}'';
})];
lib.file.mkOutOfStoreSymlink = path:
let
pathStr = toString path;
@ -236,12 +247,22 @@ in
home.activation.checkFilesChanged = hm.dag.entryBefore ["linkGeneration"] (
''
function _cmp() {
if [[ -d $1 && -d $2 ]]; then
diff -rq "$1" "$2" &> /dev/null
else
cmp --quiet "$1" "$2"
fi
}
declare -A changedFiles
'' + concatMapStrings (v: ''
cmp --quiet "${sourceStorePath v}" "${homeDirectory}/${v.target}" \
_cmp "${sourceStorePath v}" "${homeDirectory}/${v.target}" \
&& changedFiles["${v.target}"]=0 \
|| changedFiles["${v.target}"]=1
'') (filter (v: v.onChange != "") (attrValues cfg))
+ ''
unset -f _cmp
''
);
home.activation.onFilesChange = hm.dag.entryAfter ["linkGeneration"] (

View file

@ -82,6 +82,9 @@ with lib;
generations. The script will be run
<emphasis>after</emphasis> the new files have been linked
into place.
</para><para>
Note, this option cannot be used when <literal>recursive</literal>
is enabled.
'';
};