summaryrefslogtreecommitdiff
path: root/modules/system
diff options
context:
space:
mode:
authorDaiderd Jordan <daiderd@gmail.com>2017-05-11 21:21:39 +0200
committerDaiderd Jordan <daiderd@gmail.com>2017-05-11 21:27:06 +0200
commit93581740afc1d7a8a2843bba6bb571567fe06411 (patch)
treebfa4651b21ca8932250b1fde0374653186665df3 /modules/system
parent3d17ec63f88c146cd2b28174a1a2c9d3a570e771 (diff)
launchd: unload and stop disabled services
Diffstat (limited to 'modules/system')
-rw-r--r--modules/system/default.nix6
-rw-r--r--modules/system/launchd.nix51
2 files changed, 42 insertions, 15 deletions
diff --git a/modules/system/default.nix b/modules/system/default.nix
index 16731ed..4e08c74 100644
--- a/modules/system/default.nix
+++ b/modules/system/default.nix
@@ -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
diff --git a/modules/system/launchd.nix b/modules/system/launchd.nix
index 8020a08..fae1be9 100644
--- a/modules/system/launchd.nix
+++ b/modules/system/launchd.nix
@@ -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
'';
};