summaryrefslogtreecommitdiff
path: root/modules/power/default.nix
blob: e36a027d2c35978dddf86c5d20b3449e75dfe7f2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
{ config, lib, ... }:

let
  cfg = config.power;

  types = lib.types;

  onOff = cond: if cond then "on" else "off";
in

{
  options = {
    power.restartAfterPowerFailure = lib.mkOption {
      type = types.nullOr types.bool;
      default = null;
      description = ''
        Whether to restart the computer after a power failure.

        Option is not supported on all devices.
      '';
    };

    power.restartAfterFreeze = lib.mkOption {
      type = types.nullOr types.bool;
      default = null;
      description = ''
        Whether to restart the computer after a system freeze.
      '';
    };
  };

  config = {

    system.activationScripts.power.text = ''
      echo "configuring power..." >&2

      ${lib.optionalString (cfg.restartAfterPowerFailure != null) ''
        if ! systemsetup -getRestartPowerFailure | grep -q "Not supported"; then
          systemsetup -setRestartPowerFailure \
            '${onOff cfg.restartAfterPowerFailure}' &> /dev/null
        fi
      ''}

      ${lib.optionalString (cfg.restartAfterFreeze != null) ''
        systemsetup -setRestartFreeze \
          '${onOff cfg.restartAfterFreeze}' &> /dev/null
      ''}
    '';

  };
}