Power off NixOS after being idle for some timeout

I have a server running on AWS that I’d like to automatically shut down after it has been idle for some time. For me idle = no active SSH sessions. Following power management - Automatic shutdown after no activity - Unix & Linux Stack Exchange, I tried adding

  services.logind.extraConfig = ''

to my /etc/nixos/configuration.nix, rebooted, and logged out all SSH sessions. But this does not seem to have any effect.

Is there any way to get NixOS to power off after some configured idle timeout?

Scratched my own itch here and created https://github.com/samuela/nixos-idle-shutdown.

How about something like this?

  systemd.services.shutdown = {
    description = "automatic shutdown for inactivity";
    path    = with pkgs; [ procps gawk ];
    script  = ''
      # shutdown when no one is logged in
      logged=$(who | wc -l)
      if test "$logged" -lt 1 && ! pgrep -f '(sftp|tmux|something-interactive)'
      then shutdown +5 'Shutting down for inactivity.'
  systemd.timers.shutdown = {
    wantedBy = [ "timers.target" ];
    timerConfig.OnUnitActiveSec = "1h";
    timerConfig.OnBootSec = "1h";


Yeah, that also seems like a decent solution!