Home Explore Blog Models CI



nixpkgs

nixos/modules/services/system/systemd-lock-handler.md
4c86fe878b8a5d0db594499f10a6c102264b48f23f0e1e900000000300000598
# systemd-lock-handler {#module-services-systemd-lock-handler}

The `systemd-lock-handler` module provides a service that bridges
D-Bus events from `logind` to user-level systemd targets:

  - `lock.target` started by `loginctl lock-session`,
  - `unlock.target` started by `loginctl unlock-session` and
  - `sleep.target` started by `systemctl suspend`.

You can create a user service that starts with any of these targets.

For example, to create a service for `swaylock`:

```nix
{
  services.systemd-lock-handler.enable = true;

  systemd.user.services.swaylock = {
    description = "Screen locker for Wayland";
    documentation = [ "man:swaylock(1)" ];

    # If swaylock exits cleanly, unlock the session:
    onSuccess = [ "unlock.target" ];

    # When lock.target is stopped, stops this too:
    partOf = [ "lock.target" ];

    # Delay lock.target until this service is ready:
    before = [ "lock.target" ];
    wantedBy = [ "lock.target" ];

    serviceConfig = {
      # systemd will consider this service started when swaylock forks...
      Type = "forking";

      # ... and swaylock will fork only after it has locked the screen.
      ExecStart = "${lib.getExe pkgs.swaylock} -f";

      # If swaylock crashes, always restart it immediately:
      Restart = "on-failure";
      RestartSec = 0;
    };
  };
}
```

See [upstream documentation](https://sr.ht/~whynothugo/systemd-lock-handler) for more information.

Chunks
3ced6211 (1st chunk of `nixos/modules/services/system/systemd-lock-handler.md`)
Title: systemd-lock-handler Module
Summary
The `systemd-lock-handler` module provides a service that bridges D-Bus events from `logind` to user-level systemd targets, specifically `lock.target`, `unlock.target`, and `sleep.target`. This allows users to create custom services that automatically start or stop in response to session locking, unlocking, or system suspension. An example configuration for integrating `swaylock` as a screen locker is provided, demonstrating how to set up its dependencies and behavior relative to `lock.target`.