Hi everyone. I am an unfortunate owner of speakers with sleep mode enabled with no way to disable it.

Because I am frustrated by them all the time turning off on its own, I decided to make a timer playing 0 amplitude short sound to prevent them from going to sleep each, lets say, 10 minutes.

The problem is, I cannot make timer to work properly.

I chose to use aplay from alsa-utils, because it can run fully in terminal. Tested, and turning it by hand, works like a charm. Problem is with timer though. I created a test timer to check whether it works in configuration.nix as follows:

systemd.timers."test" = {
  wantedBy = [ "timers.target" ];
    timerConfig = {
      OnBootSec = "1m";
      OnUnitActiveSec = "1m";
      Unit = "test.service";
    };
};

systemd.services."test" = {
  path = [pkgs.alsa-utils];
  script = ''
echo $(date) >> /home/user/test.log
aplay /home/user/test_sound.mp3
  '';
  serviceConfig = {
    Type = "oneshot";
    User = "root";
  };
};

It logs current date to log file, which is printing correctly.

Although running systemctl status test shows that there is a problem with aplay

× test.service
     Loaded: loaded (/etc/systemd/system/test.service; linked; preset: enabled)
     Active: failed (Result: exit-code) since Sat 2023-11-25 22:55:18 CET; 1min 8s ago
TriggeredBy: ● test.timer
    Process: 199078 ExecStart=/nix/store/bh2qgcf1ba573b65imbbc1xsm9jfy8fb-unit-script-test-start/bin/test-start (code=exited, status=1/FAILURE)
   Main PID: 199078 (code=exited, status=1/FAILURE)
         IP: 0B in, 0B out
        CPU: 4ms

lis 25 22:55:18 pc systemd[1]: Starting test.service...
lis 25 22:55:18 pc test-start[199080]: ALSA lib pulse.c:242:(pulse_connect) PulseAudio: Unable to connect: Connection refused
lis 25 22:55:18 pc test-start[199080]: aplay: main:834: audio open error: Connection refused
lis 25 22:55:18 pc systemd[1]: test.service: Main process exited, code=exited, status=1/FAILURE
lis 25 22:55:18 pc systemd[1]: test.service: Failed with result 'exit-code'.
lis 25 22:55:18 pc systemd[1]: Failed to start test.service.
lis 25 22:56:26 pc systemd[1]: Starting test.service...

Does anybody knows how to deal with that?