diff --git a/modules/services/databases/postgresql.nix b/modules/services/databases/postgresql.nix index 18e72381cdc..a52d241e96c 100644 --- a/modules/services/databases/postgresql.nix +++ b/modules/services/databases/postgresql.nix @@ -194,15 +194,14 @@ in ''; serviceConfig = - '' - # Shut down Postgres using SIGINT ("Fast Shutdown mode"). See + { # Shut down Postgres using SIGINT ("Fast Shutdown mode"). See # http://www.postgresql.org/docs/current/static/server-shutdown.html - KillSignal=SIGINT + KillSignal = "SIGINT"; # Give Postgres a decent amount of time to clean up after # receiving systemd's SIGINT. - TimeoutSec=60 - ''; + TimeoutSec = 60; + }; }; }; diff --git a/modules/services/logging/syslogd.nix b/modules/services/logging/syslogd.nix index 2b7e4a8e44c..8c815ddf25f 100644 --- a/modules/services/logging/syslogd.nix +++ b/modules/services/logging/syslogd.nix @@ -106,8 +106,8 @@ in # FIXME: restarting syslog seems to break journal logging. boot.systemd.services.syslog = - { description = "Syslog daemon"; - + { description = "Syslog Daemon"; + requires = [ "syslog.socket" ]; wantedBy = [ "multi-user.target" "syslog.target" ]; @@ -115,11 +115,10 @@ in environment.TZ = config.time.timeZone; serviceConfig = - '' - ExecStart=${pkgs.sysklogd}/sbin/syslogd ${toString cfg.extraParams} -f ${syslogConf} -n + { ExecStart = "${pkgs.sysklogd}/sbin/syslogd ${toString cfg.extraParams} -f ${syslogConf} -n"; # Prevent syslogd output looping back through journald. - StandardOutput=null - ''; + StandardOutput = "null"; + }; }; }; diff --git a/modules/services/misc/nix-daemon.nix b/modules/services/misc/nix-daemon.nix index ba970b95bc0..718566ee5b9 100644 --- a/modules/services/misc/nix-daemon.nix +++ b/modules/services/misc/nix-daemon.nix @@ -258,7 +258,7 @@ in ListenStream=/nix/var/nix/daemon-socket/socket ''; }; - + boot.systemd.services."nix-daemon" = { description = "Nix Daemon"; @@ -268,16 +268,14 @@ in environment = cfg.envVars; serviceConfig = - '' - ExecStart=${nix}/bin/nix-worker --daemon - KillMode=process - PIDFile=/run/sshd.pid - Nice=${toString cfg.daemonNiceLevel} - IOSchedulingPriority=${toString cfg.daemonIONiceLevel} - LimitNOFILE=4096 - ''; + { ExecStart = "${nix}/bin/nix-worker --daemon"; + KillMode = "process"; + Nice = cfg.daemonNiceLevel; + IOSchedulingPriority = cfg.daemonIONiceLevel; + LimitNOFILE = 4096; + }; }; - + nix.envVars = { NIX_CONF_DIR = "/etc/nix"; diff --git a/modules/services/networking/dhcpcd.nix b/modules/services/networking/dhcpcd.nix index 6eea947f604..cfbc7ba3679 100644 --- a/modules/services/networking/dhcpcd.nix +++ b/modules/services/networking/dhcpcd.nix @@ -91,7 +91,7 @@ in config = mkIf config.networking.useDHCP { - jobs.dhcpcd = + boot.systemd.services.dhcpcd = { description = "DHCP Client"; wantedBy = [ "multi-user.target" ]; @@ -99,14 +99,12 @@ in path = [ dhcpcd pkgs.nettools pkgs.openresolv ]; - daemonType = "fork"; - - exec = "dhcpcd --config ${dhcpcdConf}"; - serviceConfig = - '' - ExecReload=${dhcpcd}/sbin/dhcpcd --rebind - ''; + { Type = "forking"; + PIDFile = "/run/dhcpcd.pid"; + ExecStart = "@${dhcpcd}/sbin/dhcpcd dhcpcd --config ${dhcpcdConf}"; + ExecReload = "${dhcpcd}/sbin/dhcpcd --rebind"; + }; }; environment.systemPackages = [ dhcpcd ]; diff --git a/modules/services/networking/ssh/sshd.nix b/modules/services/networking/ssh/sshd.nix index 373b482f85c..83b7b5372ec 100644 --- a/modules/services/networking/ssh/sshd.nix +++ b/modules/services/networking/ssh/sshd.nix @@ -321,11 +321,8 @@ in script = mkAuthkeyScript; - serviceConfig = - '' - Type=oneshot - RemainAfterExit=true - ''; + serviceConfig.Type = "oneshot"; + serviceConfig.RemainAfterExit = true; }; boot.systemd.services.sshd = @@ -349,15 +346,14 @@ in ''; serviceConfig = - '' - ExecStart=\ - ${pkgs.openssh}/sbin/sshd -D -h ${cfg.hostKeyPath} \ - -f ${pkgs.writeText "sshd_config" cfg.extraConfig} - Restart=always - Type=simple - KillMode=process - PIDFile=/run/sshd.pid - ''; + { ExecStart = + "${pkgs.openssh}/sbin/sshd -D -h ${cfg.hostKeyPath} " + + "-f ${pkgs.writeText "sshd_config" cfg.extraConfig}"; + Restart = "always"; + Type = "simple"; + KillMode = "process"; + PIDFile = "/run/sshd.pid"; + }; }; networking.firewall.allowedTCPPorts = cfg.ports; diff --git a/modules/services/system/nscd.nix b/modules/services/system/nscd.nix index 54e661896d9..eecb845d547 100644 --- a/modules/services/system/nscd.nix +++ b/modules/services/system/nscd.nix @@ -53,15 +53,16 @@ in ''; serviceConfig = - '' - ExecStart=@${pkgs.glibc}/sbin/nscd nscd -f ${./nscd.conf} - Type=forking - PIDFile=/run/nscd/nscd.pid - Restart=always - ExecReload=${pkgs.glibc}/sbin/nscd --invalidate passwd - ExecReload=${pkgs.glibc}/sbin/nscd --invalidate group - ExecReload=${pkgs.glibc}/sbin/nscd --invalidate hosts - ''; + { ExecStart = "@${pkgs.glibc}/sbin/nscd nscd -f ${./nscd.conf}"; + Type = "forking"; + PIDFile = "/run/nscd/nscd.pid"; + Restart = "always"; + ExecReload = + [ "${pkgs.glibc}/sbin/nscd --invalidate passwd" + "${pkgs.glibc}/sbin/nscd --invalidate group" + "${pkgs.glibc}/sbin/nscd --invalidate hosts" + ]; + }; }; }; diff --git a/modules/system/boot/systemd-unit-options.nix b/modules/system/boot/systemd-unit-options.nix index f56ee4fc0d3..c5fa23b36bb 100644 --- a/modules/system/boot/systemd-unit-options.nix +++ b/modules/system/boot/systemd-unit-options.nix @@ -81,21 +81,28 @@ with pkgs.lib; }; unitConfig = mkOption { - default = ""; - type = types.string; + default = {}; + example = { RequiresMountsFor = "/data"; }; + type = types.attrs; description = '' - Contents of the [Unit] section of the unit. - See systemd.unit + Each attribute in this set specifies an option in the + [Unit] section of the unit. See + systemd.unit 5 for details. ''; }; serviceConfig = mkOption { - default = ""; - type = types.string; + default = {}; + example = + { StartLimitInterval = 10; + RestartSec = 5; + }; + type = types.attrs; description = '' - Contents of the [Service] section of the unit. - See systemd.service + Each attribute in this set specifies an option in the + [Service] section of the unit. See + systemd.service 5 for details. ''; }; diff --git a/modules/system/boot/systemd.nix b/modules/system/boot/systemd.nix index 4cb0e1cdf1d..a7a7729bb8b 100644 --- a/modules/system/boot/systemd.nix +++ b/modules/system/boot/systemd.nix @@ -177,24 +177,38 @@ let pkgs.gnused systemd ]; + unitConfig = + { Requires = concatStringsSep " " config.requires; + Wants = concatStringsSep " " config.wants; + After = concatStringsSep " " config.after; + Before = concatStringsSep " " config.before; + PartOf = concatStringsSep " " config.partOf; + } // optionalAttrs (config.description != "") + { Description = config.description; + }; }; }; + toOption = x: + if x == true then "true" + else if x == false then "false" + else toString x; + + attrsToSection = as: + concatStrings (concatLists (mapAttrsToList (name: value: + map (x: '' + ${name}=${toOption x} + '') + (if isList value then value else [value])) + as)); + serviceToUnit = name: def: { inherit (def) wantedBy; text = '' [Unit] - ${optionalString (def.description != "") '' - Description=${def.description} - ''} - Requires=${concatStringsSep " " def.requires} - Wants=${concatStringsSep " " def.wants} - After=${concatStringsSep " " def.after} - Before=${concatStringsSep " " def.before} - PartOf=${concatStringsSep " " def.partOf} - ${def.unitConfig} + ${attrsToSection def.unitConfig} [Service] Environment=PATH=${def.path} @@ -215,7 +229,7 @@ let ''} ''} - ${def.serviceConfig} + ${attrsToSection def.serviceConfig} ''; }; diff --git a/modules/system/upstart/upstart.nix b/modules/system/upstart/upstart.nix index fbbbf72e027..a32bca8ddd7 100644 --- a/modules/system/upstart/upstart.nix +++ b/modules/system/upstart/upstart.nix @@ -54,7 +54,7 @@ let ''; in { - inherit (job) description requires wants before partOf environment path restartIfChanged; + inherit (job) description requires wants before partOf environment path restartIfChanged unitConfig; after = (if job.startOn == "stopped udevtrigger" then [ "systemd-udev-settle.service" ] else @@ -72,40 +72,23 @@ let [ "multi-user.target" ]) ++ job.wantedBy; serviceConfig = - '' - ${job.serviceConfig} - - ${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"} - ''; + job.serviceConfig + // optionalAttrs (job.preStart != "" && (job.script != "" || job.exec != "")) + { ExecStartPre = preStartScript; } + // optionalAttrs (job.script != "" || job.exec != "") + { ExecStart = startScript; } + // optionalAttrs (job.postStart != "") + { ExecStartPost = postStartScript; } + // optionalAttrs (job.preStop != "") + { ExecStop = preStopScript; } + // optionalAttrs (job.postStop != "") + { ExecStopPost = postStopScript; } + // (if job.script == "" && job.exec == "" then { Type = "oneshot"; RemainAfterExit = true; } else + if job.daemonType == "fork" then { Type = "forking"; GuessMainPID = true; } else + if job.daemonType == "none" then { } else + throw "invalid daemon type `${job.daemonType}'") + // optionalAttrs (!job.task && job.respawn) + { Restart = "always"; }; };