Prevent laptop from suspending when lid is closed if on AC

Before I go and implement this myself, does anyone happen to have a nixos module in their config that configures their laptop such that it does not suspend when the lid is closed if it is on AC wall power?

This looks like a pretty clear description of how to do it, which could be adapted for NixOS: Prevent suspend on lid close with systemd – nrocco.github.io, but if anyone’s already done the work, I’d love to steal your work.

1 Like

I think this should work: NixOS 23.11 manual | Nix & NixOS

services.logind.lidSwitchExternalPower = "ignore";
5 Likes

Brilliant!! Thank you for pointing me to that.

This works, but only as long as I’m logged in. Being in the process of repurposing an old Laptop to serve as home cloud, this is not sufficient. Any other options?

I needed to do this below. I don’t think services.logind.lidSwitchExternalPower was doing anything for me. After below (and a restart seemed necessary), I can ssh in after hours of idle time. Plus, I can do sudo reboot from an ssh session, it will end my session, the laptop will reboot, and then I can ssh back in a minute or two later.:

  # Enable automatic login for the user.
  services.displayManager.autoLogin.enable = true;
  services.displayManager.autoLogin.user = "eric";

  # Workaround for GNOME autologin: https://github.com/NixOS/nixpkgs/issues/103746#issuecomment-945091229
  systemd.services."getty@tty1".enable = false;
  systemd.services."autovt@tty1".enable = false;

  systemd.sleep.extraConfig = ''
    AllowSuspend=no
    AllowHibernation=no
    AllowHybridSleep=no
    AllowSuspendThenHibernate=no
  '';
1 Like

Omg this solved it TYSM