2008-12-04 15:48:27 +00:00
|
|
|
{config, pkgs, ...}:
|
2008-11-18 18:00:09 +00:00
|
|
|
|
2009-07-15 16:24:11 +01:00
|
|
|
with pkgs.lib;
|
2008-12-07 12:27:46 +00:00
|
|
|
|
2009-07-15 16:24:11 +01:00
|
|
|
let
|
2009-07-15 12:38:17 +01:00
|
|
|
|
2009-07-15 16:24:11 +01:00
|
|
|
# From a job description, generate an Upstart job file.
|
2009-10-12 18:09:38 +01:00
|
|
|
makeJob = job:
|
2009-07-15 16:24:11 +01:00
|
|
|
|
|
|
|
let
|
|
|
|
|
2009-10-12 18:09:38 +01:00
|
|
|
jobText =
|
2009-07-15 16:24:11 +01:00
|
|
|
''
|
2009-07-16 15:51:49 +01:00
|
|
|
# Upstart job `${job.name}'. This is a generated file. Do not edit.
|
|
|
|
|
2009-07-15 16:24:11 +01:00
|
|
|
description "${job.description}"
|
|
|
|
|
2009-07-16 15:51:49 +01:00
|
|
|
${if isList job.startOn then
|
|
|
|
# This is a hack to support or-dependencies on Upstart 0.3.
|
|
|
|
concatMapStrings (x: "start on ${x}\n") job.startOn
|
|
|
|
else if job.startOn != "" then
|
|
|
|
"start on ${job.startOn}"
|
|
|
|
else ""
|
|
|
|
}
|
|
|
|
|
|
|
|
${if job.stopOn != "" then "stop on ${job.stopOn}" else ""}
|
2009-07-15 16:24:11 +01:00
|
|
|
|
|
|
|
${concatMapStrings (n: "env ${n}=${getAttr n job.environment}\n") (attrNames job.environment)}
|
|
|
|
|
|
|
|
${if job.preStart != "" then ''
|
|
|
|
start script
|
|
|
|
${job.preStart}
|
|
|
|
end script
|
|
|
|
'' else ""}
|
|
|
|
|
2009-07-16 15:51:49 +01:00
|
|
|
${if job.script != "" && job.exec != "" then
|
|
|
|
abort "Job ${job.name} has both a `script' and `exec' attribute."
|
|
|
|
else if job.script != "" then
|
|
|
|
''
|
|
|
|
script
|
|
|
|
${job.script}
|
|
|
|
end script
|
|
|
|
''
|
|
|
|
else if job.exec != "" then
|
|
|
|
''
|
|
|
|
exec ${job.exec}
|
|
|
|
''
|
|
|
|
else
|
|
|
|
# Simulate jobs without a main process (which Upstart 0.3
|
|
|
|
# doesn't support) using a semi-infinite sleep.
|
|
|
|
''
|
|
|
|
exec sleep 1e100
|
|
|
|
''
|
|
|
|
}
|
|
|
|
|
|
|
|
${if job.respawn && !job.task then "respawn" else ""}
|
2009-07-16 14:46:49 +01:00
|
|
|
|
|
|
|
${if job.postStop != "" then ''
|
|
|
|
stop script
|
|
|
|
${job.postStop}
|
|
|
|
end script
|
|
|
|
'' else ""}
|
2009-09-04 16:27:52 +01:00
|
|
|
|
|
|
|
${job.extraConfig}
|
2009-07-15 16:24:11 +01:00
|
|
|
'';
|
|
|
|
|
|
|
|
in
|
|
|
|
pkgs.runCommand ("upstart-" + job.name)
|
2009-10-12 18:09:38 +01:00
|
|
|
{ inherit (job) buildHook; inherit jobText; }
|
2009-07-15 16:24:11 +01:00
|
|
|
''
|
|
|
|
eval "$buildHook"
|
|
|
|
ensureDir $out/etc/event.d
|
|
|
|
echo "$jobText" > $out/etc/event.d/${job.name}
|
|
|
|
'';
|
|
|
|
|
2009-07-15 13:52:32 +01:00
|
|
|
jobs =
|
2009-10-12 18:32:30 +01:00
|
|
|
[pkgs.upstart] # for the built-in logd job
|
2009-10-12 19:09:34 +01:00
|
|
|
++ map (job: job.upstartPkg) (attrValues config.jobs);
|
2009-10-06 10:30:13 +01:00
|
|
|
|
2009-05-27 11:32:30 +01:00
|
|
|
# Create an etc/event.d directory containing symlinks to the
|
|
|
|
# specified list of Upstart job files.
|
2009-07-15 12:38:17 +01:00
|
|
|
jobsDir = pkgs.runCommand "upstart-jobs" {inherit jobs;}
|
2009-05-27 11:32:30 +01:00
|
|
|
''
|
|
|
|
ensureDir $out/etc/event.d
|
|
|
|
for i in $jobs; do
|
|
|
|
if ln -s $i . ; then
|
|
|
|
if test -d $i; then
|
|
|
|
ln -s $i/etc/event.d/* $out/etc/event.d/
|
|
|
|
fi
|
|
|
|
else
|
|
|
|
echo Duplicate entry: $i;
|
|
|
|
fi;
|
|
|
|
done
|
2009-05-28 00:30:29 +01:00
|
|
|
''; # */
|
2009-01-02 16:06:41 +00:00
|
|
|
|
2009-10-06 10:30:13 +01:00
|
|
|
# !! remove extra indentations.
|
|
|
|
jobOptions = {
|
2009-07-15 14:41:00 +01:00
|
|
|
|
|
|
|
name = mkOption {
|
2009-10-06 10:30:13 +01:00
|
|
|
# !!! The type should ensure that this could be a filename.
|
2009-07-15 14:41:00 +01:00
|
|
|
type = types.string;
|
|
|
|
example = "sshd";
|
|
|
|
description = ''
|
|
|
|
Name of the Upstart job.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
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>.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2009-07-15 16:24:11 +01:00
|
|
|
description = mkOption {
|
|
|
|
type = types.string;
|
|
|
|
default = "(no description given)";
|
|
|
|
description = ''
|
|
|
|
A short description of this job.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
startOn = mkOption {
|
2009-07-16 15:51:49 +01:00
|
|
|
# !!! Re-enable this once we're on Upstart >= 0.6.
|
|
|
|
#type = types.string;
|
2009-07-15 16:24:11 +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;
|
2009-07-16 14:55:11 +01:00
|
|
|
default = "shutdown";
|
2009-07-15 16:24:11 +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
|
2009-07-16 14:46:49 +01:00
|
|
|
(i.e. before the job's main process is started).
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
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).
|
2009-07-15 16:24:11 +01:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
exec = mkOption {
|
|
|
|
type = types.string;
|
|
|
|
default = "";
|
|
|
|
description = ''
|
2009-07-16 14:46:49 +01:00
|
|
|
Command to start the job's main process. If empty, the
|
|
|
|
job has no main process, but can still have pre/post-start
|
|
|
|
and pre/post-stop scripts, and is considered "running"
|
|
|
|
until it is stopped.
|
2009-07-15 16:24:11 +01:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2009-07-16 15:51:49 +01:00
|
|
|
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.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2009-07-15 16:53:39 +01:00
|
|
|
respawn = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = true;
|
|
|
|
description = ''
|
|
|
|
Whether to restart the job automatically if its process
|
|
|
|
ends unexpectedly.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2009-07-16 15:51:49 +01:00
|
|
|
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-07-15 16:24:11 +01:00
|
|
|
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-09-04 16:27:52 +01:00
|
|
|
extraConfig = mkOption {
|
|
|
|
type = types.string;
|
|
|
|
default = "";
|
|
|
|
example = "limit nofile 4096 4096";
|
|
|
|
description = ''
|
|
|
|
Additional Upstart stanzas not otherwise supported.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2009-07-15 14:41:00 +01:00
|
|
|
};
|
2009-10-05 19:31:27 +01:00
|
|
|
|
2009-10-06 10:30:13 +01:00
|
|
|
upstartJob = {name, config, ...}: {
|
|
|
|
options = {
|
|
|
|
upstartPkg = mkOption {
|
|
|
|
default = makeJob config;
|
|
|
|
type = types.uniq types.package;
|
|
|
|
description = ''
|
|
|
|
Upstart package which contains upstart events inside
|
|
|
|
<filename>/etc/event.d/</filename>. The default value is
|
|
|
|
generated from other options.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
config = {
|
|
|
|
# The default name is the name extracted from the attribute path.
|
|
|
|
name = mkDefaultValue (
|
|
|
|
replaceChars ["<" ">" "*"] ["_" "_" "_name_"] name
|
|
|
|
);
|
|
|
|
};
|
|
|
|
};
|
2009-10-05 19:31:27 +01:00
|
|
|
|
|
|
|
in
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
###### interface
|
|
|
|
|
|
|
|
options = {
|
|
|
|
|
2009-10-12 19:09:34 +01:00
|
|
|
jobs = mkOption {
|
2009-10-05 19:31:27 +01:00
|
|
|
default = {};
|
|
|
|
description = ''
|
|
|
|
This option defines the system jobs started and managed by the
|
|
|
|
Upstart daemon.
|
|
|
|
'';
|
|
|
|
type = types.attrsOf types.optionSet;
|
2009-10-06 10:30:13 +01:00
|
|
|
options = [ jobOptions upstartJob ];
|
2009-10-05 19:31:27 +01:00
|
|
|
};
|
|
|
|
|
2009-07-15 10:06:36 +01: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>).
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2008-11-18 18:00:09 +00:00
|
|
|
};
|
|
|
|
|
2009-07-15 10:06:36 +01:00
|
|
|
|
|
|
|
###### implementation
|
|
|
|
|
|
|
|
config = {
|
|
|
|
|
|
|
|
environment.etc =
|
|
|
|
[ { # The Upstart events defined above.
|
2009-07-15 12:38:17 +01:00
|
|
|
source = "${jobsDir}/etc/event.d";
|
2009-07-15 10:06:36 +01:00
|
|
|
target = "event.d";
|
|
|
|
}
|
|
|
|
];
|
|
|
|
|
2009-10-12 17:36:19 +01:00
|
|
|
# !!! fix this
|
2009-07-15 10:06:36 +01:00
|
|
|
tests.upstartJobs = { recurseForDerivations = true; } //
|
2009-10-06 10:30:13 +01:00
|
|
|
builtins.listToAttrs (map (job: {
|
|
|
|
name = removePrefix "upstart-" job.name;
|
|
|
|
value = job;
|
|
|
|
}) jobs);
|
2009-07-15 10:06:36 +01:00
|
|
|
|
2008-11-18 18:00:09 +00:00
|
|
|
};
|
2009-07-15 10:06:36 +01:00
|
|
|
|
2009-01-02 16:07:21 +00:00
|
|
|
}
|