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