From 51170db7ce1b6560fda24c4f949ec9ed9b4b97cb Mon Sep 17 00:00:00 2001
From: Ethel Morgan <eth@ethulhu.co.uk>
Date: Wed, 24 Jun 2020 16:01:35 +0100
Subject: add services.ambience, to add ambient sounds to snapserver

---
 nixos/modules/services/ambience.nix | 71 +++++++++++++++++++++++++++++++++++++
 1 file changed, 71 insertions(+)
 create mode 100644 nixos/modules/services/ambience.nix

(limited to 'nixos/modules/services')

diff --git a/nixos/modules/services/ambience.nix b/nixos/modules/services/ambience.nix
new file mode 100644
index 0000000..a4e03cf
--- /dev/null
+++ b/nixos/modules/services/ambience.nix
@@ -0,0 +1,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);
+  };
+}
-- 
cgit v1.2.3