2016-11-01 20:25:22 +00:00
|
|
|
|
{ config, lib, pkgs, ... }:
|
|
|
|
|
|
|
|
|
|
with lib;
|
|
|
|
|
|
|
|
|
|
let
|
2017-05-15 06:34:53 +00:00
|
|
|
|
inherit (pkgs) stdenv;
|
|
|
|
|
|
2016-11-01 20:25:22 +00:00
|
|
|
|
cfg = config.launchd;
|
|
|
|
|
|
2016-12-01 22:56:20 +00:00
|
|
|
|
toEnvironmentText = name: value: {
|
|
|
|
|
name = "${value.serviceConfig.Label}.plist";
|
2024-01-28 01:09:41 +00:00
|
|
|
|
value.text = generators.toPlist { } value.serviceConfig;
|
2016-12-01 22:56:20 +00:00
|
|
|
|
};
|
|
|
|
|
|
2016-11-01 20:25:22 +00:00
|
|
|
|
launchdConfig = import ./launchd.nix;
|
|
|
|
|
|
2017-05-13 22:03:49 +00:00
|
|
|
|
makeDrvBinPath = ps: concatMapStringsSep ":" (p: if isDerivation p then "${p}/bin" else p) ps;
|
|
|
|
|
|
2016-11-01 20:25:22 +00:00
|
|
|
|
serviceOptions =
|
|
|
|
|
{ config, name, ... }:
|
2017-01-25 20:16:53 +00:00
|
|
|
|
let
|
|
|
|
|
|
|
|
|
|
cmd = config.command;
|
|
|
|
|
env = config.environment // optionalAttrs (config.path != "") { PATH = config.path; };
|
|
|
|
|
|
|
|
|
|
in
|
|
|
|
|
|
2016-11-01 20:25:22 +00:00
|
|
|
|
{ options = {
|
2017-01-25 20:16:53 +00:00
|
|
|
|
environment = mkOption {
|
|
|
|
|
type = types.attrsOf (types.either types.str (types.listOf types.str));
|
|
|
|
|
default = {};
|
|
|
|
|
example = { PATH = "/foo/bar/bin"; LANG = "nl_NL.UTF-8"; };
|
2024-04-14 21:02:32 +00:00
|
|
|
|
description = "Environment variables passed to the service's processes.";
|
2017-01-25 20:16:53 +00:00
|
|
|
|
apply = mapAttrs (n: v: if isList v then concatStringsSep ":" v else v);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
path = mkOption {
|
2017-10-06 17:57:47 +00:00
|
|
|
|
type = types.listOf (types.either types.path types.str);
|
2017-01-25 20:16:53 +00:00
|
|
|
|
default = [];
|
2024-04-14 21:02:32 +00:00
|
|
|
|
description = ''
|
2023-06-22 11:21:32 +00:00
|
|
|
|
Packages added to the service's {env}`PATH`
|
|
|
|
|
environment variable. Only the {file}`bin`
|
2017-05-13 22:03:49 +00:00
|
|
|
|
and subdirectories of each package is added.
|
2017-01-25 20:16:53 +00:00
|
|
|
|
'';
|
2017-05-13 22:03:49 +00:00
|
|
|
|
apply = ps: if isList ps then (makeDrvBinPath ps) else ps;
|
2017-01-25 20:16:53 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
command = mkOption {
|
|
|
|
|
type = types.either types.str types.path;
|
|
|
|
|
default = "";
|
2024-04-14 21:02:32 +00:00
|
|
|
|
description = "Command executed as the service's main process.";
|
2017-01-25 20:16:53 +00:00
|
|
|
|
};
|
|
|
|
|
|
2017-05-15 06:34:53 +00:00
|
|
|
|
script = mkOption {
|
|
|
|
|
type = types.lines;
|
|
|
|
|
default = "";
|
2024-04-14 21:02:32 +00:00
|
|
|
|
description = "Shell commands executed as the service's main process.";
|
2017-05-15 06:34:53 +00:00
|
|
|
|
};
|
|
|
|
|
|
2017-01-25 20:16:53 +00:00
|
|
|
|
# preStart = mkOption {
|
|
|
|
|
# type = types.lines;
|
|
|
|
|
# default = "";
|
|
|
|
|
# description = ''
|
|
|
|
|
# Shell commands executed before the service's main process
|
|
|
|
|
# is started.
|
|
|
|
|
# '';
|
|
|
|
|
# };
|
|
|
|
|
|
2016-11-01 20:25:22 +00:00
|
|
|
|
serviceConfig = mkOption {
|
|
|
|
|
type = types.submodule launchdConfig;
|
|
|
|
|
example =
|
|
|
|
|
{ Program = "/run/current-system/sw/bin/nix-daemon";
|
|
|
|
|
KeepAlive = true;
|
|
|
|
|
};
|
|
|
|
|
default = {};
|
2024-04-14 21:02:32 +00:00
|
|
|
|
description = ''
|
2017-10-19 19:55:37 +00:00
|
|
|
|
Each attribute in this set specifies an option for a key in the plist.
|
2023-06-22 11:21:32 +00:00
|
|
|
|
<https://developer.apple.com/legacy/library/documentation/Darwin/Reference/ManPages/man5/launchd.plist.5.html>
|
2016-11-01 20:25:22 +00:00
|
|
|
|
'';
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
config = {
|
2017-05-15 06:34:53 +00:00
|
|
|
|
command = mkIf (config.script != "") (pkgs.writeScript "${name}-start" ''
|
|
|
|
|
#! ${stdenv.shell}
|
|
|
|
|
|
|
|
|
|
${config.script}
|
|
|
|
|
'');
|
|
|
|
|
|
2020-04-14 21:53:31 +00:00
|
|
|
|
serviceConfig.Label = mkDefault "${cfg.labelPrefix}.${name}";
|
2017-01-25 20:16:53 +00:00
|
|
|
|
serviceConfig.ProgramArguments = mkIf (cmd != "") [ "/bin/sh" "-c" "exec ${cmd}" ];
|
|
|
|
|
serviceConfig.EnvironmentVariables = mkIf (env != {}) env;
|
2016-11-01 20:25:22 +00:00
|
|
|
|
};
|
|
|
|
|
};
|
2016-12-01 22:56:20 +00:00
|
|
|
|
in
|
|
|
|
|
|
|
|
|
|
{
|
2016-11-01 20:25:22 +00:00
|
|
|
|
options = {
|
2020-04-14 21:53:31 +00:00
|
|
|
|
launchd.labelPrefix = mkOption {
|
|
|
|
|
type = types.str;
|
|
|
|
|
default = "org.nixos";
|
2024-04-14 21:02:32 +00:00
|
|
|
|
description = ''
|
2020-04-14 21:53:31 +00:00
|
|
|
|
The default prefix of the service label. Individual services can
|
|
|
|
|
override this by setting the Label attribute.
|
|
|
|
|
'';
|
|
|
|
|
};
|
|
|
|
|
|
2017-05-13 22:26:22 +00:00
|
|
|
|
launchd.envVariables = mkOption {
|
|
|
|
|
type = types.attrsOf (types.either types.str (types.listOf types.str));
|
|
|
|
|
default = {};
|
|
|
|
|
example = { LANG = "nl_NL.UTF-8"; };
|
2024-04-14 21:02:32 +00:00
|
|
|
|
description = ''
|
2017-05-13 22:26:22 +00:00
|
|
|
|
A set of environment variables to be set on all future
|
|
|
|
|
processes launched by launchd in the caller's context.
|
|
|
|
|
The value of each variable can be either a string or a list of
|
|
|
|
|
strings. The latter is concatenated, interspersed with colon
|
|
|
|
|
characters.
|
|
|
|
|
'';
|
|
|
|
|
apply = mapAttrs (n: v: if isList v then concatStringsSep ":" v else v);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
launchd.user.envVariables = mkOption {
|
|
|
|
|
type = types.attrsOf (types.either types.str (types.listOf types.str));
|
|
|
|
|
default = {};
|
|
|
|
|
example = { LANG = "nl_NL.UTF-8"; };
|
2024-04-14 21:02:32 +00:00
|
|
|
|
description = ''
|
2017-05-13 22:26:22 +00:00
|
|
|
|
A set of environment variables to be set on all future
|
|
|
|
|
processes launched by launchd in the caller's context.
|
|
|
|
|
The value of each variable can be either a string or a list of
|
|
|
|
|
strings. The latter is concatenated, interspersed with colon
|
|
|
|
|
characters.
|
|
|
|
|
'';
|
|
|
|
|
apply = mapAttrs (n: v: if isList v then concatStringsSep ":" v else v);
|
|
|
|
|
};
|
|
|
|
|
|
2016-11-01 20:25:22 +00:00
|
|
|
|
launchd.agents = mkOption {
|
|
|
|
|
default = {};
|
|
|
|
|
type = types.attrsOf (types.submodule serviceOptions);
|
2024-04-14 21:02:32 +00:00
|
|
|
|
description = ''
|
2016-12-03 21:44:36 +00:00
|
|
|
|
Definition of per-user launchd agents.
|
|
|
|
|
|
|
|
|
|
When a user logs in, a per-user launchd is started.
|
|
|
|
|
It does the following:
|
|
|
|
|
1. It loads the parameters for each launch-on-demand user agent from the property list files found in /System/Library/LaunchAgents, /Library/LaunchAgents, and the user’s individual Library/LaunchAgents directory.
|
|
|
|
|
2. It registers the sockets and file descriptors requested by those user agents.
|
|
|
|
|
3. It launches any user agents that requested to be running all the time.
|
|
|
|
|
4. As requests for a particular service arrive, it launches the corresponding user agent and passes the request to it.
|
|
|
|
|
5. When the user logs out, it sends a SIGTERM signal to all of the user agents that it started.
|
|
|
|
|
'';
|
2016-11-01 20:25:22 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
launchd.daemons = mkOption {
|
|
|
|
|
default = {};
|
|
|
|
|
type = types.attrsOf (types.submodule serviceOptions);
|
2024-04-14 21:02:32 +00:00
|
|
|
|
description = ''
|
2016-12-03 21:44:36 +00:00
|
|
|
|
Definition of launchd daemons.
|
|
|
|
|
|
|
|
|
|
After the system is booted and the kernel is running, launchd is run to finish the system initialization.
|
|
|
|
|
As part of that initialization, it goes through the following steps:
|
|
|
|
|
1. It loads the parameters for each launch-on-demand system-level daemon from the property list files found in /System/Library/LaunchDaemons/ and /Library/LaunchDaemons/.
|
|
|
|
|
2. It registers the sockets and file descriptors requested by those daemons.
|
|
|
|
|
3. It launches any daemons that requested to be running all the time.
|
|
|
|
|
4. As requests for a particular service arrive, it launches the corresponding daemon and passes the request to it.
|
|
|
|
|
5. When the system shuts down, it sends a SIGTERM signal to all of the daemons that it started.
|
|
|
|
|
'';
|
2016-11-01 20:25:22 +00:00
|
|
|
|
};
|
|
|
|
|
|
2017-01-25 21:35:06 +00:00
|
|
|
|
launchd.user.agents = mkOption {
|
|
|
|
|
default = {};
|
|
|
|
|
type = types.attrsOf (types.submodule serviceOptions);
|
2024-04-14 21:02:32 +00:00
|
|
|
|
description = ''
|
2017-01-25 21:35:06 +00:00
|
|
|
|
Definition of per-user launchd agents.
|
|
|
|
|
|
|
|
|
|
When a user logs in, a per-user launchd is started.
|
|
|
|
|
It does the following:
|
|
|
|
|
1. It loads the parameters for each launch-on-demand user agent from the property list files found in /System/Library/LaunchAgents, /Library/LaunchAgents, and the user’s individual Library/LaunchAgents directory.
|
|
|
|
|
2. It registers the sockets and file descriptors requested by those user agents.
|
|
|
|
|
3. It launches any user agents that requested to be running all the time.
|
|
|
|
|
4. As requests for a particular service arrive, it launches the corresponding user agent and passes the request to it.
|
|
|
|
|
5. When the user logs out, it sends a SIGTERM signal to all of the user agents that it started.
|
|
|
|
|
'';
|
|
|
|
|
};
|
2016-11-01 20:25:22 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
config = {
|
|
|
|
|
|
2016-12-01 22:56:20 +00:00
|
|
|
|
environment.launchAgents = mapAttrs' toEnvironmentText cfg.agents;
|
|
|
|
|
environment.launchDaemons = mapAttrs' toEnvironmentText cfg.daemons;
|
2016-11-01 20:25:22 +00:00
|
|
|
|
|
2017-01-25 21:35:06 +00:00
|
|
|
|
environment.userLaunchAgents = mapAttrs' toEnvironmentText cfg.user.agents;
|
|
|
|
|
|
2016-11-01 20:25:22 +00:00
|
|
|
|
};
|
|
|
|
|
}
|