From 4a95f8996b11ca9876e24a37d4b521925a1c33bf Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Sat, 16 Jun 2012 00:19:43 -0400 Subject: [PATCH] =?UTF-8?q?To=20ease=20migration=20to=20systemd,=20generat?= =?UTF-8?q?e=20units=20from=20the=20=E2=80=98jobs=E2=80=99=20option?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Also get rid of the ‘buildHook’ job option because it wasn't very useful. --- modules/config/networking.nix | 2 +- modules/module-list.nix | 3 - .../services/monitoring/nagios/default.nix | 7 +- modules/services/networking/ssh/sshd.nix | 1 - modules/services/system/nscd.nix | 5 +- modules/services/ttys/mingetty.nix | 2 + .../web-servers/apache-httpd/default.nix | 13 +- modules/system/boot/stage-2-init.sh | 2 +- modules/system/boot/systemd.nix | 8 +- .../upstart-events/control-alt-delete.nix | 26 --- modules/system/upstart-events/runlevel.nix | 38 ----- modules/system/upstart/upstart.nix | 161 ++++++++++-------- modules/tasks/filesystems.nix | 4 +- modules/tasks/kbd.nix | 5 +- modules/tasks/network-interfaces.nix | 3 +- 15 files changed, 108 insertions(+), 172 deletions(-) delete mode 100644 modules/system/upstart-events/control-alt-delete.nix delete mode 100644 modules/system/upstart-events/runlevel.nix diff --git a/modules/config/networking.nix b/modules/config/networking.nix index 5065fc22f2ce..757bb9e85e0c 100644 --- a/modules/config/networking.nix +++ b/modules/config/networking.nix @@ -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"; } diff --git a/modules/module-list.nix b/modules/module-list.nix index 0b324e7fffd2..04fa9c58ec6c 100644 --- a/modules/module-list.nix +++ b/modules/module-list.nix @@ -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 diff --git a/modules/services/monitoring/nagios/default.nix b/modules/services/monitoring/nagios/default.nix index 6d2fe3f2acec..3c32a3c25ec9 100644 --- a/modules/services/monitoring/nagios/default.nix +++ b/modules/services/monitoring/nagios/default.nix @@ -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"; diff --git a/modules/services/networking/ssh/sshd.nix b/modules/services/networking/ssh/sshd.nix index e4ca7f39a568..9710082f4207 100644 --- a/modules/services/networking/ssh/sshd.nix +++ b/modules/services/networking/ssh/sshd.nix @@ -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 diff --git a/modules/services/system/nscd.nix b/modules/services/system/nscd.nix index 6f35cd30f582..b2596dbb4139 100644 --- a/modules/services/system/nscd.nix +++ b/modules/services/system/nscd.nix @@ -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 { diff --git a/modules/services/ttys/mingetty.nix b/modules/services/ttys/mingetty.nix index 85db3f8966e1..8304ca180cb8 100644 --- a/modules/services/ttys/mingetty.nix +++ b/modules/services/ttys/mingetty.nix @@ -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. diff --git a/modules/services/web-servers/apache-httpd/default.nix b/modules/services/web-servers/apache-httpd/default.nix index ce3311a5f598..259847d07261 100644 --- a/modules/services/web-servers/apache-httpd/default.nix +++ b/modules/services/web-servers/apache-httpd/default.nix @@ -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 diff --git a/modules/system/boot/stage-2-init.sh b/modules/system/boot/stage-2-init.sh index 384c9e301cb8..8238fc1537ea 100644 --- a/modules/system/boot/stage-2-init.sh +++ b/modules/system/boot/stage-2-init.sh @@ -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 diff --git a/modules/system/boot/systemd.nix b/modules/system/boot/systemd.nix index 0d4256990e7e..c5a646208cb0 100644 --- a/modules/system/boot/systemd.nix +++ b/modules/system/boot/systemd.nix @@ -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" = diff --git a/modules/system/upstart-events/control-alt-delete.nix b/modules/system/upstart-events/control-alt-delete.nix deleted file mode 100644 index f0f146160e58..000000000000 --- a/modules/system/upstart-events/control-alt-delete.nix +++ /dev/null @@ -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 - ''; -} diff --git a/modules/system/upstart-events/runlevel.nix b/modules/system/upstart-events/runlevel.nix deleted file mode 100644 index 016217ea3a67..000000000000 --- a/modules/system/upstart-events/runlevel.nix +++ /dev/null @@ -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 - ''; - }; - -} diff --git a/modules/system/upstart/upstart.nix b/modules/system/upstart/upstart.nix index 284f1aafd37d..b09fdc7d676f 100644 --- a/modules/system/upstart/upstart.nix +++ b/modules/system/upstart/upstart.nix @@ -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 - httpd.conf. - ''; - }; - 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., - nix-build /etc/nixos/nixos -A - tests.upstartJobs.xserver). - ''; - }; - 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); + }; } diff --git a/modules/tasks/filesystems.nix b/modules/tasks/filesystems.nix index 9495cac67855..8eaf77330a3d 100644 --- a/modules/tasks/filesystems.nix +++ b/modules/tasks/filesystems.nix @@ -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 ''; }; + */ }; diff --git a/modules/tasks/kbd.nix b/modules/tasks/kbd.nix index 62c4b92ed094..b563d33dc5b1 100644 --- a/modules/tasks/kbd.nix +++ b/modules/tasks/kbd.nix @@ -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}' ''; }; + */ }; diff --git a/modules/tasks/network-interfaces.nix b/modules/tasks/network-interfaces.nix index c12f9b28ca91..611de13148a3 100644 --- a/modules/tasks/network-interfaces.nix +++ b/modules/tasks/network-interfaces.nix @@ -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 ''} ''; };