summaryrefslogtreecommitdiff
path: root/custom/uucp-mediaclient.nix
blob: f01e2a548f4c8afbf0a1354b65c7cf04cae9f75b (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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
{ 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;
    wrapperdir = config.security.wrapperDir;
    mediadir = cfg.mediaDir;
    donotify = if cfg.notify.users != [] then "true" else "false";
    showtitle = if cfg.notify.extractTitle then "true" else "false";
    ffmpeg = if cfg.notify.users != [] then ffmpeg-full else "/no-such-path";
    notify = writeScript "notify.sh" ''
      #!${zsh}/bin/zsh

      input=$(cat)

      ${concatMapStringsSep "\n" (name: ''${wrapperdir}/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";
      };

      mediaGroup = mkOption {
        type = types.str;
        default = "media";
      };
    };
  };

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

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

    assertions = map (user: { assertion = elem user config.services.notify-users; message = "Notification must be allowed for ${user}!"; }) (unique cfg.notify.users);

    users.groups."${cfg.mediaGroup}" = {
      members = [ "uucp" ];
    };

    system.activationScripts = {
      media-directory = ''
        mkdir -m 0755 -pv ${cfg.mediaDir}
        chown :${cfg.mediaGroup} ${cfg.mediaDir}
        chmod -c "g+rwx" ${cfg.mediaDir}
      '';
    };
  };
}