How to make xscreensaver lock screen when laptop lid closed?

xscreensaver says

If your system has systemd (1) 221 or newer, then closing the lid of your laptop will cause the screen to lock immediately.

I have in home-manager

xscreensaver = {
  enable = true;
  settings = {lock = true;};
}

It installs xscreensaver and locks automatically after a few minutes, but it doesn’t lock automatically when I close the laptop lid. How can I make it do that?

Using nixos unstable, lightdm, picom, qtile.

1 Like

I made a custom service on the main config for this. Maybe there is a better way, but works for now:

    lock-before-sleeping = {
      restartIfChanged = false;
      unitConfig = {
        Description = "Helper service to bind locker to sleep.target";
      };
      serviceConfig = {
        ExecStart = "${pkgs.slock}/bin/slock";
        Type = "simple";
      };
      before = [
        "pre-sleep.service"
      ];
      wantedBy= [
        "pre-sleep.service"
      ];
      environment = {
        DISPLAY = ":0";
        XAUTHORITY = "/home/gurkan/.Xauthority";
      };
    };
1 Like