files: add special handling of systemd files

Unfortunately systemd derives nonsensical unit names when the unit
file is a link to a link to a file. This commit ensures that any file
whose target path matches the pattern `*/systemd/user/*` will be
reachable with only one link hop.

This also reverts f52ec0df7c, which
contained a temporary fix. This commit is an improvements in that it
is more explicit and also handles unit files given directly as a home
file source.
This commit is contained in:
Robert Helgesson 2017-11-12 00:49:36 +01:00
parent d7537777c3
commit 7a9c873093
No known key found for this signature in database
GPG key ID: C3DB11069E65DC86
2 changed files with 20 additions and 5 deletions

View file

@ -225,11 +225,26 @@ in
install -m "$mode" "$source" "$target"
else
[[ -x $source ]] && isExecutable=1 || isExecutable=""
if [[ $executable == inherit || $isExecutable == $executable ]]; then
# Link the file into the home file directory if possible,
# i.e., if the executable bit of the source is the same we
# expect for the target. Otherwise, we copy the file and
# set the executable bit to the expected value.
#
# Note, as a special case we always copy systemd units
# because it derives the unit name from the ultimate link
# target, which may be a store path with the hash
# included.
if [[ ($executable == inherit || $isExecutable == $executable) \
&& $relTarget != *"/systemd/user/"* ]]; then
ln -s "$source" "$target"
else
cp "$source" "$target"
if [[ $executable ]]; then
if [[ $executable == inherit ]]; then
# Don't change file mode if it should match the source.
:
elif [[ $executable ]]; then
chmod +x "$target"
else
chmod -x "$target"

View file

@ -24,17 +24,17 @@ let
buildService = style: name: serviceCfg:
let
source = pkgs.writeScript "${name}.${style}" (toSystemdIni serviceCfg);
source = pkgs.writeText "${name}.${style}" (toSystemdIni serviceCfg);
wantedBy = target:
{
name = "systemd/user/${target}.wants/${name}.${style}";
value = { inherit source; executable = false; };
value = { inherit source; };
};
in
singleton {
name = "systemd/user/${name}.${style}";
value = { inherit source; executable = false; };
value = { inherit source; };
}
++
map wantedBy (serviceCfg.Install.WantedBy or []);