files: link home files instead of copying

Only copy files that need their execute bit changed or use the
deprecated `mode` option.
This commit is contained in:
Cornelius Mika 2017-11-06 10:28:50 +01:00 committed by Robert Helgesson
parent b8ddb11796
commit 9627fe6be6
No known key found for this signature in database
GPG key ID: C3DB11069E65DC86

View file

@ -264,36 +264,57 @@ in
home-files = pkgs.stdenv.mkDerivation { home-files = pkgs.stdenv.mkDerivation {
name = "home-manager-files"; name = "home-manager-files";
buildCommand = # Symlink directories and files that have the right execute bit.
"mkdir -p $out\n" + # Copy files that need their execute bit changed or use the
concatStringsSep "\n" ( # deprecated 'mode' option.
mapAttrsToList (n: v: buildCommand = ''
let mkdir -p $out
mode =
if v.mode != null
then v.mode
else
if v.executable != null
then (if v.executable then "+x" else "-x")
else "+r"; # Acts as a no-op.
in ''
target="$(realpath -m "$out/${v.target}")"
# Target file must be within $HOME. function insertFile() {
if [[ ! "$target" =~ "$out" ]] ; then local source="$1"
echo "Error installing file '${v.target}' outside \$HOME" >&2 local relTarget="$2"
exit 1 local executable="$3"
fi local mode="$4" # For backwards compatibility.
if [ -d "${v.source}" ]; then # Figure out the real absolute path to the target.
mkdir -p "$(dirname "$out/${v.target}")" local target
ln -s "${v.source}" "$target" target="$(realpath -m "$out/$relTarget")"
# Target path must be within $HOME.
if [[ ! $target =~ $out ]] ; then
echo "Error installing file '$relTarget' outside \$HOME" >&2
exit 1
fi
mkdir -p "$(dirname "$target")"
if [[ -d $source ]]; then
ln -s "$source" "$target"
elif [[ $mode ]]; then
install -m "$mode" "$source" "$target"
else
[[ -x $source ]] && isExecutable=1 || isExecutable=""
if [[ $executable == symlink || $isExecutable == $executable ]]; then
ln -s "$source" "$target"
else
cp "$source" "$target"
if [[ $executable ]]; then
chmod +x "$target"
else else
install -D -m${mode} "${v.source}" "$target" chmod -x "$target"
fi fi
'' fi
) cfg fi
); }
'' + concatStrings (
mapAttrsToList (n: v: ''
insertFile "${v.source}" \
"${v.target}" \
"${if v.executable == null
then "symlink"
else builtins.toString v.executable}" \
"${builtins.toString v.mode}"
'') cfg
);
}; };
}; };
} }