···
+
host = config.networking.hostName or "unknown"
+
+ optionalString (config.networking.domain != null) ".${config.networking.domain}";
cfg = config.services.smartd;
+
nm = cfg.notifications.mail;
+
nw = cfg.notifications.wall;
+
nx = cfg.notifications.x11;
+
smartdNotify = pkgs.writeScript "smartd-notify.sh" ''
+
#! ${pkgs.stdenv.shell}
+
${optionalString nm.enable ''
+
From: smartd on ${host} <root>
+
To: undisclosed-recipients:;
+
Subject: SMART error on $SMARTD_DEVICESTRING: $SMARTD_FAILTYPE
+
${pkgs.smartmontools}/sbin/smartctl -a -d "$SMARTD_DEVICETYPE" "$SMARTD_DEVICE"
+
} | ${nm.mailer} -i "${nm.recipient}"
+
${optionalString nw.enable ''
+
Problem detected with disk: $SMARTD_DEVICESTRING
+
Warning message from smartd is:
+
} | ${pkgs.utillinux}/bin/wall 2>/dev/null
+
${optionalString nx.enable ''
+
export DISPLAY=${nx.display}
+
Problem detected with disk: $SMARTD_DEVICESTRING
+
Warning message from smartd is:
+
} | ${pkgs.xorg.xmessage}/bin/xmessage -file - 2>/dev/null &
+
notifyOpts = optionalString (nm.enable || nw.enable || nx.enable)
+
("-m <nomailer> -M exec ${smartdNotify} " + optionalString cfg.notifications.test "-M test ");
+
smartdConf = pkgs.writeText "smartd.conf" ''
+
# Autogenerated smartd startup config file
+
DEFAULT ${notifyOpts}${cfg.defaults.monitored}
+
${concatMapStringsSep "\n" (d: "${d.device} ${d.options}") cfg.devices}
+
${optionalString cfg.autodetect
+
"DEVICESCAN ${notifyOpts}${cfg.defaults.autodetected}"}
smartdOpts = { name, ... }: {
···
type = types.separatedString " ";
description = "Options that determine how smartd monitors the device.";
···
+
enable = mkEnableOption "smartd daemon from <literal>smartmontools</literal> package";
+
autodetect = mkOption {
+
Whenever smartd should monitor all devices connected to the
+
machine at the time it's being started (the default).
+
Set to false to monitor the devices listed in
+
<option>services.smartd.devices</option> only.
+
default = config.services.mail.sendmailSetuidWrapper != null;
+
description = "Whenever to send e-mail notifications.";
+
description = "Recipient of the notification messages.";
+
default = "/var/setuid-wrappers/sendmail";
+
Sendmail-compatible binary to be used to send the messages.
+
You should probably enable
+
<option>services.postfix</option> or some other MTA for
+
description = "Whenever to send wall notifications to all users.";
+
default = config.services.xserver.enable;
+
description = "Whenever to send X11 xmessage notifications.";
+
default = ":${toString config.services.xserver.display}";
+
description = "DISPLAY to send X11 notifications to.";
+
description = "Whenever to send a test notification on startup.";
+
type = types.separatedString " ";
+
example = "-a -o on -s (S/../.././02|L/../../7/04)";
+
Common default options for explicitly monitored (listed in
+
<option>services.smartd.devices</option>) devices.
+
The default value turns on monitoring of all the things (see
+
<literal>man 5 smartd.conf</literal>).
+
The example also turns on SMART Automatic Offline Testing on
+
startup, and schedules short self-tests daily, and long
+
autodetected = mkOption {
+
default = cfg.defaults.monitored;
+
type = types.separatedString " ";
+
Like <option>services.smartd.defaults.monitored</option>, but for the
···
example = [ { device = "/dev/sda"; } { device = "/dev/sdb"; options = "-d sat"; } ];
type = types.listOf types.optionSet;
options = [ smartdOpts ];
+
description = "List of devices to monitor.";
···
config = mkIf cfg.enable {
+
assertion = cfg.autodetect || cfg.devices != [];
+
message = "smartd can't run with both disabled autodetect and an empty list of devices to monitor.";
systemd.services.smartd = {
description = "S.M.A.R.T. Daemon";
wantedBy = [ "multi-user.target" ];
+
path = [ pkgs.nettools ]; # for hostname and dnsdomanname calls in smartd
+
serviceConfig.ExecStart = "${pkgs.smartmontools}/sbin/smartd --no-fork --configfile=${smartdConf}";