1 { config, lib, pkgs, ... }:
6 cfg = config.services.apcupsd;
8 configFile = pkgs.writeText "apcupsd.conf" ''
9 ## apcupsd.conf v1.1 ##
10 # apcupsd complains if the first line is not like above.
12 SCRIPTDIR ${toString scriptDir}
15 # List of events from "man apccontrol"
40 shellCmdsForEventScript = eventname: commands: ''
41 echo "#!${pkgs.runtimeShell}" > "$out/${eventname}"
42 echo '${commands}' >> "$out/${eventname}"
43 chmod a+x "$out/${eventname}"
46 eventToShellCmds = event: if builtins.hasAttr event cfg.hooks then (shellCmdsForEventScript event (builtins.getAttr event cfg.hooks)) else "";
48 scriptDir = pkgs.runCommand "apcupsd-scriptdir" { preferLocalBuild = true; } (''
50 # Copy SCRIPTDIR from apcupsd package
51 cp -r ${pkgs.apcupsd}/etc/apcupsd/* "$out"/
52 # Make the files writeable (nix will unset the write bits afterwards)
54 # Remove the sample event notification scripts, because they don't work
55 # anyways (they try to send mail to "root" with the "mail" command)
56 (cd "$out" && rm changeme commok commfailure onbattery offbattery)
57 # Remove the sample apcupsd.conf file (we're generating our own)
58 rm "$out/apcupsd.conf"
59 # Set the SCRIPTDIR= line in apccontrol to the dir we're creating now
60 sed -i -e "s|^SCRIPTDIR=.*|SCRIPTDIR=$out|" "$out/apccontrol"
61 '' + concatStringsSep "\n" (map eventToShellCmds eventList)
78 description = lib.mdDoc ''
79 Whether to enable the APC UPS daemon. apcupsd monitors your UPS and
80 permits orderly shutdown of your computer in the event of a power
81 failure. User manual: http://www.apcupsd.com/manual/manual.html.
82 Note that apcupsd runs as root (to allow shutdown of computer).
83 You can check the status of your UPS with the "apcaccess" command.
87 configText = mkOption {
95 description = lib.mdDoc ''
96 Contents of the runtime configuration file, apcupsd.conf. The default
97 settings makes apcupsd autodetect USB UPSes, limit network access to
98 localhost and shutdown the system when the battery level is below 50
99 percent, or when the UPS has calculated that it has 5 minutes or less
100 of remaining power-on time. See man apcupsd.conf for details.
107 doshutdown = "# shell commands to notify that the computer is shutting down";
109 type = types.attrsOf types.lines;
110 description = lib.mdDoc ''
111 Each attribute in this option names an apcupsd event and the string
112 value it contains will be executed in a shell, in response to that
113 event (prior to the default action). See "man apccontrol" for the
114 list of events and what they represent.
116 A hook script can stop apccontrol from doing its default action by
117 exiting with value 99. Do not do this unless you know what you're
127 ###### implementation
129 config = mkIf cfg.enable {
132 assertion = let hooknames = builtins.attrNames cfg.hooks; in all (x: elem x eventList) hooknames;
134 One (or more) attribute names in services.apcupsd.hooks are invalid.
135 Current attribute names: ${toString (builtins.attrNames cfg.hooks)}
136 Valid attribute names : ${toString eventList}
140 # Give users access to the "apcaccess" tool
141 environment.systemPackages = [ pkgs.apcupsd ];
143 # NOTE 1: apcupsd runs as root because it needs permission to run
146 # NOTE 2: When apcupsd calls "wall", it prints an error because stdout is
147 # not connected to a tty (it is connected to the journal):
148 # wall: cannot get tty name: Inappropriate ioctl for device
149 # The message still gets through.
150 systemd.services.apcupsd = {
151 description = "APC UPS Daemon";
152 wantedBy = [ "multi-user.target" ];
153 preStart = "mkdir -p /run/apcupsd/";
155 ExecStart = "${pkgs.apcupsd}/bin/apcupsd -b -f ${configFile} -d1";
156 # TODO: When apcupsd has initiated a shutdown, systemd always ends up
157 # waiting for it to stop ("A stop job is running for UPS daemon"). This
158 # is weird, because in the journal one can clearly see that apcupsd has
159 # received the SIGTERM signal and has already quit (or so it seems).
160 # This reduces the wait time from 90 seconds (default) to just 5. Then
161 # systemd kills it with SIGKILL.
164 unitConfig.Documentation = "man:apcupsd(8)";
167 # A special service to tell the UPS to power down/hibernate just before the
168 # computer shuts down. (The UPS has a built in delay before it actually
169 # shuts off power.) Copied from here:
170 # http://forums.opensuse.org/english/get-technical-help-here/applications/479499-apcupsd-systemd-killpower-issues.html
171 systemd.services.apcupsd-killpower = {
172 description = "APC UPS Kill Power";
173 after = [ "shutdown.target" ]; # append umount.target?
174 before = [ "final.target" ];
175 wantedBy = [ "shutdown.target" ];
177 ConditionPathExists = "/run/apcupsd/powerfail";
178 DefaultDependencies = "no";
182 ExecStart = "${pkgs.apcupsd}/bin/apcupsd --killpower -f ${configFile}";
183 TimeoutSec = "infinity";
184 StandardOutput = "tty";
185 RemainAfterExit = "yes";