summaryrefslogtreecommitdiff
path: root/custom/uucp-mediaclient.nix
blob: 3b49c9c704dbbdb73bc6e09461b53367be93fc3b (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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
{ config, lib, pkgs, ... }:

with lib;

let
  cfg = config.services.uucp.media-client;

  recv-media = with pkgs; stdenv.mkDerivation rec {
    name = "recv-media";
    src = ./recv-media.sh;

    phases = [ "unpackPhase" "buildPhase" "installPhase" ];

    unpackPhase = ''
      cp $src recv-media
    '';

    inherit zsh coreutils utillinux;
    inherit (config.security) wrapperDir;
    inherit (cfg) mediaDir;
    doNotify = cfg.notify.users != [];
    ffmpeg = if doNotify then ffmpeg-full else "/no-such-path";
    notify = writeScript "notify.sh" ''
      #!${zsh}/bin/zsh

      input=$(cat)

      ${concatMapStringsSep "\n" (name: ''${notify-user name}/bin/notify-${name} $@ <<<''${input}'') cfg.notify.users}
    '';

    buildPhase = ''
      substituteAllInPlace recv-media
    '';

    installPhase = ''
      mkdir -p $out/bin

      install -m 755 -t $out/bin \
        recv-media
    '';
  };


in {
  options = {
    services.uucp.media-client = {
      remoteNodes = mkOption {
        type = with types; listOf str;
        default = [];
        description = ''
          Servers to receive media from
        '';
      };

      notify = {
        users = mkOption {
          type = with types; listOf str;
          default = [];
          description = ''
            Users to notify
          '';
        };

        extractTitle = mkOption {
          type = types.bool;
          default = true;
          description = ''
            Use ffmpeg to include the media title in notifications
          '';
        };
      };

      mediaDir = mkOption {
        type = types.path;
        default = "/var/media";
        description = "Media directory";
      };
    };
  };

  imports = [ ./notify-users.nix ];

  config = mkIf (cfg.remoteNodes != []) {
    services.uucp.commandPath = [ "${recv-media}/bin" ];
    services.uucp.remoteNodes = genAttrs cfg.remoteNodes (name: { commands = ["recv-media"]; } );

    services.notify-users = mkIf (cfg.notify.users != []) notify.users;
  };
}