1
0
Fork 0
mirror of https://github.com/LnL7/nix-darwin.git synced 2025-03-06 08:47:00 +00:00
nix-darwin/modules/system/launchd.nix

128 lines
4.7 KiB
Nix
Raw Normal View History

2016-12-02 08:34:05 +01:00
{ config, lib, pkgs, ... }:
with lib;
let
cfg = config.system;
2017-01-02 08:21:27 +01:00
text = import ../lib/write-text.nix {
2016-12-02 08:34:05 +01:00
inherit lib;
mkTextDerivation = pkgs.writeText;
};
2016-12-19 21:38:34 +01:00
launchdActivation = basedir: target: ''
2017-05-13 16:25:07 +02:00
if ! diff '${cfg.build.launchd}/Library/${basedir}/${target}' '/Library/${basedir}/${target}' &> /dev/null; then
if test -f '/Library/${basedir}/${target}'; then
echo "reloading service $(basename ${target} .plist)" >&2
launchctl unload -w '/Library/${basedir}/${target}' || true
else
echo "creating service $(basename ${target} .plist)" >&2
fi
cp -f '${cfg.build.launchd}/Library/${basedir}/${target}' '/Library/${basedir}/${target}'
2017-05-13 16:25:07 +02:00
launchctl load -w '/Library/${basedir}/${target}'
2016-12-19 21:38:34 +01:00
fi
'';
2017-01-25 22:35:06 +01:00
userLaunchdActivation = target: ''
2017-05-13 16:25:07 +02:00
if ! diff ${cfg.build.launchd}/user/Library/LaunchAgents/${target} ~/Library/LaunchAgents/${target} &> /dev/null; then
2017-05-11 23:43:05 +02:00
if test -f ~/Library/LaunchAgents/${target}; then
echo "reloading user service $(basename ${target} .plist)" >&2
2017-05-11 23:43:05 +02:00
launchctl unload -w ~/Library/LaunchAgents/${target} || true
else
echo "creating user service $(basename ${target} .plist)" >&2
fi
2017-05-11 23:43:05 +02:00
cp -f '${cfg.build.launchd}/user/Library/LaunchAgents/${target}' ~/Library/LaunchAgents/${target}
2017-05-13 16:25:07 +02:00
launchctl load -w ~/Library/LaunchAgents/${target}
2017-01-25 22:35:06 +01:00
fi
'';
2016-12-02 08:34:05 +01:00
launchAgents = filter (f: f.enable) (attrValues config.environment.launchAgents);
launchDaemons = filter (f: f.enable) (attrValues config.environment.launchDaemons);
2017-01-25 22:35:06 +01:00
userLaunchAgents = filter (f: f.enable) (attrValues config.environment.userLaunchAgents);
2016-12-02 08:34:05 +01:00
in
{
options = {
environment.launchAgents = mkOption {
type = types.loaOf (types.submodule text);
default = {};
description = ''
Set of files that have to be linked in <filename>/Library/LaunchAgents</filename>.
'';
};
environment.launchDaemons = mkOption {
type = types.loaOf (types.submodule text);
default = {};
description = ''
Set of files that have to be linked in <filename>/Library/LaunchDaemons</filename>.
'';
};
2017-01-25 22:35:06 +01:00
environment.userLaunchAgents = mkOption {
type = types.loaOf (types.submodule text);
default = {};
description = ''
Set of files that have to be linked in <filename>~/Library/LaunchAgents</filename>.
'';
};
2016-12-02 08:34:05 +01:00
};
config = {
system.build.launchd = pkgs.runCommand "launchd" {} ''
2017-05-11 23:43:05 +02:00
mkdir -p $out/Library/LaunchAgents $out/Library/LaunchDaemons $out/user/Library/LaunchAgents
2016-12-02 08:34:05 +01:00
cd $out/Library/LaunchAgents
${concatMapStringsSep "\n" (attr: "ln -s '${attr.source}' '${attr.target}'") launchAgents}
cd $out/Library/LaunchDaemons
${concatMapStringsSep "\n" (attr: "ln -s '${attr.source}' '${attr.target}'") launchDaemons}
2017-05-11 23:43:05 +02:00
cd $out/user/Library/LaunchAgents
2017-01-25 22:35:06 +01:00
${concatMapStringsSep "\n" (attr: "ln -s '${attr.source}' '${attr.target}'") userLaunchAgents}
'';
2016-12-02 08:34:05 +01:00
system.activationScripts.launchd.text = ''
# Set up launchd services in /Library/LaunchAgents and /Library/LaunchDaemons
echo "setting up launchd services..."
2016-12-19 21:38:34 +01:00
${concatMapStringsSep "\n" (attr: launchdActivation "LaunchAgents" attr.target) launchAgents}
${concatMapStringsSep "\n" (attr: launchdActivation "LaunchDaemons" attr.target) launchDaemons}
for f in $(ls /run/current-system/Library/LaunchAgents); do
if test ! -e "${cfg.build.launchd}/Library/LaunchAgents/$f"; then
echo "removing service $(basename $f .plist)" >&2
launchctl unload -w "/Library/LaunchAgents/$f" || true
if test -e "/Library/LaunchAgents/$f"; then rm -f "/Library/LaunchAgents/$f"; fi
fi
done
for f in $(ls /run/current-system/Library/LaunchDaemons); do
if test ! -e "${cfg.build.launchd}/Library/LaunchDaemons/$f"; then
echo "removing service $(basename $f .plist)" >&2
launchctl unload -w "/Library/LaunchDaemons/$f" || true
if test -e "/Library/LaunchDaemons/$f"; then rm -f "/Library/LaunchDaemons/$f"; fi
fi
done
2016-12-02 08:34:05 +01:00
'';
2017-01-25 22:35:06 +01:00
system.activationScripts.userLaunchd.text = ''
2017-05-11 23:43:05 +02:00
# Set up user launchd services in ~/Library/LaunchAgents
2017-01-25 22:35:06 +01:00
echo "setting up user launchd services..."
${concatMapStringsSep "\n" (attr: userLaunchdActivation attr.target) userLaunchAgents}
2017-05-11 23:43:05 +02:00
for f in $(ls /run/current-system/user/Library/LaunchAgents); do
if test ! -e "${cfg.build.launchd}/user/Library/LaunchAgents/$f"; then
echo "removing user service $(basename $f .plist)" >&2
2017-05-11 23:43:05 +02:00
launchctl unload -w ~/Library/LaunchAgents/$f || true
if test -e ~/Library/LaunchAgents/$f; then rm -f ~/Library/LaunchAgents/$f; fi
fi
done
2017-01-25 22:35:06 +01:00
'';
2016-12-02 08:34:05 +01:00
};
}