1
0
Fork 0
mirror of https://github.com/LnL7/nix-darwin.git synced 2024-12-14 11:57:34 +00:00
nix-darwin/modules/launchd/default.nix

89 lines
2.5 KiB
Nix
Raw Normal View History

2016-11-01 20:25:22 +00:00
{ config, lib, pkgs, ... }:
with import ./lib.nix { inherit lib; };
with lib;
let
cfg = config.launchd;
launchdConfig = import ./launchd.nix;
serviceOptions =
{ config, name, ... }:
{ options = {
plist = mkOption {
internal = true;
2016-11-07 20:00:23 +00:00
type = types.path;
2016-11-01 20:25:22 +00:00
description = "The generated plist.";
};
serviceConfig = mkOption {
type = types.submodule launchdConfig;
example =
{ Program = "/run/current-system/sw/bin/nix-daemon";
KeepAlive = true;
};
default = {};
description = ''
Each attribute in this set specifies an option for a <key> in the plist.
https://developer.apple.com/legacy/library/documentation/Darwin/Reference/ManPages/man5/launchd.plist.5.html
'';
};
};
config = {
serviceConfig.Label = mkDefault "org.nixos.${name}";
2016-11-30 22:12:06 +00:00
plist = pkgs.writeText "${config.serviceConfig.Label}.plist" (toPLIST config.serviceConfig);
2016-11-01 20:25:22 +00:00
};
};
in {
options = {
launchd.agents = mkOption {
default = {};
type = types.attrsOf (types.submodule serviceOptions);
description = "Definition of launchd agents.";
};
launchd.daemons = mkOption {
default = {};
type = types.attrsOf (types.submodule serviceOptions);
description = "Definition of launchd daemons.";
};
launchd.user.agents = mkOption {
default = {};
type = types.attrsOf (types.submodule serviceOptions);
description = "Definition of launchd per-user agents.";
};
};
config = {
2016-11-07 20:00:23 +00:00
system.build.launchd = pkgs.stdenvNoCC.mkDerivation {
name = "launchd-library";
preferLocalBuild = true;
buildCommand = ''
mkdir -p $out/Library/LaunchDaemons
ln -s ${cfg.daemons.nix-daemon.plist} $out/Library/LaunchDaemons/${cfg.daemons.nix-daemon.serviceConfig.Label}.plist
'';
};
2016-11-01 20:25:22 +00:00
system.activationScripts.launchd.text = ''
# Set up launchd services in /Library/LaunchAgents, /Library/LaunchDaemons and ~/Library/LaunchAgents
echo "setting up launchd services..."
launchctl unload '/Library/LaunchDaemons/${cfg.daemons.nix-daemon.serviceConfig.Label}.plist'
ln -sfn '${cfg.daemons.nix-daemon.plist}' '/Library/LaunchDaemons/${cfg.daemons.nix-daemon.serviceConfig.Label}.plist'
launchctl load '/Library/LaunchDaemons/${cfg.daemons.nix-daemon.serviceConfig.Label}.plist'
2016-11-01 20:25:22 +00:00
'';
};
}