2011-01-09 22:00:41 +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
|
|
|
|
2010-02-15 15:55:54 +00:00
|
|
|
upstart = pkgs.upstart;
|
2009-11-06 09:36:35 +00:00
|
|
|
|
2012-03-04 12:58:06 +00: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 09:36:35 +00:00
|
|
|
|
2012-07-19 21:41:42 +00:00
|
|
|
makeJobScript = name: content: "${pkgs.writeScriptBin name content}/bin/${name}";
|
|
|
|
|
2012-06-16 04:19:43 +00:00
|
|
|
# From a job description, generate an systemd unit file.
|
|
|
|
makeUnit = job:
|
2009-07-15 15:24:11 +00:00
|
|
|
|
|
|
|
let
|
2010-06-11 21:44:06 +00:00
|
|
|
hasMain = job.script != "" || job.exec != "";
|
2009-07-15 15:24:11 +00:00
|
|
|
|
2011-01-09 22:00:41 +00:00
|
|
|
env = config.system.upstartEnvironment // job.environment;
|
|
|
|
|
2012-07-19 21:41:42 +00:00
|
|
|
preStartScript = makeJobScript "${job.name}-pre-start.sh"
|
2009-07-15 15:24:11 +00:00
|
|
|
''
|
2012-06-16 04:19:43 +00:00
|
|
|
#! ${pkgs.stdenv.shell} -e
|
|
|
|
${job.preStart}
|
|
|
|
'';
|
|
|
|
|
2012-07-19 21:41:42 +00:00
|
|
|
startScript = makeJobScript "${job.name}-start.sh"
|
2012-06-16 04:19:43 +00:00
|
|
|
''
|
|
|
|
#! ${pkgs.stdenv.shell} -e
|
|
|
|
${if job.script != "" then job.script else ''
|
|
|
|
exec ${job.exec}
|
|
|
|
''}
|
|
|
|
'';
|
|
|
|
|
2012-07-19 21:41:42 +00:00
|
|
|
postStartScript = makeJobScript "${job.name}-post-start.sh"
|
2012-06-16 04:19:43 +00:00
|
|
|
''
|
|
|
|
#! ${pkgs.stdenv.shell} -e
|
|
|
|
${job.postStart}
|
|
|
|
'';
|
|
|
|
|
2012-07-19 21:41:42 +00:00
|
|
|
preStopScript = makeJobScript "${job.name}-pre-stop.sh"
|
2012-06-16 04:19:43 +00:00
|
|
|
''
|
|
|
|
#! ${pkgs.stdenv.shell} -e
|
|
|
|
${job.preStop}
|
|
|
|
'';
|
|
|
|
|
2012-07-19 21:41:42 +00:00
|
|
|
postStopScript = makeJobScript "${job.name}-post-stop.sh"
|
2012-06-16 04:19:43 +00:00
|
|
|
''
|
|
|
|
#! ${pkgs.stdenv.shell} -e
|
|
|
|
${job.postStop}
|
|
|
|
'';
|
2012-06-18 03:31:21 +00:00
|
|
|
in {
|
2012-06-16 04:19:43 +00:00
|
|
|
|
2012-06-18 21:42:26 +00:00
|
|
|
inherit (job) description path environment;
|
|
|
|
|
|
|
|
after =
|
|
|
|
if job.startOn == "stopped udevtrigger" then [ "systemd-udev-settle.service" ] else
|
|
|
|
if job.startOn == "started udev" then [ "systemd-udev.service" ] else
|
|
|
|
[];
|
2012-06-16 04:19:43 +00:00
|
|
|
|
2012-06-18 21:42:26 +00:00
|
|
|
wantedBy = if job.startOn == "" then [ ] else [ "multi-user.target" ];
|
2011-09-14 18:20:50 +00:00
|
|
|
|
2012-06-18 21:42:26 +00:00
|
|
|
serviceConfig =
|
|
|
|
''
|
2012-06-16 04:19:43 +00:00
|
|
|
${optionalString (job.preStart != "" && (job.script != "" || job.exec != "")) ''
|
|
|
|
ExecStartPre=${preStartScript}
|
|
|
|
''}
|
|
|
|
|
|
|
|
${optionalString (job.preStart != "" && job.script == "" && job.exec == "") ''
|
|
|
|
ExecStart=${preStartScript}
|
|
|
|
''}
|
|
|
|
|
|
|
|
${optionalString (job.script != "" || job.exec != "") ''
|
|
|
|
ExecStart=${startScript}
|
|
|
|
''}
|
|
|
|
|
|
|
|
${optionalString (job.postStart != "") ''
|
|
|
|
ExecStartPost=${postStartScript}
|
|
|
|
''}
|
|
|
|
|
|
|
|
${optionalString (job.preStop != "") ''
|
|
|
|
ExecStop=${preStopScript}
|
|
|
|
''}
|
|
|
|
|
|
|
|
${optionalString (job.postStop != "") ''
|
|
|
|
ExecStopPost=${postStopScript}
|
|
|
|
''}
|
|
|
|
|
|
|
|
${if job.script == "" && job.exec == "" then "Type=oneshot\nRemainAfterExit=true" else
|
|
|
|
if job.daemonType == "fork" then "Type=forking\nGuessMainPID=true" else
|
|
|
|
if job.daemonType == "none" then "" else
|
|
|
|
throw "invalid daemon type `${job.daemonType}'"}
|
|
|
|
|
|
|
|
${optionalString (!job.task && job.respawn) "Restart=always"}
|
|
|
|
'';
|
2012-06-18 03:31:21 +00:00
|
|
|
};
|
|
|
|
|
2012-03-17 19:12:33 +00:00
|
|
|
|
2009-10-06 09:30:13 +00:00
|
|
|
jobOptions = {
|
2009-07-15 13:41:00 +00:00
|
|
|
|
2009-11-06 09:26:36 +00: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 15:27:52 +00:00
|
|
|
|
2009-11-06 09:26:36 +00:00
|
|
|
description = mkOption {
|
|
|
|
type = types.string;
|
2012-03-04 16:17:56 +00:00
|
|
|
default = "";
|
2009-11-06 09:26:36 +00:00
|
|
|
description = ''
|
|
|
|
A short description of this job.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
startOn = mkOption {
|
|
|
|
# !!! Re-enable this once we're on Upstart >= 0.6.
|
2011-09-14 18:20:50 +00:00
|
|
|
#type = types.string;
|
2009-11-06 09:26:36 +00: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 16:04:08 +00:00
|
|
|
default = "starting shutdown";
|
2009-11-06 09:26:36 +00: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 15:23:16 +00: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 22:45:19 +00: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 15:23:16 +00:00
|
|
|
};
|
|
|
|
|
2009-11-06 09:26:36 +00: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 15:23:16 +00:00
|
|
|
and pre/post-stop scripts, and is considered “running”
|
2009-11-06 09:26:36 +00: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.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2012-03-18 02:10:39 +00:00
|
|
|
restartIfChanged = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = true;
|
|
|
|
description = ''
|
|
|
|
Whether the job should be restarted if it has changed after a
|
|
|
|
NixOS configuration switch.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2009-11-06 09:26:36 +00: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.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
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 15:23:16 +00: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 12:58:06 +00: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.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2009-11-06 09:26:36 +00:00
|
|
|
extraConfig = mkOption {
|
|
|
|
type = types.string;
|
|
|
|
default = "";
|
|
|
|
example = "limit nofile 4096 4096";
|
|
|
|
description = ''
|
|
|
|
Additional Upstart stanzas not otherwise supported.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2010-09-14 11:22:17 +00:00
|
|
|
path = mkOption {
|
2012-06-18 21:42:26 +00:00
|
|
|
default = [];
|
2010-09-14 11:22:17 +00:00
|
|
|
description = ''
|
|
|
|
Packages added to the job's <envar>PATH</envar> environment variable.
|
2011-09-14 18:20:50 +00:00
|
|
|
Both the <filename>bin</filename> and <filename>sbin</filename>
|
2010-09-14 11:22:17 +00:00
|
|
|
subdirectories of each package are added.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2012-03-18 17:48:19 +00:00
|
|
|
console = mkOption {
|
|
|
|
default = "";
|
|
|
|
example = "console";
|
|
|
|
description = ''
|
|
|
|
If set to <literal>output</literal>, job output is written to
|
|
|
|
the console. If it's <literal>owner</literal>, additionally
|
|
|
|
the job becomes owner of the console. It it's empty (the
|
|
|
|
default), output is written to
|
|
|
|
<filename>/var/log/upstart/<replaceable>jobname</replaceable></filename>
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2009-11-06 09:26:36 +00:00
|
|
|
};
|
2009-10-05 18:31:27 +00:00
|
|
|
|
2011-09-14 18:20:50 +00:00
|
|
|
|
2009-10-06 09:30:13 +00:00
|
|
|
upstartJob = {name, config, ...}: {
|
2011-09-14 18:20:50 +00:00
|
|
|
|
2009-10-06 09:30:13 +00:00
|
|
|
options = {
|
2012-02-20 19:23:15 +00:00
|
|
|
|
2012-06-18 03:31:21 +00:00
|
|
|
unit = mkOption {
|
2012-06-16 04:19:43 +00:00
|
|
|
default = makeUnit config;
|
2012-06-18 03:31:21 +00:00
|
|
|
description = "Generated definition of the systemd unit corresponding to this job.";
|
2009-10-06 09:30:13 +00:00
|
|
|
};
|
2012-02-20 19:23:15 +00:00
|
|
|
|
2009-10-06 09:30:13 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
config = {
|
2012-02-20 19:23:15 +00:00
|
|
|
|
2009-10-06 09:30:13 +00:00
|
|
|
# The default name is the name extracted from the attribute path.
|
2012-03-19 18:07:28 +00:00
|
|
|
name = mkDefaultValue name;
|
2012-02-20 19:23:15 +00:00
|
|
|
|
|
|
|
# Default path for Upstart jobs. Should be quite minimal.
|
|
|
|
path =
|
|
|
|
[ pkgs.coreutils
|
|
|
|
pkgs.findutils
|
|
|
|
pkgs.gnugrep
|
|
|
|
pkgs.gnused
|
|
|
|
upstart
|
|
|
|
];
|
|
|
|
|
2009-10-06 09:30:13 +00:00
|
|
|
};
|
2011-09-14 18:20:50 +00:00
|
|
|
|
2009-10-06 09:30:13 +00:00
|
|
|
};
|
2009-10-05 18:31:27 +00:00
|
|
|
|
|
|
|
in
|
2011-09-14 18:20:50 +00:00
|
|
|
|
2009-10-05 18:31:27 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
###### interface
|
2011-09-14 18:20:50 +00:00
|
|
|
|
2009-10-05 18:31:27 +00:00
|
|
|
options = {
|
|
|
|
|
2009-10-12 18:09:34 +00:00
|
|
|
jobs = mkOption {
|
2009-10-05 18:31:27 +00:00
|
|
|
default = {};
|
|
|
|
description = ''
|
|
|
|
This option defines the system jobs started and managed by the
|
|
|
|
Upstart daemon.
|
|
|
|
'';
|
2011-07-27 20:55:35 +00:00
|
|
|
type = types.loaOf types.optionSet;
|
2009-10-06 09:30:13 +00:00
|
|
|
options = [ jobOptions upstartJob ];
|
2009-10-05 18:31:27 +00:00
|
|
|
};
|
2011-09-14 18:20:50 +00:00
|
|
|
|
2011-01-09 22:00:41 +00:00
|
|
|
system.upstartEnvironment = mkOption {
|
|
|
|
type = types.attrs;
|
|
|
|
default = {};
|
|
|
|
example = { TZ = "CET"; };
|
|
|
|
description = ''
|
|
|
|
Environment variables passed to <emphasis>all</emphasis> Upstart jobs.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2008-11-18 18:00:09 +00:00
|
|
|
};
|
|
|
|
|
2009-07-15 09:06:36 +00:00
|
|
|
|
|
|
|
###### implementation
|
2011-09-14 18:20:50 +00:00
|
|
|
|
2009-07-15 09:06:36 +00:00
|
|
|
config = {
|
|
|
|
|
2012-07-20 22:25:23 +00:00
|
|
|
system.build.upstart = "/no-upstart";
|
2009-11-06 09:36:35 +00:00
|
|
|
|
2012-06-18 21:42:26 +00:00
|
|
|
boot.systemd.services =
|
2012-06-16 04:19:43 +00:00
|
|
|
flip mapAttrs' config.jobs (name: job:
|
2012-06-18 03:31:21 +00:00
|
|
|
nameValuePair "${job.name}.service" job.unit);
|
2012-06-16 04:19:43 +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
|
|
|
}
|