# Desktop notifications from Systemd Service

**URL:** https://discourse.nixos.org/t/desktop-notifications-from-systemd-service/17672
**Category:** Help
**Created:** [February 13, 2022, 11:05am UTC](https://discourse.nixos.org/t/desktop-notifications-from-systemd-service/17672 "2022-02-13T11:05:17Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![payas](https://discourse.nixos.org/letter_avatar_proxy/v4/letter/p/3da27b/32.png) [@payas](https://discourse.nixos.org/u/payas)
#### Post date: [February 13, 2022, 11:05am UTC](https://discourse.nixos.org/t/desktop-notifications-from-systemd-service/17672/1 "2022-02-13T11:05:17Z")

</div>

I am trying to setup a proper monitoring for my backups (done via Restic+rclone, setup via Nix, working as expected).

I would like to receive a desktop notification (I’m using plasma desktop) every time backup succeeds or fails (setup at hourly interval). To test things out, I created config as below:

```auto
  systemd.services.backup-success-notify-org = {
      description = "Desktop notification about successful backup";
      serviceConfig = {
        ExecStart = "${pkgs.libnotify}/bin/notify-send \'Successfully backed up: Org\'"; # for some strange reason, I don't see quotes in systemd logs, but it doesn't complain regardless of quote escaping
      };
      path = [pkgs.bash]; # from NixOS manual, its presence or absence doesn't matter
    };
    restic-backups-org = { # this is name of actual restic backup service configured elsewhere
      onFailure = ["backup-failure-alert-org.service"];
      unitConfig.OnSuccess = ["backup-success-notify-org.service"]; # this shouldn't be necessary per systemd docs, but it doesn't work regardless of presence/absence
    };
  };

  systemd.timers.backup-success-notify-org = {
    timerConfig = {
      OnCalendar = "minutely"; # for testing and debugging. Ultimately this should only be invoked by OnSuccess/OnFailure events of backup service
    };
  };

```

I see the service and timer files are properly generated and enabled, and I see from logs that service is successfully invoked by timer every minute:

```auto
❱ systemctl status backup-success-notify-org.service
○ backup-success-notify-org.service - Desktop notification about successful backup
     Loaded: loaded (/etc/systemd/system/backup-success-notify-org.service; linked; vendor preset: enabled)
     Active: inactive (dead) since Sun 2022-02-13 17:36:02 IST; 11s ago
TriggeredBy: ● backup-success-notify-org.timer
    Process: 70743 ExecStart=/nix/store/avi8rkdrxw41b9f0gzd6i4dsaz176m9y-libnotify-0.7.9/bin/notify-send Successfully backed up: Org (code=exited, status=0/SUCCESS)
   Main PID: 70743 (code=exited, status=0/SUCCESS)
         IP: 0B in, 0B out
        CPU: 4ms

Feb 13 17:36:02 enterprise systemd[1]: Started Desktop notification about successful backup.
Feb 13 17:36:02 enterprise systemd[1]: backup-success-notify-org.service: Deactivated successfully.

```

But, there are no desktop notification. I can run `notify-send "Some text"` from terminal to desired effect, but it doesn’t seem to work when systemd service does the same. What am I missing and how can I get it to work?

---

<div class="post-metadata">

### Author: ![cmm](https://discourse.nixos.org/user_avatar/discourse.nixos.org/cmm/32/3347_2.png) [@cmm](https://discourse.nixos.org/u/cmm)
#### Post date: [February 13, 2022, 11:32am UTC](https://discourse.nixos.org/t/desktop-notifications-from-systemd-service/17672/2 "2022-02-13T11:32:57Z")

</div>

I don’t know anything about libnotify specifically, but this seems relevant: [[SOLVED] Desktop notifications from a systemd service / Applications & Desktop Environments / Arch Linux Forums](https://bbs.archlinux.org/viewtopic.php?id=205867)

---

<div class="post-metadata">

### Author: ![payas](https://discourse.nixos.org/letter_avatar_proxy/v4/letter/p/3da27b/32.png) [@payas](https://discourse.nixos.org/u/payas)
#### Post date: [February 13, 2022, 11:37am UTC](https://discourse.nixos.org/t/desktop-notifications-from-systemd-service/17672/3 "2022-02-13T11:37:27Z")

</div>

Thanks.

I tried with and without `User=payas` and it runs both ways, but result is same.

As for other solution mentioned in the link, I am not running X11, but Plasma Wayland session, and have no idea how that one can fit here.

---

<div class="post-metadata">

### Author: ![cmm](https://discourse.nixos.org/user_avatar/discourse.nixos.org/cmm/32/3347_2.png) [@cmm](https://discourse.nixos.org/u/cmm)
#### Post date: [February 13, 2022, 11:43am UTC](https://discourse.nixos.org/t/desktop-notifications-from-systemd-service/17672/4 "2022-02-13T11:43:22Z")

</div>

I would try looking at any `XDG_*` variables set in your user session.

---

<div class="post-metadata">

### Author: ![payas](https://discourse.nixos.org/letter_avatar_proxy/v4/letter/p/3da27b/32.png) [@payas](https://discourse.nixos.org/u/payas)
#### Post date: [February 13, 2022, 11:53am UTC](https://discourse.nixos.org/t/desktop-notifications-from-systemd-service/17672/5 "2022-02-13T11:53:12Z")

</div>

Pardon me if its too simple, but I have these env variables set for XDG, but not sure what I can use them for. I don’t know of Xauthority parallel in Wayland world:

```auto
❱ env | rg DISPLAY
DISPLAY=:1
WAYLAND_DISPLAY=wayland-0

❱ env | rg "XDG_[^=]+" -o
XDG_DATA_DIRS
XDG_SESSION_PATH
XDG_SEAT
XDG_ACTIVATION_TOKEN
XDG_SESSION_DESKTOP
XDG_SEAT_PATH
XDG_RUNTIME_DIR
XDG_CONFIG_DIRS
XDG_SESSION_ID
XDG_SESSION_CLASS
XDG_CURRENT_DESKTOP
XDG_SESSION_TYPE
XDG_VTNR
XDG_DESKTOP_PORTAL_DIR

```

Edit: Judging by [this](https://wiki.archlinux.org/title/Running_GUI_applications_as_root), it doesn’t appear to be possible with Wayland. What I’m wondering about is why would specifying User not work, as then none of this will be necessary…

---

<div class="post-metadata">

### Author: ![eliasp](https://discourse.nixos.org/user_avatar/discourse.nixos.org/eliasp/32/1235_2.png) [@eliasp](https://discourse.nixos.org/u/eliasp)
#### Post date: [February 13, 2022, 2:07pm UTC](https://discourse.nixos.org/t/desktop-notifications-from-systemd-service/17672/6 "2022-02-13T14:07:31Z")

</div>

I’d say the main issue is the system service/`notify-send` not having access to the user session’s D-Bus instance.

You might try giving it access by using:

```auto
[Service]
User=payas
Environment=DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/%U/bus

```

---

<div class="post-metadata">

### Author: ![payas](https://discourse.nixos.org/letter_avatar_proxy/v4/letter/p/3da27b/32.png) [@payas](https://discourse.nixos.org/u/payas)
#### Post date: [February 13, 2022, 4:46pm UTC](https://discourse.nixos.org/t/desktop-notifications-from-systemd-service/17672/7 "2022-02-13T16:46:34Z")

</div>

That worked! Thanks a lot!

---

<div class="post-metadata">

### Author: ![Father2303](https://discourse.nixos.org/user_avatar/discourse.nixos.org/father2303/32/8705_2.png) [@Father2303](https://discourse.nixos.org/u/Father2303)
#### Post date: [September 5, 2023, 10:59am UTC](https://discourse.nixos.org/t/desktop-notifications-from-systemd-service/17672/8 "2023-09-05T10:59:40Z")

</div>

Hi, i’d like to do the same with `awesome` under ` X11` but it fails and i don’t understand why. This is what i’ve got (with a timer triggering on boot for testing):

```auto
  systemd = {
    timers."notify" = {
      wantedBy = ["timers.target"];
      timerConfig = {
        OnStartupSec = "1s";
        AccuracySec = "1us";
        Unit = "notify.service";
      };
    };

    services."notify" = {
      script = ''
        ${pkgs.libnotify}/bin/notify-send --urgency=critical "Systemd Service has failed"
      '';
      environment = {
        USER = "sperber"; # makes no difference
        # Display = ":0"; # makes no difference
        DBUS_SESSION_BUS_ADDRESS = "unix:path=/run/user/%U/bus";        
        # DBUS_SESSION_BUS_ADDRESS = "unix:path=/run/user/1000/bus"; # makes no difference
        # XAUTHORITY = "/home/sperber/.Xauthority"; # makes no difference
      };
      serviceConfig = {
        Type = "oneshot";
        User = "sperber";
      };
    };
  };

```

However, no notification is displayed and it fails like this:

```auto
~ ❯ systemctl status notify ✘ 3
× notify.service
     Loaded: loaded (/etc/systemd/system/notify.service; linked; preset: enabled)
     Active: failed (Result: exit-code) since Tue 2023-09-05 12:38:14 CEST; 4min 0s ago
TriggeredBy: ● notify.timer
    Process: 1280 ExecStart=/nix/store/4ibrrh7p6ggvdsd6ih9wk16r71g71bfj-unit-script-notify-start/bin/notify-start (code=exited, status=>
   Main PID: 1280 (code=exited, status=1/FAILURE)
         IP: 0B in, 0B out
        CPU: 6ms

Sep 05 12:38:14 nixos systemd[1]: Starting notify.service...
Sep 05 12:38:14 nixos notify-start[1286]: Could not connect: No such file or directory
Sep 05 12:38:14 nixos systemd[1]: notify.service: Main process exited, code=exited, status=1/FAILURE
Sep 05 12:38:14 nixos systemd[1]: notify.service: Failed with result 'exit-code'.
Sep 05 12:38:14 nixos systemd[1]: Failed to start notify.service.

```

If i run `notify.service` in the terminal, there is no error and the notification is displayed:

```auto
~ ❯ systemctl start notify ✘ 3
==== AUTHENTICATING FOR org.freedesktop.systemd1.manage-units ====
Authentication is required to start 'notify.service'.
Authenticating as: sperber
Password:
==== AUTHENTICATION COMPLETE ====

```

This works as well:

```auto
~ ❯ sudo systemctl start notify 7s
doas (sperber@nixos) password:

```

So there is something missing when run as root by systemd. What do i have to add?

---

<div class="post-metadata">

### Author: ![Father2303](https://discourse.nixos.org/user_avatar/discourse.nixos.org/father2303/32/8705_2.png) [@Father2303](https://discourse.nixos.org/u/Father2303)
#### Post date: [September 7, 2023, 1:43pm UTC](https://discourse.nixos.org/t/desktop-notifications-from-systemd-service/17672/9 "2023-09-07T13:43:52Z")

</div>

I set it up as a user service and after removing the specified user, it works like this:

```auto
      services."notify" = {
        script = ''
          ${pkgs.libnotify}/bin/notify-send --urgency=critical "Systemd Service has failed"
        '';
        environment = {
          # USER = "sperber";
          # HOME = "/home/sperber/";
          # Display = ":0";
          # DBUS_SESSION_BUS_ADDRESS = "unix:path=/run/user/%U/bus";
          # DBUS_SESSION_BUS_ADDRESS = "unix:path=/run/user/1000/bus";
          # XAUTHORITY = "/home/sperber/.Xauthority";
        };
        serviceConfig = {
          Type = "oneshot";
          # User = "sperber";
        };
      };

```

---

<div class="post-metadata">

### Author: ![Quelklef](https://discourse.nixos.org/user_avatar/discourse.nixos.org/quelklef/32/6757_2.png) [@Quelklef](https://discourse.nixos.org/u/Quelklef)
#### Post date: [May 21, 2025, 6:06am UTC](https://discourse.nixos.org/t/desktop-notifications-from-systemd-service/17672/10 "2025-05-21T06:06:41Z")

</div>

Chiming in to say that @Father2303’s suggestion worked for me. COULD NOT get it to work otherwise.

For others, here’s a minimal example

```nix
systemd.user.services.notify-example = {
  script = "${lib.getExe pkgs.libnotify} Hello";
  # Or: script = "${pkgs.libnotify}/bin/notify-send Hello";
};

```

Then, running

```bash
systemctl --user start notify-example

```

should successfully send “Hello” to your notification daemon
