To ease migration to systemd, generate units from the ‘jobs’ option

Also get rid of the ‘buildHook’ job option because it wasn't very useful.
This commit is contained in:
Eelco Dolstra 2012-06-16 00:19:43 -04:00
parent 66f4d10843
commit 4a95f8996b
15 changed files with 108 additions and 172 deletions

View File

@ -67,7 +67,7 @@ in
'' + optionalString config.services.nscd.enable ''
# Invalidate the nscd cache whenever resolv.conf is
# regenerated.
libc_restart='${pkgs.upstart}/sbin/start invalidate-nscd'
libc_restart='${pkgs.systemd}/bin/systemctl start invalidate-nscd.service'
'' );
target = "resolvconf.conf";
}

View File

@ -199,9 +199,6 @@
./system/boot/stage-2.nix
./system/boot/systemd.nix
./system/etc/etc.nix
./system/upstart-events/control-alt-delete.nix
./system/upstart-events/runlevel.nix
./system/upstart-events/shutdown.nix
./system/upstart/upstart.nix
./tasks/cpu-freq.nix
./tasks/filesystems.nix

View File

@ -159,12 +159,7 @@ in
environment.systemPackages = [ pkgs.nagios ];
jobs.nagios =
{ # Run `nagios -v' to check the validity of the configuration file so
# that a nixos-rebuild fails *before* we kill the running Nagios
# daemon.
buildHook = "${pkgs.nagios}/bin/nagios -v ${nagiosCfgFile}";
description = "Nagios monitoring daemon";
{ description = "Nagios monitoring daemon";
startOn = "started network-interfaces";
stopOn = "stopping network-interfaces";

View File

@ -329,7 +329,6 @@ in
${pkgs.openssh}/sbin/sshd -h ${cfg.hostKeyPath} \
-f ${pkgs.writeText "sshd_config" cfg.extraConfig}
Restart=always
RestartSec=5
Type=forking
KillMode=process
PIDFile=/run/sshd.pid

View File

@ -20,15 +20,14 @@ in
enable = mkOption {
default = true;
description = "
Whether to enable the Name Service Cache Daemon.
";
description = "Whether to enable the Name Service Cache Daemon.";
};
};
};
###### implementation
config = mkIf config.services.nscd.enable {

View File

@ -56,6 +56,7 @@ with pkgs.lib;
config = {
# Generate a separate job for each tty.
/*
jobs = listToAttrs (map (tty: nameValuePair tty {
startOn =
@ -72,6 +73,7 @@ with pkgs.lib;
environment.LOCALE_ARCHIVE = "/var/run/current-system/sw/lib/locale/locale-archive";
}) config.services.mingetty.ttys);
*/
environment.etc = singleton
{ # Friendly greeting on the virtual consoles.

View File

@ -532,18 +532,7 @@ in
'';
jobs.httpd =
{ # Statically verify the syntactic correctness of the generated
# httpd.conf. !!! this is impure! It doesn't just check for
# syntax, but also whether the Apache user/group exist,
# whether SSL keys exist, etc.
buildHook =
''
echo
echo '=== Checking the generated Apache configuration file ==='
${httpd}/bin/httpd -f ${httpdConf} -t || true
'';
description = "Apache HTTPD";
{ description = "Apache HTTPD";
startOn = "started networking and filesystem"
# Hacky. Some subservices depend on Postgres

View File

@ -192,4 +192,4 @@ fi
echo "starting systemd..."
PATH=/var/run/current-system/systemd/lib/systemd \
MODULE_DIR=/var/run/current-system/kernel-modules/lib/modules \
exec systemd --log-target journal --log-level debug --crash-shell
exec systemd --log-target journal # --log-level debug --crash-shell

View File

@ -111,7 +111,7 @@ let
nixosUnits = mapAttrsToList makeUnit config.boot.systemd.units;
systemUnits = pkgs.runCommand "system-units" { }
units = pkgs.runCommand "units" { preferLocalBuild = true; }
''
mkdir -p $out/system
for i in ${toString upstreamUnits}; do
@ -161,10 +161,12 @@ in
system.build.systemd = systemd;
system.build.units = units;
environment.systemPackages = [ systemd ];
environment.etc =
[ { source = systemUnits;
[ { source = units;
target = "systemd";
}
];
@ -177,7 +179,7 @@ in
After=multi-user.target
Conflicts=rescue.target
AllowIsolate=yes
Wants=sshd.service autovt@tty1.service # FIXME
Wants=sshd.service
'';
boot.systemd.units."getty@.service" =

View File

@ -1,26 +0,0 @@
{ config, pkgs, ... }:
###### implementation
{
jobs.control_alt_delete =
{ name = "control-alt-delete";
startOn = "control-alt-delete";
task = true;
script =
''
shutdown -r now 'Ctrl-Alt-Delete pressed'
'';
};
system.activationScripts.poweroff =
''
# Allow the kernel to find the poweroff command. This is used
# (for instance) by Xen's "xm shutdown" command to signal a
# guest to shut down cleanly.
echo ${config.system.build.upstart}/sbin/poweroff > /proc/sys/kernel/poweroff_cmd
'';
}

View File

@ -1,38 +0,0 @@
{ config, pkgs, ... }:
with pkgs.lib;
{
# After booting, go to runlevel 2. (NixOS doesn't really use
# runlevels, but this keeps wtmp happy.)
jobs.boot =
{ name = "boot";
startOn = "startup";
task = true;
restartIfChanged = false;
script = "telinit 2";
};
jobs.runlevel =
{ name = "runlevel";
startOn = "runlevel [0123456S]";
task = true;
restartIfChanged = false;
script =
''
case "$RUNLEVEL" in
0) initctl start shutdown --no-wait MODE=poweroff;;
1) initctl start shutdown --no-wait MODE=maintenance;;
2) true;;
6) initctl start shutdown --no-wait MODE=reboot;;
*) echo "Unsupported runlevel: $RUNLEVEL";;
esac
'';
};
}

View File

@ -12,19 +12,90 @@ let
groupExists = g:
(g == "") || any (gg: gg.name == g) (attrValues config.users.extraGroups);
# From a job description, generate an Upstart job file.
makeJob = job:
# From a job description, generate an systemd unit file.
makeUnit = job:
let
hasMain = job.script != "" || job.exec != "";
env = config.system.upstartEnvironment // job.environment;
jobText =
let log = "/var/log/upstart/${job.name}"; in
preStartScript = pkgs.writeScript "${job.name}-pre-start.sh"
''
# Upstart job `${job.name}'. This is a generated file. Do not edit.
#! ${pkgs.stdenv.shell} -e
${job.preStart}
'';
startScript = pkgs.writeScript "${job.name}-start.sh"
''
#! ${pkgs.stdenv.shell} -e
${if job.script != "" then job.script else ''
exec ${job.exec}
''}
'';
postStartScript = pkgs.writeScript "${job.name}-post-start.sh"
''
#! ${pkgs.stdenv.shell} -e
${job.postStart}
'';
preStopScript = pkgs.writeScript "${job.name}-pre-stop.sh"
''
#! ${pkgs.stdenv.shell} -e
${job.preStop}
'';
postStopScript = pkgs.writeScript "${job.name}-post-stop.sh"
''
#! ${pkgs.stdenv.shell} -e
${job.postStop}
'';
text =
''
[Unit]
Description=${job.description}
[Service]
Environment=PATH=${job.path}
${concatMapStrings (n: "Environment=${n}=\"${getAttr n env}\"\n") (attrNames env)}
${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"}
'';
/*
text =
''
${optionalString (job.description != "") ''
description "${job.description}"
''}
@ -45,9 +116,6 @@ let
${optionalString (job.console != "") "console ${job.console}"}
pre-start script
${optionalString (job.console == "") ''
exec >> ${log} 2>&1
''}
ln -sfn "$(readlink -f "/etc/init/${job.name}.conf")" /var/run/upstart-jobs/${job.name}
${optionalString (job.preStart != "") ''
source ${jobHelpers}
@ -60,9 +128,6 @@ let
else if job.script != "" then
''
script
${optionalString (job.console == "") ''
exec >> ${log} 2>&1
''}
source ${jobHelpers}
${job.script}
end script
@ -70,7 +135,6 @@ let
else if job.exec != "" && job.console == "" then
''
script
exec >> ${log} 2>&1
exec ${job.exec}
end script
''
@ -83,9 +147,6 @@ let
${optionalString (job.postStart != "") ''
post-start script
${optionalString (job.console == "") ''
exec >> ${log} 2>&1
''}
source ${jobHelpers}
${job.postStart}
end script
@ -98,9 +159,6 @@ let
# (upstart 0.6.5, job.c:562)
optionalString (job.preStop != "") (assert hasMain; ''
pre-stop script
${optionalString (job.console == "") ''
exec >> ${log} 2>&1
''}
source ${jobHelpers}
${job.preStop}
end script
@ -108,9 +166,6 @@ let
${optionalString (job.postStop != "") ''
post-stop script
${optionalString (job.console == "") ''
exec >> ${log} 2>&1
''}
source ${jobHelpers}
${job.postStop}
end script
@ -132,14 +187,9 @@ let
${job.extraConfig}
'';
*/
in
pkgs.runCommand ("upstart-" + job.name + ".conf")
{ inherit (job) buildHook; inherit jobText; preferLocalBuild = true; }
''
eval "$buildHook"
echo "$jobText" > $out
'';
in text;
# Shell functions for use in Upstart jobs.
@ -199,17 +249,6 @@ let
'';
};
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>.
'';
};
description = mkOption {
type = types.string;
default = "";
@ -401,13 +440,10 @@ let
options = {
jobDrv = mkOption {
default = makeJob config;
type = types.uniq types.package;
description = ''
Derivation that builds the Upstart job file. The default
value is generated from other options.
'';
unitText = mkOption {
default = makeUnit config;
type = types.uniq types.string;
description = "Generated text of the systemd unit corresponding to this job.";
};
};
@ -448,16 +484,6 @@ in
options = [ jobOptions upstartJob ];
};
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>).
'';
};
system.upstartEnvironment = mkOption {
type = types.attrs;
default = {};
@ -476,25 +502,10 @@ in
system.build.upstart = upstart;
/*
environment.etc =
flip map (attrValues config.jobs) (job:
{ source = job.jobDrv;
target = "init/${job.name}.conf";
} );
# Upstart can listen on the system bus, allowing normal users to
# do status queries.
services.dbus.packages = [ upstart ];
system.activationScripts.chownJobLogs = stringAfter ["var"]
(concatMapStrings (job: ''
touch /var/log/upstart/${job.name}
${optionalString (job.setuid != "") "chown ${job.setuid} /var/log/upstart/${job.name}"}
${optionalString (job.setgid != "") "chown :${job.setgid} /var/log/upstart/${job.name}"}
'') (attrValues config.jobs));
*/
boot.systemd.units =
flip mapAttrs' config.jobs (name: job:
nameValuePair "${job.name}.service" job.unitText);
};
}

View File

@ -174,7 +174,7 @@ in
system.fsPackages = [ pkgs.dosfstools ];
environment.systemPackages =
[ pkgs.ntfs3g pkgs.cifs_utils pkgs.mountall ]
[ pkgs.ntfs3g pkgs.cifs_utils ]
++ config.system.fsPackages;
environment.etc = singleton
@ -182,6 +182,7 @@ in
target = "fstab";
};
/*
jobs.mountall =
{ startOn = "started udev or config-changed";
@ -309,6 +310,7 @@ in
initctl start --no-wait mountall
'';
};
*/
};

View File

@ -54,8 +54,10 @@ in
inherit requiredTTYs; # pass it to ./modules/tasks/tty-backgrounds.nix
environment.systemPackages = [pkgs.kbd];
environment.systemPackages = [ pkgs.kbd ];
/* FIXME - remove; this is handled by systemd now.
jobs.kbd =
{ description = "Keyboard / console initialisation";
@ -120,6 +122,7 @@ in
${pkgs.kbd}/bin/loadkeys '${consoleKeyMap}'
'';
};
*/
};

View File

@ -267,7 +267,8 @@ in
${optionalString (cfg.interfaces != [] || cfg.localCommands != "") ''
# Emit the ip-up event (e.g. to start ntpd).
initctl emit -n ip-up
#FIXME
#initctl emit -n ip-up
''}
'';
};