2011-01-09 23:00:41 +01:00
|
|
|
|
{ config, pkgs, ... }:
|
2008-11-18 19:00:09 +01:00
|
|
|
|
|
2009-07-15 17:24:11 +02:00
|
|
|
|
with pkgs.lib;
|
2012-08-06 17:45:59 +02:00
|
|
|
|
with import ../boot/systemd-unit-options.nix { inherit config pkgs; };
|
2008-12-07 13:27:46 +01:00
|
|
|
|
|
2009-07-15 17:24:11 +02:00
|
|
|
|
let
|
2009-07-15 13:38:17 +02:00
|
|
|
|
|
2012-03-04 13:58:06 +01:00
|
|
|
|
userExists = u:
|
|
|
|
|
(u == "") || any (uu: uu.name == u) (attrValues config.users.extraUsers);
|
|
|
|
|
|
|
|
|
|
groupExists = g:
|
|
|
|
|
(g == "") || any (gg: gg.name == g) (attrValues config.users.extraGroups);
|
2009-11-06 10:36:35 +01:00
|
|
|
|
|
2012-07-19 23:41:42 +02:00
|
|
|
|
makeJobScript = name: content: "${pkgs.writeScriptBin name content}/bin/${name}";
|
|
|
|
|
|
2012-06-16 06:19:43 +02:00
|
|
|
|
# From a job description, generate an systemd unit file.
|
|
|
|
|
makeUnit = job:
|
2009-07-15 17:24:11 +02:00
|
|
|
|
|
|
|
|
|
let
|
2010-06-11 23:44:06 +02:00
|
|
|
|
hasMain = job.script != "" || job.exec != "";
|
2009-07-15 17:24:11 +02:00
|
|
|
|
|
2012-10-30 17:27:14 +01:00
|
|
|
|
env = job.environment;
|
2011-01-09 23:00:41 +01:00
|
|
|
|
|
2012-10-15 22:01:30 +02:00
|
|
|
|
preStartScript = makeJobScript "${job.name}-pre-start"
|
2009-07-15 17:24:11 +02:00
|
|
|
|
''
|
2012-06-16 06:19:43 +02:00
|
|
|
|
#! ${pkgs.stdenv.shell} -e
|
|
|
|
|
${job.preStart}
|
|
|
|
|
'';
|
|
|
|
|
|
2012-10-15 22:01:30 +02:00
|
|
|
|
startScript = makeJobScript "${job.name}-start"
|
2012-06-16 06:19:43 +02:00
|
|
|
|
''
|
|
|
|
|
#! ${pkgs.stdenv.shell} -e
|
|
|
|
|
${if job.script != "" then job.script else ''
|
|
|
|
|
exec ${job.exec}
|
|
|
|
|
''}
|
|
|
|
|
'';
|
|
|
|
|
|
2012-10-15 22:01:30 +02:00
|
|
|
|
postStartScript = makeJobScript "${job.name}-post-start"
|
2012-06-16 06:19:43 +02:00
|
|
|
|
''
|
|
|
|
|
#! ${pkgs.stdenv.shell} -e
|
|
|
|
|
${job.postStart}
|
|
|
|
|
'';
|
|
|
|
|
|
2012-10-15 22:01:30 +02:00
|
|
|
|
preStopScript = makeJobScript "${job.name}-pre-stop"
|
2012-06-16 06:19:43 +02:00
|
|
|
|
''
|
|
|
|
|
#! ${pkgs.stdenv.shell} -e
|
|
|
|
|
${job.preStop}
|
|
|
|
|
'';
|
|
|
|
|
|
2012-10-15 22:01:30 +02:00
|
|
|
|
postStopScript = makeJobScript "${job.name}-post-stop"
|
2012-06-16 06:19:43 +02:00
|
|
|
|
''
|
|
|
|
|
#! ${pkgs.stdenv.shell} -e
|
|
|
|
|
${job.postStop}
|
|
|
|
|
'';
|
2012-06-18 05:31:21 +02:00
|
|
|
|
in {
|
2012-06-16 06:19:43 +02:00
|
|
|
|
|
2012-10-02 16:32:29 +02:00
|
|
|
|
inherit (job) description requires before partOf environment path restartIfChanged unitConfig;
|
2012-06-18 23:42:26 +02:00
|
|
|
|
|
|
|
|
|
after =
|
2012-08-06 18:26:52 +02:00
|
|
|
|
(if job.startOn == "stopped udevtrigger" then [ "systemd-udev-settle.service" ] else
|
|
|
|
|
if job.startOn == "started udev" then [ "systemd-udev.service" ] else
|
2012-10-11 22:18:48 +02:00
|
|
|
|
if job.startOn == "started network-interfaces" then [ "network-interfaces.target" ] else
|
2012-08-20 17:21:11 +02:00
|
|
|
|
if job.startOn == "started networking" then [ "network.target" ] else
|
2012-08-15 21:36:54 +02:00
|
|
|
|
if job.startOn == "ip-up" then [] else
|
|
|
|
|
if job.startOn == "" || job.startOn == "startup" then [] else
|
2012-08-14 23:30:11 +02:00
|
|
|
|
builtins.trace "Warning: job ‘${job.name}’ has unknown startOn value ‘${job.startOn}’." []
|
|
|
|
|
) ++ job.after;
|
2012-06-16 06:19:43 +02:00
|
|
|
|
|
2012-10-02 16:32:29 +02:00
|
|
|
|
wants =
|
2012-10-02 16:26:55 +02:00
|
|
|
|
(if job.startOn == "stopped udevtrigger" then [ "systemd-udev-settle.service" ] else []
|
2012-10-02 16:32:29 +02:00
|
|
|
|
) ++ job.wants;
|
2012-10-02 16:26:55 +02:00
|
|
|
|
|
2012-08-06 18:26:52 +02:00
|
|
|
|
wantedBy =
|
2012-08-15 21:36:54 +02:00
|
|
|
|
(if job.startOn == "" then [] else
|
|
|
|
|
if job.startOn == "ip-up" then [ "ip-up.target" ] else
|
|
|
|
|
[ "multi-user.target" ]) ++ job.wantedBy;
|
2011-09-14 20:20:50 +02:00
|
|
|
|
|
2012-06-18 23:42:26 +02:00
|
|
|
|
serviceConfig =
|
2012-10-01 22:27:42 +02:00
|
|
|
|
job.serviceConfig
|
|
|
|
|
// optionalAttrs (job.preStart != "" && (job.script != "" || job.exec != ""))
|
|
|
|
|
{ ExecStartPre = preStartScript; }
|
2012-10-02 00:20:22 +02:00
|
|
|
|
// optionalAttrs (job.preStart != "" && job.script == "" && job.exec == "")
|
|
|
|
|
{ ExecStart = preStartScript; }
|
2012-10-01 22:27:42 +02:00
|
|
|
|
// optionalAttrs (job.script != "" || job.exec != "")
|
|
|
|
|
{ ExecStart = startScript; }
|
|
|
|
|
// optionalAttrs (job.postStart != "")
|
|
|
|
|
{ ExecStartPost = postStartScript; }
|
|
|
|
|
// optionalAttrs (job.preStop != "")
|
|
|
|
|
{ ExecStop = preStopScript; }
|
|
|
|
|
// optionalAttrs (job.postStop != "")
|
|
|
|
|
{ ExecStopPost = postStopScript; }
|
|
|
|
|
// (if job.script == "" && job.exec == "" then { Type = "oneshot"; RemainAfterExit = true; } else
|
2012-10-26 16:21:01 +02:00
|
|
|
|
if job.daemonType == "fork" || job.daemonType == "daemon" then { Type = "forking"; GuessMainPID = true; } else
|
2012-10-01 22:27:42 +02:00
|
|
|
|
if job.daemonType == "none" then { } else
|
|
|
|
|
throw "invalid daemon type `${job.daemonType}'")
|
|
|
|
|
// optionalAttrs (!job.task && job.respawn)
|
2012-10-23 14:30:50 +02:00
|
|
|
|
{ Restart = "always"; }
|
|
|
|
|
// optionalAttrs job.task
|
|
|
|
|
{ Type = "oneshot"; RemainAfterExit = false; };
|
2012-06-18 05:31:21 +02:00
|
|
|
|
};
|
|
|
|
|
|
2012-03-17 20:12:33 +01:00
|
|
|
|
|
2012-08-06 17:45:59 +02:00
|
|
|
|
jobOptions = serviceOptions // {
|
2009-07-15 15:41:00 +02:00
|
|
|
|
|
2009-11-06 10:26:36 +01:00
|
|
|
|
name = mkOption {
|
|
|
|
|
# !!! The type should ensure that this could be a filename.
|
|
|
|
|
type = types.string;
|
|
|
|
|
example = "sshd";
|
|
|
|
|
description = ''
|
|
|
|
|
Name of the Upstart job.
|
|
|
|
|
'';
|
|
|
|
|
};
|
2009-09-04 17:27:52 +02:00
|
|
|
|
|
2009-11-06 10:26:36 +01:00
|
|
|
|
startOn = mkOption {
|
|
|
|
|
# !!! Re-enable this once we're on Upstart >= 0.6.
|
2011-09-14 20:20:50 +02:00
|
|
|
|
#type = types.string;
|
2009-11-06 10:26:36 +01:00
|
|
|
|
default = "";
|
|
|
|
|
description = ''
|
|
|
|
|
The Upstart event that triggers this job to be started.
|
|
|
|
|
If empty, the job will not start automatically.
|
|
|
|
|
'';
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
stopOn = mkOption {
|
|
|
|
|
type = types.string;
|
2010-06-02 18:04:08 +02:00
|
|
|
|
default = "starting shutdown";
|
2009-11-06 10:26:36 +01:00
|
|
|
|
description = ''
|
|
|
|
|
The Upstart event that triggers this job to be stopped.
|
|
|
|
|
'';
|
|
|
|
|
};
|
|
|
|
|
|
2009-11-06 16:23:16 +01:00
|
|
|
|
postStart = mkOption {
|
|
|
|
|
type = types.string;
|
|
|
|
|
default = "";
|
|
|
|
|
description = ''
|
|
|
|
|
Shell commands executed after the job is started (i.e. after
|
|
|
|
|
the job's main process is started), but before the job is
|
|
|
|
|
considered “running”.
|
|
|
|
|
'';
|
2009-11-06 23:45:19 +01:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
preStop = mkOption {
|
|
|
|
|
type = types.string;
|
|
|
|
|
default = "";
|
|
|
|
|
description = ''
|
|
|
|
|
Shell commands executed before the job is stopped
|
|
|
|
|
(i.e. before Upstart kills the job's main process). This can
|
|
|
|
|
be used to cleanly shut down a daemon.
|
|
|
|
|
'';
|
2009-11-06 16:23:16 +01:00
|
|
|
|
};
|
|
|
|
|
|
2009-11-06 10:26:36 +01:00
|
|
|
|
postStop = mkOption {
|
|
|
|
|
type = types.string;
|
|
|
|
|
default = "";
|
|
|
|
|
description = ''
|
|
|
|
|
Shell commands executed after the job has stopped
|
|
|
|
|
(i.e. after the job's main process has terminated).
|
|
|
|
|
'';
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
exec = mkOption {
|
|
|
|
|
type = types.string;
|
|
|
|
|
default = "";
|
|
|
|
|
description = ''
|
|
|
|
|
Command to start the job's main process. If empty, the
|
|
|
|
|
job has no main process, but can still have pre/post-start
|
2009-11-06 16:23:16 +01:00
|
|
|
|
and pre/post-stop scripts, and is considered “running”
|
2009-11-06 10:26:36 +01:00
|
|
|
|
until it is stopped.
|
|
|
|
|
'';
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
respawn = mkOption {
|
|
|
|
|
type = types.bool;
|
|
|
|
|
default = true;
|
|
|
|
|
description = ''
|
|
|
|
|
Whether to restart the job automatically if its process
|
|
|
|
|
ends unexpectedly.
|
|
|
|
|
'';
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
task = mkOption {
|
|
|
|
|
type = types.bool;
|
|
|
|
|
default = false;
|
|
|
|
|
description = ''
|
|
|
|
|
Whether this job is a task rather than a service. Tasks
|
|
|
|
|
are executed only once, while services are restarted when
|
|
|
|
|
they exit.
|
|
|
|
|
'';
|
|
|
|
|
};
|
|
|
|
|
|
2009-11-06 16:23:16 +01:00
|
|
|
|
daemonType = mkOption {
|
|
|
|
|
type = types.string;
|
|
|
|
|
default = "none";
|
|
|
|
|
description = ''
|
|
|
|
|
Determines how Upstart detects when a daemon should be
|
|
|
|
|
considered “running”. The value <literal>none</literal> means
|
|
|
|
|
that the daemon is considered ready immediately. The value
|
|
|
|
|
<literal>fork</literal> means that the daemon will fork once.
|
|
|
|
|
The value <literal>daemon</literal> means that the daemon will
|
|
|
|
|
fork twice. The value <literal>stop</literal> means that the
|
|
|
|
|
daemon will raise the SIGSTOP signal to indicate readiness.
|
|
|
|
|
'';
|
|
|
|
|
};
|
|
|
|
|
|
2012-03-04 13:58:06 +01:00
|
|
|
|
setuid = mkOption {
|
|
|
|
|
type = types.string;
|
|
|
|
|
check = userExists;
|
|
|
|
|
default = "";
|
|
|
|
|
description = ''
|
|
|
|
|
Run the daemon as a different user.
|
|
|
|
|
'';
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
setgid = mkOption {
|
|
|
|
|
type = types.string;
|
|
|
|
|
check = groupExists;
|
|
|
|
|
default = "";
|
|
|
|
|
description = ''
|
|
|
|
|
Run the daemon as a different group.
|
|
|
|
|
'';
|
|
|
|
|
};
|
|
|
|
|
|
2010-09-14 13:22:17 +02:00
|
|
|
|
path = mkOption {
|
2012-06-18 23:42:26 +02:00
|
|
|
|
default = [];
|
2010-09-14 13:22:17 +02:00
|
|
|
|
description = ''
|
|
|
|
|
Packages added to the job's <envar>PATH</envar> environment variable.
|
2011-09-14 20:20:50 +02:00
|
|
|
|
Both the <filename>bin</filename> and <filename>sbin</filename>
|
2010-09-14 13:22:17 +02:00
|
|
|
|
subdirectories of each package are added.
|
|
|
|
|
'';
|
|
|
|
|
};
|
|
|
|
|
|
2009-11-06 10:26:36 +01:00
|
|
|
|
};
|
2009-10-05 20:31:27 +02:00
|
|
|
|
|
2011-09-14 20:20:50 +02:00
|
|
|
|
|
2012-08-06 17:45:59 +02:00
|
|
|
|
upstartJob = { name, config, ... }: {
|
2011-09-14 20:20:50 +02:00
|
|
|
|
|
2009-10-06 11:30:13 +02:00
|
|
|
|
options = {
|
2012-08-06 17:45:59 +02:00
|
|
|
|
|
2012-06-18 05:31:21 +02:00
|
|
|
|
unit = mkOption {
|
2012-06-16 06:19:43 +02:00
|
|
|
|
default = makeUnit config;
|
2012-06-18 05:31:21 +02:00
|
|
|
|
description = "Generated definition of the systemd unit corresponding to this job.";
|
2009-10-06 11:30:13 +02:00
|
|
|
|
};
|
2012-08-06 17:45:59 +02:00
|
|
|
|
|
2009-10-06 11:30:13 +02:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
config = {
|
2012-08-06 17:45:59 +02:00
|
|
|
|
|
2009-10-06 11:30:13 +02:00
|
|
|
|
# The default name is the name extracted from the attribute path.
|
2012-03-19 19:07:28 +01:00
|
|
|
|
name = mkDefaultValue name;
|
2012-08-06 17:45:59 +02:00
|
|
|
|
|
2009-10-06 11:30:13 +02:00
|
|
|
|
};
|
2011-09-14 20:20:50 +02:00
|
|
|
|
|
2009-10-06 11:30:13 +02:00
|
|
|
|
};
|
2009-10-05 20:31:27 +02:00
|
|
|
|
|
|
|
|
|
in
|
2011-09-14 20:20:50 +02:00
|
|
|
|
|
2009-10-05 20:31:27 +02:00
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
###### interface
|
2011-09-14 20:20:50 +02:00
|
|
|
|
|
2009-10-05 20:31:27 +02:00
|
|
|
|
options = {
|
|
|
|
|
|
2009-10-12 20:09:34 +02:00
|
|
|
|
jobs = mkOption {
|
2009-10-05 20:31:27 +02:00
|
|
|
|
default = {};
|
|
|
|
|
description = ''
|
|
|
|
|
This option defines the system jobs started and managed by the
|
|
|
|
|
Upstart daemon.
|
|
|
|
|
'';
|
2011-07-27 22:55:35 +02:00
|
|
|
|
type = types.loaOf types.optionSet;
|
2009-10-06 11:30:13 +02:00
|
|
|
|
options = [ jobOptions upstartJob ];
|
2009-10-05 20:31:27 +02:00
|
|
|
|
};
|
2011-09-14 20:20:50 +02:00
|
|
|
|
|
2008-11-18 19:00:09 +01:00
|
|
|
|
};
|
|
|
|
|
|
2009-07-15 11:06:36 +02:00
|
|
|
|
|
|
|
|
|
###### implementation
|
2011-09-14 20:20:50 +02:00
|
|
|
|
|
2009-07-15 11:06:36 +02:00
|
|
|
|
config = {
|
|
|
|
|
|
2013-01-16 12:33:18 +01:00
|
|
|
|
systemd.services =
|
2012-06-16 06:19:43 +02:00
|
|
|
|
flip mapAttrs' config.jobs (name: job:
|
2012-08-23 16:25:27 +02:00
|
|
|
|
nameValuePair job.name job.unit);
|
2012-08-06 17:45:59 +02:00
|
|
|
|
|
2008-11-18 19:00:09 +01:00
|
|
|
|
};
|
2009-07-15 11:06:36 +02:00
|
|
|
|
|
2009-01-02 17:07:21 +01:00
|
|
|
|
}
|