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