Commit 21529d18 authored by Bjørn Forsman's avatar Bjørn Forsman
Browse files

nixos/ups: shutdown UPS at host shutdown

Implement the missing bit of the NUT shutdown design[1]. This ensures
that machines come back up automatically after a power outage. (Without
this change they will only come back up if the UPS completely empties
its battery.)

[1] https://networkupstools.org/docs/user-manual.chunked/Configuration_notes.html#Shutdown_design
parent 3b781a1e
Loading
Loading
Loading
Loading
+20 −0
Original line number Diff line number Diff line
@@ -311,6 +311,7 @@ let
            MINSUPPLIES = 1;
            MONITOR = <generated from config.power.ups.upsmon.monitor>
            NOTIFYCMD = "''${pkgs.nut}/bin/upssched";
            POWERDOWNFLAG = "/run/killpower";
            RUN_AS_USER = "root";
            SHUTDOWNCMD = "''${pkgs.systemd}/bin/shutdown now";
          }
@@ -334,6 +335,7 @@ let
        MINSUPPLIES = lib.mkDefault 1;
        MONITOR = lib.flip lib.mapAttrsToList cfg.upsmon.monitor (name: monitor: with monitor; [ system powerValue user "\"@upsmon_password_${name}@\"" type ]);
        NOTIFYCMD = lib.mkDefault "${pkgs.nut}/bin/upssched";
        POWERDOWNFLAG = lib.mkDefault "/run/killpower";
        RUN_AS_USER = "root"; # TODO: replace 'root' by another username.
        SHUTDOWNCMD = lib.mkDefault "${pkgs.systemd}/bin/shutdown now";
      };
@@ -575,6 +577,24 @@ in
      ];
    };

    systemd.services.ups-killpower = lib.mkIf (cfg.upsmon.settings.POWERDOWNFLAG != null) {
      enable = cfg.upsd.enable;
      description = "UPS Kill Power";
      wantedBy = [ "shutdown.target" ];
      after = [ "shutdown.target" ];
      before = [ "final.target" ];
      unitConfig = {
        ConditionPathExists = cfg.upsmon.settings.POWERDOWNFLAG;
        DefaultDependencies = "no";
      };
      environment = envVars;
      serviceConfig = {
        Type = "oneshot";
        ExecStart = "${pkgs.nut}/bin/upsdrvctl shutdown";
        Slice = "system-ups.slice";
      };
    };

    environment.etc = {
      "nut/nut.conf".source = pkgs.writeText "nut.conf"
        ''