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;
|
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
|
|
|
|
2010-02-15 16:55:54 +01:00
|
|
|
upstart = pkgs.upstart;
|
2009-11-06 10:36:35 +01:00
|
|
|
|
|
|
|
|
2009-11-06 11:43:38 +01:00
|
|
|
# Path for Upstart jobs. Should be quite minimal.
|
|
|
|
upstartPath =
|
|
|
|
[ pkgs.coreutils
|
|
|
|
pkgs.findutils
|
|
|
|
pkgs.gnugrep
|
|
|
|
pkgs.gnused
|
|
|
|
upstart
|
|
|
|
];
|
|
|
|
|
|
|
|
|
2009-07-15 17:24:11 +02:00
|
|
|
# From a job description, generate an Upstart job file.
|
2009-10-12 19:09:38 +02:00
|
|
|
makeJob = 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
|
|
|
|
2011-01-09 23:00:41 +01:00
|
|
|
env = config.system.upstartEnvironment // job.environment;
|
|
|
|
|
2009-10-12 19:09:38 +02:00
|
|
|
jobText =
|
2010-06-01 21:44:23 +02:00
|
|
|
let log = "/var/log/upstart/${job.name}"; in
|
2009-07-15 17:24:11 +02:00
|
|
|
''
|
2009-07-16 16:51:49 +02:00
|
|
|
# Upstart job `${job.name}'. This is a generated file. Do not edit.
|
|
|
|
|
2009-07-15 17:24:11 +02:00
|
|
|
description "${job.description}"
|
|
|
|
|
2009-07-16 16:51:49 +02:00
|
|
|
${if isList job.startOn then
|
2009-11-06 16:46:56 +01:00
|
|
|
"start on ${concatStringsSep " or " job.startOn}"
|
2009-07-16 16:51:49 +02:00
|
|
|
else if job.startOn != "" then
|
|
|
|
"start on ${job.startOn}"
|
|
|
|
else ""
|
|
|
|
}
|
|
|
|
|
2009-11-06 16:23:16 +01:00
|
|
|
${optionalString (job.stopOn != "") "stop on ${job.stopOn}"}
|
2009-07-15 17:24:11 +02:00
|
|
|
|
2010-09-14 13:22:17 +02:00
|
|
|
env PATH=${makeSearchPath "bin" (job.path ++ upstartPath)}:${makeSearchPath "sbin" (job.path ++ upstartPath)}
|
|
|
|
|
2011-01-09 23:00:41 +01:00
|
|
|
${concatMapStrings (n: "env ${n}=\"${getAttr n env}\"\n") (attrNames env)}
|
2009-07-15 17:24:11 +02:00
|
|
|
|
2009-11-06 16:23:16 +01:00
|
|
|
${optionalString (job.preStart != "") ''
|
2009-11-06 11:43:38 +01:00
|
|
|
pre-start script
|
2010-06-01 21:44:23 +02:00
|
|
|
exec >> ${log} 2>&1
|
2009-07-15 17:24:11 +02:00
|
|
|
${job.preStart}
|
|
|
|
end script
|
2009-11-06 16:23:16 +01:00
|
|
|
''}
|
2009-07-15 17:24:11 +02:00
|
|
|
|
2009-07-16 16:51:49 +02:00
|
|
|
${if job.script != "" && job.exec != "" then
|
|
|
|
abort "Job ${job.name} has both a `script' and `exec' attribute."
|
|
|
|
else if job.script != "" then
|
|
|
|
''
|
|
|
|
script
|
2010-06-01 21:44:23 +02:00
|
|
|
exec >> ${log} 2>&1
|
2009-07-16 16:51:49 +02:00
|
|
|
${job.script}
|
|
|
|
end script
|
|
|
|
''
|
|
|
|
else if job.exec != "" then
|
|
|
|
''
|
2010-06-01 22:41:59 +02:00
|
|
|
script
|
|
|
|
exec >> ${log} 2>&1
|
|
|
|
exec ${job.exec}
|
|
|
|
end script
|
2009-07-16 16:51:49 +02:00
|
|
|
''
|
2009-11-06 11:43:38 +01:00
|
|
|
else ""
|
2009-07-16 16:51:49 +02:00
|
|
|
}
|
|
|
|
|
2009-11-06 16:23:16 +01:00
|
|
|
${optionalString (job.postStart != "") ''
|
|
|
|
post-start script
|
2010-06-01 21:44:23 +02:00
|
|
|
exec >> ${log} 2>&1
|
2009-11-06 16:23:16 +01:00
|
|
|
${job.postStart}
|
|
|
|
end script
|
|
|
|
''}
|
|
|
|
|
|
|
|
${optionalString job.task "task"}
|
2010-06-02 18:04:08 +02:00
|
|
|
${optionalString (!job.task && job.respawn) "respawn"}
|
2009-07-16 15:46:49 +02:00
|
|
|
|
2010-06-11 23:44:06 +02:00
|
|
|
${ # preStop is run only if there is exec or script.
|
|
|
|
# (upstart 0.6.5, job.c:562)
|
|
|
|
optionalString (job.preStop != "") (assert hasMain; ''
|
2009-11-06 23:45:19 +01:00
|
|
|
pre-stop script
|
2010-06-01 21:44:23 +02:00
|
|
|
exec >> ${log} 2>&1
|
2009-11-06 23:45:19 +01:00
|
|
|
${job.preStop}
|
|
|
|
end script
|
2010-06-11 23:44:06 +02:00
|
|
|
'')}
|
2009-11-06 23:45:19 +01:00
|
|
|
|
2009-11-06 16:23:16 +01:00
|
|
|
${optionalString (job.postStop != "") ''
|
2009-11-06 11:43:38 +01:00
|
|
|
post-stop script
|
2010-06-01 21:44:23 +02:00
|
|
|
exec >> ${log} 2>&1
|
2009-07-16 15:46:49 +02:00
|
|
|
${job.postStop}
|
|
|
|
end script
|
2009-11-06 16:23:16 +01:00
|
|
|
''}
|
|
|
|
|
|
|
|
${optionalString (!job.task) (
|
|
|
|
if job.daemonType == "fork" then "expect fork" else
|
|
|
|
if job.daemonType == "daemon" then "expect daemon" else
|
|
|
|
if job.daemonType == "stop" then "expect stop" else
|
|
|
|
if job.daemonType == "none" then "" else
|
|
|
|
throw "invalid daemon type `${job.daemonType}'"
|
|
|
|
)}
|
2009-09-04 17:27:52 +02:00
|
|
|
|
|
|
|
${job.extraConfig}
|
2009-07-15 17:24:11 +02:00
|
|
|
'';
|
|
|
|
|
|
|
|
in
|
2009-11-06 11:43:38 +01:00
|
|
|
pkgs.runCommand ("upstart-" + job.name + ".conf")
|
2009-10-12 19:09:38 +02:00
|
|
|
{ inherit (job) buildHook; inherit jobText; }
|
2009-07-15 17:24:11 +02:00
|
|
|
''
|
|
|
|
eval "$buildHook"
|
2009-11-06 11:43:38 +01:00
|
|
|
echo "$jobText" > $out
|
2009-07-15 17:24:11 +02:00
|
|
|
'';
|
|
|
|
|
2009-11-06 10:26:36 +01:00
|
|
|
|
2009-10-06 11:30:13 +02:00
|
|
|
jobOptions = {
|
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
|
|
|
buildHook = mkOption {
|
|
|
|
type = types.string;
|
|
|
|
default = "true";
|
|
|
|
description = ''
|
|
|
|
Command run while building the Upstart job. Can be used
|
|
|
|
to perform simple regression tests (e.g., the Apache
|
|
|
|
Upstart job uses it to check the syntax of the generated
|
|
|
|
<filename>httpd.conf</filename>.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
description = mkOption {
|
|
|
|
type = types.string;
|
|
|
|
default = "(no description given)";
|
|
|
|
description = ''
|
|
|
|
A short description of this job.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
startOn = mkOption {
|
|
|
|
# !!! Re-enable this once we're on Upstart >= 0.6.
|
|
|
|
#type = types.string;
|
|
|
|
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.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
preStart = mkOption {
|
|
|
|
type = types.string;
|
|
|
|
default = "";
|
|
|
|
description = ''
|
|
|
|
Shell commands executed before the job is started
|
|
|
|
(i.e. before the job's main process is started).
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
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.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
script = mkOption {
|
|
|
|
type = types.string;
|
|
|
|
default = "";
|
|
|
|
description = ''
|
|
|
|
Shell commands executed as the job's main process. Can be
|
|
|
|
specified instead of the <varname>exec</varname> attribute.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
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.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
environment = mkOption {
|
|
|
|
type = types.attrs;
|
|
|
|
default = {};
|
|
|
|
example = { PATH = "/foo/bar/bin"; LANG = "nl_NL.UTF-8"; };
|
|
|
|
description = ''
|
|
|
|
Environment variables passed to the job's processes.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
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.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2009-11-06 10:26:36 +01:00
|
|
|
extraConfig = mkOption {
|
|
|
|
type = types.string;
|
|
|
|
default = "";
|
|
|
|
example = "limit nofile 4096 4096";
|
|
|
|
description = ''
|
|
|
|
Additional Upstart stanzas not otherwise supported.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2010-09-14 13:22:17 +02:00
|
|
|
path = mkOption {
|
|
|
|
default = [ ];
|
|
|
|
description = ''
|
|
|
|
Packages added to the job's <envar>PATH</envar> environment variable.
|
|
|
|
Both the <filename>bin</filename> and <filename>sbin</filename>
|
|
|
|
subdirectories of each package are added.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2009-11-06 10:26:36 +01:00
|
|
|
};
|
2009-10-05 20:31:27 +02:00
|
|
|
|
2009-11-06 10:26:36 +01:00
|
|
|
|
2009-10-06 11:30:13 +02:00
|
|
|
upstartJob = {name, config, ...}: {
|
2010-03-15 14:41:21 +01:00
|
|
|
|
2009-10-06 11:30:13 +02:00
|
|
|
options = {
|
2009-11-06 11:43:38 +01:00
|
|
|
jobDrv = mkOption {
|
2009-10-06 11:30:13 +02:00
|
|
|
default = makeJob config;
|
|
|
|
type = types.uniq types.package;
|
|
|
|
description = ''
|
2009-11-06 11:43:38 +01:00
|
|
|
Derivation that builds the Upstart job file. The default
|
|
|
|
value is generated from other options.
|
2009-10-06 11:30:13 +02:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
config = {
|
|
|
|
# The default name is the name extracted from the attribute path.
|
|
|
|
name = mkDefaultValue (
|
|
|
|
replaceChars ["<" ">" "*"] ["_" "_" "_name_"] name
|
|
|
|
);
|
|
|
|
};
|
2010-03-15 14:41:21 +01:00
|
|
|
|
2009-10-06 11:30:13 +02:00
|
|
|
};
|
2009-10-05 20:31:27 +02:00
|
|
|
|
|
|
|
in
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
###### interface
|
|
|
|
|
|
|
|
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.
|
|
|
|
'';
|
|
|
|
type = types.attrsOf types.optionSet;
|
2009-10-06 11:30:13 +02:00
|
|
|
options = [ jobOptions upstartJob ];
|
2009-10-05 20:31:27 +02:00
|
|
|
};
|
|
|
|
|
2009-07-15 11:06:36 +02:00
|
|
|
tests.upstartJobs = mkOption {
|
|
|
|
internal = true;
|
|
|
|
default = {};
|
|
|
|
description = ''
|
|
|
|
Make it easier to build individual Upstart jobs. (e.g.,
|
|
|
|
<command>nix-build /etc/nixos/nixos -A
|
|
|
|
tests.upstartJobs.xserver</command>).
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2011-01-09 23:00:41 +01:00
|
|
|
system.upstartEnvironment = mkOption {
|
|
|
|
type = types.attrs;
|
|
|
|
default = {};
|
|
|
|
example = { TZ = "CET"; };
|
|
|
|
description = ''
|
|
|
|
Environment variables passed to <emphasis>all</emphasis> Upstart jobs.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2008-11-18 19:00:09 +01:00
|
|
|
};
|
|
|
|
|
2009-07-15 11:06:36 +02:00
|
|
|
|
|
|
|
###### implementation
|
|
|
|
|
|
|
|
config = {
|
|
|
|
|
2009-11-06 10:36:35 +01:00
|
|
|
system.build.upstart = upstart;
|
|
|
|
|
2009-07-15 11:06:36 +02:00
|
|
|
environment.etc =
|
2009-11-06 11:43:38 +01:00
|
|
|
flip map (attrValues config.jobs) (job:
|
|
|
|
{ source = job.jobDrv;
|
|
|
|
target = "init/${job.name}.conf";
|
|
|
|
} );
|
2009-07-15 11:06:36 +02:00
|
|
|
|
2009-11-06 16:59:23 +01:00
|
|
|
# Upstart can listen on the system bus, allowing normal users to
|
|
|
|
# do status queries.
|
|
|
|
services.dbus.packages = [ upstart ];
|
|
|
|
|
2008-11-18 19:00:09 +01:00
|
|
|
};
|
2009-07-15 11:06:36 +02:00
|
|
|
|
2009-01-02 17:07:21 +01:00
|
|
|
}
|