summaryrefslogtreecommitdiff
path: root/nixos/modules/services/ambience.nix
blob: a4e03cf22182d068ea4ba57f3dc78910d55fbc93 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
{ config, lib, pkgs, ... }:
with lib;

let
  cfg = config.eth.services.ambience;

  mkService = name: opts: {
    name = "ambience-${name}";
    value = {
      enable = opts.enable;
      description = "Play ambient ${name} on loop";
      wants = [ "sound.target" ];
      after = [ "sound.target" ];
      wantedBy = [ "multi-user.target" ];
      partOf = [ "snapserver.service" ];
      serviceConfig = {
        DynamicUser = true;
        Group = "audio";

        ExecStart = "${pkgs.mpv}/bin/mpv --audio-display=no --audio-channels=stereo --audio-samplerate=48000 --audio-format=s16 --ao=pcm --ao-pcm-file=${opts.pipe} --loop=inf ${opts.file}";
        NoNewPrivileges = true;
        ProtectHome = true;
        ProtectKernelTunables = true;
        ProtectControlGroups = true;
        ProtectKernelModules = true;
        RestrictAddressFamilies = "";
        RestrictNamespaces = true;
      };
    };
  };

in {
  options.eth.services.ambience = {
    enable = mkEnableOption "Play ambient sounds.";

    streams = mkOption {
      type = types.attrsOf (types.submodule {
        options = {
          enable = mkEnableOption "Play ambient sounds.";

          file = mkOption {
            type = types.str;
            description = "file to play on loop";
            default = "";
            example = "/var/lib/ambience/rain.m4a";
          };

          pipe = mkOption {
            type = types.str;
            description = "pipe to play into";
            default = "";
            example = "/run/snapserver/rain";
          };
        };
      });
      default = {};
      # example = { rain = { enable = true; file = "/var/lib/ambience/rain.mp3"; pipe = "/run/snapserver/rain"; }; };
      description = "Streams to pipe to Snapserver";
    };
  };

  config = mkIf cfg.enable {
    assertions = [
      {
        assertion = config.services.snapserver.enable;
        message = "must enable Snapserver to enable Ambience";
      }
    ];
    systemd.services = listToAttrs (mapAttrsToList mkService cfg.streams);
  };
}