1
0
Fork 0
mirror of https://github.com/LnL7/nix-darwin.git synced 2025-03-15 13:07:45 +00:00
nix-darwin/modules/system/launchd.nix
Emily e65131e69c treewide: convert all option docs to Markdown
This process was automated by [my fork of `nix-doc-munge`]; thanks
to @pennae for writing this tool! It automatically checks that the
resulting documentation doesn't change, although my fork loosens
this a little to ignore some irrelevant whitespace and typographical
differences.

As of this commit there is no DocBook remaining in the options
documentation.

You can play along at home if you want to reproduce this commit:

    $ NIX_PATH=nixpkgs=flake:nixpkgs/c1bca7fe84c646cfd4ebf3482c0e6317a0b13f22 \
      nix shell nixpkgs#coreutils \
      -c find . -name '*.nix' \
      -exec nix run github:emilazy/nix-doc-munge/0a7190f600027bf7baf6cb7139e4d69ac2f51062 \
      {} +

[my fork of `nix-doc-munge`]: https://github.com/emilazy/nix-doc-munge
2023-06-24 10:48:55 +01:00

146 lines
5.4 KiB
Nix

{ config, lib, pkgs, ... }:
with lib;
let
cfg = config.system;
text = import ../lib/write-text.nix {
inherit lib;
mkTextDerivation = pkgs.writeText;
};
launchdVariables = mapAttrsToList (name: value: ''
launchctl setenv ${name} '${value}'
'');
launchdActivation = basedir: target: ''
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 '/Library/${basedir}/${target}' || true
else
echo "creating service $(basename ${target} .plist)" >&2
fi
if test -L '/Library/${basedir}/${target}'; then
rm '/Library/${basedir}/${target}'
fi
cp -f '${cfg.build.launchd}/Library/${basedir}/${target}' '/Library/${basedir}/${target}'
launchctl load -w '/Library/${basedir}/${target}'
fi
'';
userLaunchdActivation = target: ''
if ! diff ${cfg.build.launchd}/user/Library/LaunchAgents/${target} ~/Library/LaunchAgents/${target} &> /dev/null; then
if test -f ~/Library/LaunchAgents/${target}; then
echo "reloading user service $(basename ${target} .plist)" >&2
launchctl unload ~/Library/LaunchAgents/${target} || true
else
echo "creating user service $(basename ${target} .plist)" >&2
fi
if test -L ~/Library/LaunchAgents/${target}; then
rm ~/Library/LaunchAgents/${target}
fi
cp -f '${cfg.build.launchd}/user/Library/LaunchAgents/${target}' ~/Library/LaunchAgents/${target}
launchctl load -w ~/Library/LaunchAgents/${target}
fi
'';
launchAgents = filter (f: f.enable) (attrValues config.environment.launchAgents);
launchDaemons = filter (f: f.enable) (attrValues config.environment.launchDaemons);
userLaunchAgents = filter (f: f.enable) (attrValues config.environment.userLaunchAgents);
in
{
options = {
environment.launchAgents = mkOption {
type = types.attrsOf (types.submodule text);
default = { };
description = lib.mdDoc ''
Set of files that have to be linked in {file}`/Library/LaunchAgents`.
'';
};
environment.launchDaemons = mkOption {
type = types.attrsOf (types.submodule text);
default = { };
description = lib.mdDoc ''
Set of files that have to be linked in {file}`/Library/LaunchDaemons`.
'';
};
environment.userLaunchAgents = mkOption {
type = types.attrsOf (types.submodule text);
default = { };
description = lib.mdDoc ''
Set of files that have to be linked in {file}`~/Library/LaunchAgents`.
'';
};
};
config = {
system.build.launchd = pkgs.runCommand "launchd"
{ preferLocalBuild = true; }
''
mkdir -p $out/Library/LaunchAgents $out/Library/LaunchDaemons $out/user/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}
cd $out/user/Library/LaunchAgents
${concatMapStringsSep "\n" (attr: "ln -s '${attr.source}' '${attr.target}'") userLaunchAgents}
'';
system.activationScripts.launchd.text = ''
# Set up launchd services in /Library/LaunchAgents and /Library/LaunchDaemons
echo "setting up launchd services..." >&2
${concatStringsSep "\n" (launchdVariables config.launchd.envVariables)}
${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 2> /dev/null); do
if test ! -e "${cfg.build.launchd}/Library/LaunchAgents/$f"; then
echo "removing service $(basename $f .plist)" >&2
launchctl unload "/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 2> /dev/null); do
if test ! -e "${cfg.build.launchd}/Library/LaunchDaemons/$f"; then
echo "removing service $(basename $f .plist)" >&2
launchctl unload "/Library/LaunchDaemons/$f" || true
if test -e "/Library/LaunchDaemons/$f"; then rm -f "/Library/LaunchDaemons/$f"; fi
fi
done
'';
system.activationScripts.userLaunchd.text = ''
# Set up user launchd services in ~/Library/LaunchAgents
echo "setting up user launchd services..."
${concatStringsSep "\n" (launchdVariables config.launchd.user.envVariables)}
${optionalString (builtins.length userLaunchAgents > 0) ''
mkdir -p ~/Library/LaunchAgents
''}
${concatMapStringsSep "\n" (attr: userLaunchdActivation attr.target) userLaunchAgents}
for f in $(ls /run/current-system/user/Library/LaunchAgents 2> /dev/null); do
if test ! -e "${cfg.build.launchd}/user/Library/LaunchAgents/$f"; then
echo "removing user service $(basename $f .plist)" >&2
launchctl unload ~/Library/LaunchAgents/$f || true
if test -e ~/Library/LaunchAgents/$f; then rm -f ~/Library/LaunchAgents/$f; fi
fi
done
'';
};
}