From c45cf3a28ee9dba02f37fe6c1ed84ace9dbdbadc Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Wed, 15 Jul 2009 15:24:11 +0000 Subject: [PATCH] * 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 --- modules/services/system/nscd.nix | 35 ++++------ modules/system/upstart/upstart.nix | 100 +++++++++++++++++++++++++---- 2 files changed, 102 insertions(+), 33 deletions(-) diff --git a/modules/services/system/nscd.nix b/modules/services/system/nscd.nix index 63fa36b9422..11aa48720b2 100644 --- a/modules/services/system/nscd.nix +++ b/modules/services/system/nscd.nix @@ -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"; + }; }; } diff --git a/modules/system/upstart/upstart.nix b/modules/system/upstart/upstart.nix index 1c304233e84..6933b71c027 100644 --- a/modules/system/upstart/upstart.nix +++ b/modules/system/upstart/upstart.nix @@ -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 exec 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. + ''; + }; + }; };