2008-12-04 15:48:27 +00:00
|
|
|
{config, pkgs, ...}:
|
2008-11-18 18:00:09 +00:00
|
|
|
|
2009-07-15 15:24:11 +00:00
|
|
|
with pkgs.lib;
|
2008-12-07 12:27:46 +00:00
|
|
|
|
2009-07-15 15:24:11 +00:00
|
|
|
let
|
2009-07-15 11:38:17 +00:00
|
|
|
|
2009-07-15 15:24:11 +00:00
|
|
|
# From a job description, generate an Upstart job file.
|
|
|
|
makeJob = job@{buildHook ? "", ...}:
|
|
|
|
|
|
|
|
let
|
|
|
|
|
|
|
|
jobText = if job.job != "" then job.job else
|
|
|
|
''
|
|
|
|
description "${job.description}"
|
|
|
|
|
|
|
|
${if job.startOn != "" then "start on ${job.startOn}" else ""}
|
|
|
|
${if job.stopOn != "" then "start on ${job.stopOn}" else ""}
|
|
|
|
|
|
|
|
${concatMapStrings (n: "env ${n}=${getAttr n job.environment}\n") (attrNames job.environment)}
|
|
|
|
|
|
|
|
${if job.preStart != "" then ''
|
|
|
|
start script
|
|
|
|
${job.preStart}
|
|
|
|
end script
|
|
|
|
'' else ""}
|
|
|
|
|
|
|
|
${if job.exec != "" then ''
|
|
|
|
exec ${job.exec}
|
|
|
|
'' else ""}
|
2009-07-15 15:53:39 +00:00
|
|
|
|
|
|
|
${if job.respawn then "respawn" else ""}
|
2009-07-15 15:24:11 +00:00
|
|
|
'';
|
|
|
|
|
|
|
|
in
|
|
|
|
pkgs.runCommand ("upstart-" + job.name)
|
|
|
|
{ inherit buildHook; inherit jobText; }
|
|
|
|
''
|
|
|
|
eval "$buildHook"
|
|
|
|
ensureDir $out/etc/event.d
|
|
|
|
echo "$jobText" > $out/etc/event.d/${job.name}
|
|
|
|
'';
|
|
|
|
|
|
|
|
|
2009-07-15 12:52:32 +00:00
|
|
|
jobs =
|
|
|
|
[pkgs.upstart] # for the built-in logd job
|
|
|
|
++ map makeJob (config.jobs ++ config.services.extraJobs);
|
2009-05-27 10:32:30 +00:00
|
|
|
|
|
|
|
# Create an etc/event.d directory containing symlinks to the
|
|
|
|
# specified list of Upstart job files.
|
2009-07-15 11:38:17 +00:00
|
|
|
jobsDir = pkgs.runCommand "upstart-jobs" {inherit jobs;}
|
2009-05-27 10:32:30 +00: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-27 23:30:29 +00:00
|
|
|
''; # */
|
2009-01-02 16:06:41 +00:00
|
|
|
|
2009-07-15 09:06:36 +00:00
|
|
|
in
|
|
|
|
|
2008-11-18 18:00:09 +00:00
|
|
|
{
|
2009-07-15 09:06:36 +00:00
|
|
|
|
|
|
|
###### interface
|
|
|
|
|
|
|
|
options = {
|
|
|
|
|
2009-07-15 11:19:11 +00:00
|
|
|
jobs = mkOption {
|
2009-07-15 09:06:36 +00:00
|
|
|
default = [];
|
2009-07-15 13:41:00 +00:00
|
|
|
description = ''
|
|
|
|
This option defines the system jobs started and managed by the
|
|
|
|
Upstart daemon.
|
|
|
|
'';
|
|
|
|
|
|
|
|
type = types.list types.optionSet;
|
|
|
|
|
|
|
|
options = {
|
|
|
|
|
|
|
|
name = mkOption {
|
|
|
|
type = types.string;
|
|
|
|
example = "sshd";
|
|
|
|
description = ''
|
|
|
|
Name of the Upstart job.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
job = mkOption {
|
2009-07-15 15:24:11 +00:00
|
|
|
default = "";
|
2009-07-15 13:41:00 +00:00
|
|
|
type = types.string;
|
|
|
|
example =
|
|
|
|
''
|
2009-07-15 09:06:36 +00:00
|
|
|
description "nc"
|
|
|
|
start on started network-interfaces
|
|
|
|
respawn
|
|
|
|
env PATH=/var/run/current-system/sw/bin
|
|
|
|
exec sh -c "echo 'hello world' | ${pkgs.netcat}/bin/nc -l -p 9000"
|
2009-07-15 13:41:00 +00:00
|
|
|
'';
|
|
|
|
description = ''
|
|
|
|
Contents 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 15:24:11 +00:00
|
|
|
description = mkOption {
|
|
|
|
type = types.string;
|
|
|
|
default = "(no description given)";
|
|
|
|
description = ''
|
|
|
|
A short description of this job.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
startOn = mkOption {
|
|
|
|
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;
|
|
|
|
default = "";
|
|
|
|
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 <varname>exec</varname> command is run).
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
exec = mkOption {
|
|
|
|
type = types.string;
|
|
|
|
default = "";
|
|
|
|
description = ''
|
|
|
|
Command to start the job.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2009-07-15 15:53:39 +00:00
|
|
|
respawn = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = true;
|
|
|
|
description = ''
|
|
|
|
Whether to restart the job automatically if its process
|
|
|
|
ends unexpectedly.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2009-07-15 15:24:11 +00: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-07-15 13:41:00 +00:00
|
|
|
};
|
|
|
|
|
2009-07-15 11:19:11 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
services.extraJobs = mkOption {
|
|
|
|
default = [];
|
|
|
|
description = ''
|
|
|
|
Obsolete - don't use.
|
2009-07-15 09:06:36 +00: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 09:06:36 +00:00
|
|
|
|
|
|
|
###### implementation
|
|
|
|
|
|
|
|
config = {
|
|
|
|
|
|
|
|
environment.etc =
|
|
|
|
[ { # The Upstart events defined above.
|
2009-07-15 11:38:17 +00:00
|
|
|
source = "${jobsDir}/etc/event.d";
|
2009-07-15 09:06:36 +00:00
|
|
|
target = "event.d";
|
|
|
|
}
|
|
|
|
];
|
|
|
|
|
|
|
|
# see test/test-upstart-job.sh (!!! check whether this still works)
|
|
|
|
tests.upstartJobs = { recurseForDerivations = true; } //
|
2008-11-18 18:00:09 +00:00
|
|
|
builtins.listToAttrs (map (job:
|
|
|
|
{ name = if job ? jobName then job.jobName else job.name; value = job; }
|
|
|
|
) jobs);
|
2009-07-15 09:06:36 +00:00
|
|
|
|
2008-11-18 18:00:09 +00:00
|
|
|
};
|
2009-07-15 09:06:36 +00:00
|
|
|
|
2009-01-02 16:07:21 +00:00
|
|
|
}
|