* In the jobs attribute, support a more high-level way of specifying

jobs, e.g. (from the nscd job)

    { name = "nscd";

      description = "Name Service Cache Daemon";

      startOn = "startup";
      stopOn = "shutdown";

      environment = { LD_LIBRARY_PATH = nssModulesPath; };
        
      preStart =
        ''
          mkdir -m 0755 -p /var/run/nscd
          mkdir -m 0755 -p /var/db/nscd
        '';

      exec = "${pkgs.glibc}/sbin/nscd -f ${./nscd.conf} -d 2> /dev/null";
    };

  The Upstart job is generated from this.  The main goal is to provide
  some abstraction from the Upstart syntax.  For instance, this should
  make it easier to upgrade to newer versions of Upstart, to switch to
  an entirely different process management system (e.g. initng or
  launchd), or to test a job independantly from Upstart.  (However the
  startOn and stopOn attributes are tied to Upstart's event model.)

svn path=/nixos/branches/modular-nixos/; revision=16376
This commit is contained in:
Eelco Dolstra 2009-07-15 15:24:11 +00:00
parent 0e3bef4195
commit c45cf3a28e
2 changed files with 102 additions and 33 deletions

View File

@ -17,31 +17,24 @@ in
description = "Name service cache daemon user";
};
jobs = singleton {
name = "nscd";
jobs = singleton
{ name = "nscd";
description = "Name Service Cache Daemon";
startOn = "startup";
stopOn = "shutdown";
environment = { LD_LIBRARY_PATH = nssModulesPath; };
job = ''
description \"Name Service Cache Daemon\"
start on startup
stop on shutdown
env LD_LIBRARY_PATH=${nssModulesPath}
start script
preStart =
''
mkdir -m 0755 -p /var/run/nscd
mkdir -m 0755 -p /var/db/nscd
'';
rm -f /var/db/nscd/* # for testing
end script
# !!! -d turns on debug info which probably makes nscd slower
# 2>/dev/null is to make it shut up
respawn ${pkgs.glibc}/sbin/nscd -f ${./nscd.conf} -d 2> /dev/null
'';
};
exec = "${pkgs.glibc}/sbin/nscd -f ${./nscd.conf} -d 2> /dev/null";
};
};
}

View File

@ -1,20 +1,44 @@
{config, pkgs, ...}:
with pkgs.lib;
let
inherit (pkgs.lib) mkOption mergeListOption types;
makeJob =
{name, job, buildHook ? "true", passthru ? null}:
pkgs.runCommand ("upstart-" + name)
{ inherit buildHook job; }
''
eval "$buildHook"
ensureDir $out/etc/event.d
echo "$job" > $out/etc/event.d/${name}
'';
# 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 ""}
'';
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}
'';
jobs =
[pkgs.upstart] # for the built-in logd job
++ map makeJob (config.jobs ++ config.services.extraJobs);
@ -63,6 +87,7 @@ in
};
job = mkOption {
default = "";
type = types.string;
example =
''
@ -88,6 +113,57 @@ in
'';
};
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.
'';
};
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.
'';
};
};
};