This repository has no description
5.2 kB
194 lines
1{ config, pkgs, ... }: let
2
3nginxCfg = pkgs.writeText "nginx.conf" ''
4 daemon off;
5 pid /var/lib/rtmp/nginx.pid;
6 events {
7 use epoll;
8 worker_connections 128;
9 }
10 error_log stderr info;
11
12 http {
13 client_body_temp_path /var/lib/rtmp/nginx_cache_client_body;
14 proxy_temp_path /var/lib/rtmp/nginx_cache_proxy;
15 fastcgi_temp_path /var/lib/rtmp/nginx_cache_fastcgi;
16 uwsgi_temp_path /var/lib/rtmp/nginx_cache_uwsgi;
17 scgi_temp_path /var/lib/rtmp/nginx_cache_scgi;
18
19 server {
20 listen 8080;
21 root /var/lib/rtmp;
22 access_log stderr;
23 error_log stderr;
24
25 # This URL provides RTMP statistics in XML
26 location /stat {
27 rtmp_stat all;
28 }
29 }
30 }
31
32 rtmp {
33 server {
34 access_log stderr;
35 listen 1935;
36 ping 30s;
37 notify_method get;
38
39 application stream {
40 live on;
41
42 hls on;
43 hls_path /var/lib/rtmp/tmp/hls;
44 hls_fragment 1;
45 hls_playlist_length 10;
46
47 dash on;
48 dash_path /var/lib/rtmp/tmp/dash;
49 }
50 }
51 }
52'';
53
54in {
55
56 services.nginx = {
57 enable = true;
58 virtualHosts."streaming.lassul.us" = {
59 enableACME = true;
60 addSSL = true;
61 locations."/hls".extraConfig = ''
62 # Serve HLS fragments
63 types {
64 application/vnd.apple.mpegurl m3u8;
65 video/mp2t ts;
66 }
67 root /var/lib/rtmp/tmp;
68
69 # Allow CORS preflight requests
70 if ($request_method = 'OPTIONS') {
71 add_header 'Access-Control-Allow-Origin' '*';
72 add_header 'Access-Control-Max-Age' 1728000;
73 add_header 'Content-Type' 'text/plain charset=UTF-8';
74 add_header 'Content-Length' 0;
75 return 204;
76 }
77
78 if ($request_method != 'OPTIONS') {
79 add_header Cache-Control no-cache;
80
81 # CORS setup
82 add_header 'Access-Control-Allow-Origin' '*' always;
83 add_header 'Access-Control-Expose-Headers' 'Content-Length';
84 }
85 '';
86 locations."/dash".extraConfig = ''
87 # Serve DASH fragments
88 types {
89 application/dash+xml mpd;
90 video/mp4 mp4;
91 }
92 root /var/lib/rtmp/tmp;
93
94 # Allow CORS preflight requests
95 if ($request_method = 'OPTIONS') {
96 add_header 'Access-Control-Allow-Origin' '*';
97 add_header 'Access-Control-Max-Age' 1728000;
98 add_header 'Content-Type' 'text/plain charset=UTF-8';
99 add_header 'Content-Length' 0;
100 return 204;
101 }
102 if ($request_method != 'OPTIONS') {
103 add_header Cache-Control no-cache;
104
105 # CORS setup
106 add_header 'Access-Control-Allow-Origin' '*' always;
107 add_header 'Access-Control-Expose-Headers' 'Content-Length';
108 }
109 '';
110 locations."= /dash.all.min.js".extraConfig = ''
111 default_type "text/javascript";
112 alias ${pkgs.fetchurl {
113 url = "http://cdn.dashjs.org/v3.2.0/dash.all.min.js";
114 sha256 = "16f0b40gdqsnwqi01s5sz9f1q86dwzscgc3m701jd1sczygi481c";
115 }};
116 '';
117 locations."= /player".extraConfig = ''
118 default_type "text/html";
119 alias ${pkgs.writeText "player.html" ''
120 <!DOCTYPE html>
121 <html lang="en">
122 <head>
123 <meta charset="utf-8">
124 <title>lassulus livestream</title>
125 </head>
126 <body>
127 <div>
128 <video id="player" controls></video>
129 </video>
130 </div>
131 <script src="/dash.all.min.js"></script>
132 <script>
133 (function(){
134 var url = "/dash/nixos.mpd";
135 var player = dashjs.MediaPlayer().create();
136 player.initialize(document.querySelector("#player"), url, true);
137 })();
138 </script>
139 </body>
140 </html>
141 ''};
142 '';
143 locations."/records".extraConfig = ''
144 autoindex on;
145 root /var/lib/rtmp;
146 '';
147 };
148 };
149
150 fileSystems."/var/lib/rtmp/tmp" = {
151 device = "tmpfs";
152 fsType = "tmpfs";
153 options = [ "nosuid" "nodev" "noatime" ];
154 };
155
156 users.users.rtmp = {
157 home = "/var/lib/rtmp";
158 uid = genid_uint31 "rtmp";
159 isNormalUser = true;
160 createHome = true;
161 openssh.authorizedKeys.keys = with config.krebs.users; [
162 mic92.pubkey
163 palo.pubkey
164 ];
165 };
166
167 systemd.services.nginx-rtmp = {
168 wantedBy = [ "multi-user.target" ];
169 after = [ "network.target" ];
170 restartIfChanged = true;
171 script = ''
172 ${pkgs.nginx.override {
173 modules = [
174 pkgs.nginxModules.rtmp
175 ];
176 }}/bin/nginx -c ${nginxCfg} -p /var/lib/rtmp
177 '';
178 serviceConfig = {
179 ExecStartPre = pkgs.writers.writeDash "setup-rtmp" ''
180 mkdir -p /var/lib/rtmp/tmp/hls
181 mkdir -p /var/lib/rtmp/tmp/dash
182 chown rtmp:users /var/lib/rtmp/tmp/hls
183 chown rtmp:users /var/lib/rtmp/tmp/dash
184 chmod 755 /var/lib/rtmp/tmp/hls
185 chmod 755 /var/lib/rtmp/tmp/dash
186 '';
187 User = "rtmp";
188 };
189 };
190
191 krebs.iptables.tables.filter.INPUT.rules = [
192 { predicate = "-p tcp --dport 1935"; target = "ACCEPT"; }
193 ];
194}