From 6119c399d864739674d2568abd943a41ca09788c Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Thu, 16 Jul 2009 14:51:49 +0000 Subject: [PATCH] * Support tasks in the new Upstart formalism. * Swap task: fixed removing disabled swap devices. * Swap task: specified the type of swapDevices. svn path=/nixos/branches/modular-nixos/; revision=16396 --- modules/services/misc/nix-daemon.nix | 17 ++-- modules/system/upstart/upstart.nix | 63 ++++++++++++--- modules/tasks/swap.nix | 114 ++++++++++++++++----------- 3 files changed, 126 insertions(+), 68 deletions(-) diff --git a/modules/services/misc/nix-daemon.nix b/modules/services/misc/nix-daemon.nix index ee8d67f7471..b8911a8cea3 100644 --- a/modules/services/misc/nix-daemon.nix +++ b/modules/services/misc/nix-daemon.nix @@ -236,21 +236,18 @@ in target = "nix.machines"; }; - services.extraJobs = [ + jobs = pkgs.lib.singleton { name = "nix-daemon"; - job = '' - start on startup - stop on shutdown - respawn - script + startOn = "startup"; + + script = + '' export PATH=${if config.nix.distributedBuilds then "${pkgs.openssh}/bin:" else ""}${pkgs.openssl}/bin:${nix}/bin:$PATH ${config.nix.envVars} exec ${nix}/bin/nix-worker --daemon > /dev/null 2>&1 - end script - ''; - } - ]; + ''; + }; environment.shellInit = '' diff --git a/modules/system/upstart/upstart.nix b/modules/system/upstart/upstart.nix index 1461320c001..7c9253b23f0 100644 --- a/modules/system/upstart/upstart.nix +++ b/modules/system/upstart/upstart.nix @@ -11,10 +11,19 @@ let jobText = if job.job != "" then job.job else '' + # Upstart job `${job.name}'. This is a generated file. Do not edit. + description "${job.description}" - ${if job.startOn != "" then "start on ${job.startOn}" else ""} - ${if job.stopOn != "" then "start on ${job.stopOn}" else ""} + ${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 ""} ${concatMapStrings (n: "env ${n}=${getAttr n job.environment}\n") (attrNames job.environment)} @@ -24,15 +33,27 @@ let end script '' else ""} - ${if true then - # Simulate jobs without a main process (which Upstart 0.3 - # doesn't support) using a semi-infinite sleep. - '' - exec ${if job.exec != "" then job.exec else "sleep 1e100"} - '' - else ""} + ${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 then "respawn" else ""} + ${if job.respawn && !job.task then "respawn" else ""} ${if job.postStop != "" then '' stop script @@ -134,7 +155,8 @@ in }; startOn = mkOption { - type = types.string; + # !!! Re-enable this once we're on Upstart >= 0.6. + #type = types.string; default = ""; description = '' The Upstart event that triggers this job to be started. @@ -179,6 +201,15 @@ in ''; }; + script = mkOption { + type = types.string; + default = ""; + description = '' + Shell commands executed as the job's main process. Can be + specified instead of the exec attribute. + ''; + }; + respawn = mkOption { type = types.bool; default = true; @@ -188,6 +219,16 @@ in ''; }; + 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 = {}; diff --git a/modules/tasks/swap.nix b/modules/tasks/swap.nix index 173b933d992..0f1a726ab29 100644 --- a/modules/tasks/swap.nix +++ b/modules/tasks/swap.nix @@ -2,18 +2,27 @@ let -###### interface + inherit (pkgs) utillinux; + inherit (pkgs.lib) mkOption filter types; + toPath = x: if x.device != null then x.device else "/dev/disk/by-label/${x.label}"; + +in + +{ + + ###### interface + options = { - swapDevices = pkgs.lib.mkOption { + swapDevices = mkOption { default = []; example = [ { device = "/dev/hda7"; } { device = "/var/swapfile"; } { label = "bigswap"; } ]; - description = " + description = '' The swap devices and swap files. These must have been initialised using mkswap. Each element should be an attribute set specifying either the path of the @@ -21,59 +30,70 @@ let of the swap device (label, see mkswap -L). Using a label is recommended. - "; + ''; + + type = types.list types.optionSet; + + options = { + + device = mkOption { + default = null; + example = "/dev/sda3"; + type = types.nullOr types.string; + description = '' + Path of the device. + ''; + }; + + label = mkOption { + default = null; + example = "swap"; + type = types.nullOr types.string; + description = " + Label of the device. Can be used instead of device. + "; + }; + + }; + }; }; + + ###### implementation -###### implementation + config = { - inherit (pkgs) utillinux lib; + jobs = pkgs.lib.singleton + { name = "swap"; - swapDevices = config.swapDevices; + task = true; + + startOn = ["startup" "new-devices"]; - devicesByPath = - map (x: x.device) (lib.filter (x: x ? device) swapDevices); - - devicesByLabel = - map (x: x.label) (lib.filter (x: x ? label) swapDevices); + script = + '' + swapDevices=${toString (map toPath config.swapDevices)} + + for device in $swapDevices; do + ${utillinux}/sbin/swapon "$device" || true + done -in + # Remove swap devices not listed in swapDevices. + for used in $(cat /proc/swaps | grep '^/' | sed 's/ .*//'); do + found= + for device in $swapDevices; do + device=$(readlink -f $device) + if test "$used" = "$device"; then found=1; fi + done + if test -z "$found"; then + ${utillinux}/sbin/swapoff "$used" || true + fi + done + ''; + }; + }; -{ - require = [options]; - - services.extraJobs = [{ - name = "swap"; - - job = '' - start on startup - start on new-devices - - script - for device in ${toString devicesByPath}; do - ${utillinux}/sbin/swapon "$device" || true - done - - for label in ${toString devicesByLabel}; do - ${utillinux}/sbin/swapon -L "$label" || true - done - - # Remove swap devices not listed in swapDevices. - # !!! disabled because it doesn't work with labels - #for used in $(cat /proc/swaps | grep '^/' | sed 's/ .*//'); do - # found= - # for device in $ {toString swapDevices}; do - # if test "$used" = "$device"; then found=1; fi - # done - # if test -z "$found"; then - # ${utillinux}/sbin/swapoff "$used" || true - # fi - #done - - end script - ''; - }]; }