1
0
Fork 0
mirror of https://github.com/LnL7/nix-darwin.git synced 2025-03-13 20:30:02 +00:00

launchd: unload and stop disabled services

This commit is contained in:
Daiderd Jordan 2017-05-11 21:21:39 +02:00
parent 3d17ec63f8
commit 93581740af
No known key found for this signature in database
GPG key ID: D02435D05B810C96
2 changed files with 42 additions and 15 deletions

View file

@ -8,8 +8,9 @@ let
cfg = config.system;
failedAssertions = map (x: x.message) (filter (x: !x.assertion) config.assertions);
home = builtins.getEnv "HOME";
failedAssertions = map (x: x.message) (filter (x: !x.assertion) config.assertions);
throwAssertions = res: if (failedAssertions != []) then throw "\nFailed assertions:\n${concatStringsSep "\n" (map (x: "- ${x}") failedAssertions)}" else res;
showWarnings = res: fold (w: x: builtins.trace "warning: ${w}" x) res config.warnings;
@ -95,6 +96,9 @@ in
ln -s ${cfg.build.launchd}/Library/LaunchAgents $out/Library/LaunchAgents
ln -s ${cfg.build.launchd}/Library/LaunchDaemons $out/Library/LaunchDaemons
mkdir -p $out/${home}/Library
ln -s ${cfg.build.launchd}/${home}/Library/LaunchAgents $out/${home}/Library/LaunchAgents
echo "$activationScript" > $out/activate
substituteInPlace $out/activate --subst-var out
chmod u+x $out/activate

View file

@ -6,25 +6,31 @@ let
cfg = config.system;
home = builtins.getEnv "HOME";
text = import ../lib/write-text.nix {
inherit lib;
mkTextDerivation = pkgs.writeText;
};
launchdActivation = basedir: target: ''
if test -f '/Library/${basedir}/${target}'; then
launchctl unload '/Library/${basedir}/${target}' || true
if ! diff '/run/current-system/Library/${basedir}/${target}' '/Library/${basedir}/${target}'; then
if test -f '/Library/${basedir}/${target}'; then
launchctl unload -w '/Library/${basedir}/${target}' || true
fi
cp -f '${cfg.build.launchd}/Library/${basedir}/${target}' '/Library/${basedir}/${target}'
launchctl load '/Library/${basedir}/${target}'
fi
cp -f '${cfg.build.launchd}/Library/${basedir}/${target}' '/Library/${basedir}/${target}'
launchctl load '/Library/${basedir}/${target}'
'';
userLaunchdActivation = target: ''
if test -f ~/Library/LaunchAgents/${target}; then
launchctl unload ~/Library/LaunchAgents/${target} || true
if ! diff '/run/current-system/${home}/Library/LaunchAgents/${target}' '${home}/Library/LaunchAgents/${target}'; then
if test -f '${home}/Library/LaunchAgents/${target}'; then
launchctl unload -w '${home}/Library/LaunchAgents/${target}' || true
fi
cp -f '${cfg.build.launchd}/${home}/Library/LaunchAgents/${target}' '${home}/Library/LaunchAgents/${target}'
launchctl load '${home}/Library/LaunchAgents/${target}'
fi
cp -f ${cfg.build.userLaunchd}/Library/LaunchAgents/${target} ~/Library/LaunchAgents/${target}
launchctl load ~/Library/LaunchAgents/${target}
'';
launchAgents = filter (f: f.enable) (attrValues config.environment.launchAgents);
@ -65,16 +71,12 @@ in
config = {
system.build.launchd = pkgs.runCommand "launchd" {} ''
mkdir -p $out/Library/LaunchAgents $out/Library/LaunchDaemons
mkdir -p $out/Library/LaunchAgents $out/Library/LaunchDaemons $out/${home}/Library/LaunchAgents
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}
'';
system.build.userLaunchd = pkgs.runCommand "user-launchd" {} ''
mkdir -p $out/Library/LaunchAgents $out/Library/LaunchDaemons
cd $out/Library/LaunchAgents
cd $out/${home}/Library/LaunchAgents
${concatMapStringsSep "\n" (attr: "ln -s '${attr.source}' '${attr.target}'") userLaunchAgents}
'';
@ -84,6 +86,20 @@ in
${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
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
launchctl unload -w "/Library/LaunchDaemons/$f" || true
if test -e "/Library/LaunchDaemons/$f"; then rm -f "/Library/LaunchDaemons/$f"; fi
fi
done
'';
system.activationScripts.userLaunchd.text = ''
@ -91,6 +107,13 @@ in
echo "setting up user launchd services..."
${concatMapStringsSep "\n" (attr: userLaunchdActivation attr.target) userLaunchAgents}
for f in $(ls /run/current-system/${home}/Library/LaunchAgents); do
if test ! -e "${cfg.build.launchd}/${home}/Library/LaunchAgents/$f"; then
launchctl unload -w "${home}/Library/LaunchAgents/$f" || true
if test -e "${home}/Library/LaunchAgents/$f"; then rm -f "${home}/Library/LaunchAgents/$f"; fi
fi
done
'';
};