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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
{ depot, lib, config, ... }:

let
  cfg = config.profpatsch.mediathek;
  mediathek = depot.users.Profpatsch.mediathek;
  stateDir = "/var/lib/mediathek";
  dbPath = "${stateDir}/mediathek.db";

  hardening = {
    ProtectSystem = "strict";
    ProtectHome = true;
    PrivateTmp = true;
    PrivateDevices = true;
    NoNewPrivileges = true;
    ProtectKernelTunables = true;
    ProtectKernelModules = true;
    ProtectControlGroups = true;
    RestrictSUIDSGID = true;
    LockPersonality = true;
    RestrictAddressFamilies = [ "AF_INET" "AF_INET6" "AF_UNIX" ];
    ReadWritePaths = [ stateDir ];
  };
in
{
  options.profpatsch.mediathek = {
    enable = lib.mkEnableOption "mediathek — the MediathekView film list as a PeerTube instance for NewPipe";

    host = lib.mkOption {
      type = lib.types.str;
      default = "mediathek-newpipe.softwaregardening.org";
      description = "Public host name served by nginx; also the base URL of every link the API hands out.";
    };

    port = lib.mkOption {
      type = lib.types.port;
      # 8765-8780 are taken by other services on legosi.
      default = 8781;
      description = "Local port of the server";
    };
  };

  config = lib.mkIf cfg.enable {
    # A static user rather than DynamicUser: the server and the importer must
    # share the database file, and the importer is a oneshot that is not
    # running most of the time, so the two units must get the same uid.
    users.users.mediathek = {
      isSystemUser = true;
      group = "mediathek";
      home = stateDir;
    };
    users.groups.mediathek = { };

    systemd.services.mediathek = {
      description = "mediathek — MediathekView film list as a PeerTube instance";
      wantedBy = [ "multi-user.target" ];
      after = [ "network.target" ];
      serviceConfig = hardening // {
        ExecStart = lib.escapeShellArgs [
          "${mediathek}/bin/mediathek"
          "serve"
          "--db"
          dbPath
          "--addr"
          "127.0.0.1:${toString cfg.port}"
          "--base-url"
          "https://${cfg.host}"
        ];
        User = "mediathek";
        Group = "mediathek";
        StateDirectory = "mediathek";
        Restart = "on-failure";
        RestartSec = 5;
        MemoryMax = "512M";
      };
    };

    # Daily full import. A new generation goes live only after verification;
    # the server picks it up on its next request, so no restart is needed.
    # A failed import leaves the unit failed and the old data live — see
    # mediathek(1), GENERATIONS.
    systemd.services.mediathek-import = {
      description = "mediathek — import the MediathekView film list";
      after = [ "network-online.target" ];
      wants = [ "network-online.target" ];
      serviceConfig = hardening // {
        Type = "oneshot";
        ExecStart = lib.escapeShellArgs [
          "${mediathek}/bin/mediathek"
          "import"
          "--db"
          dbPath
        ];
        User = "mediathek";
        Group = "mediathek";
        StateDirectory = "mediathek";
        # Parsing streams at a few MB; the rest is SQLite's page cache.
        MemoryMax = "1G";
        # Background work on a small shared machine.
        # Not IOSchedulingClass=idle: the import reads the whole generation
        # back several times (dedup, full-text index), and on this host the
        # idle class starved it for tens of minutes.
        Nice = 10;
        CPUWeight = 20;
        TimeoutStartSec = "2h";
      };
    };

    systemd.timers.mediathek-import = {
      description = "mediathek — daily film list import";
      wantedBy = [ "timers.target" ];
      timerConfig = {
        # MediathekView rebuilds the full list several times a day; early
        # morning keeps the import clear of evening viewing.
        OnCalendar = "*-*-* 04:30:00";
        RandomizedDelaySec = "30m";
        Persistent = true;
      };
    };

    services.nginx.virtualHosts.${cfg.host} = {
      forceSSL = true;
      enableACME = true;
      locations."/" = {
        proxyPass = "http://127.0.0.1:${toString cfg.port}";
        extraConfig = ''
          proxy_set_header Host $host;
          proxy_set_header X-Real-IP $remote_addr;
          proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
          proxy_set_header X-Forwarded-Proto $scheme;
        '';
      };
    };
  };
}