1{
2 config,
3 lib,
4 pkgs,
5 ...
6}: let
7 cfg = config.services.invidious-router;
8 settingsFormat = pkgs.formats.yaml {};
9 configFile = settingsFormat.generate "config.yaml" cfg.settings;
10in {
11 meta.maintainers = [lib.maintainers.s1ls];
12
13 options.services.invidious-router = {
14 enable = lib.mkEnableOption "Enables the invidious-router service";
15 port = lib.mkOption {
16 type = lib.types.port;
17 default = 8050;
18 description = ''
19 Port to bind to.
20 '';
21 };
22 address = lib.mkOption {
23 type = lib.types.str;
24 default = "127.0.0.1";
25 description = ''
26 Address on which invidious-router should listen on.
27 '';
28 };
29 settings = lib.mkOption {
30 type = lib.types.submodule {
31 freeformType = settingsFormat.type;
32 };
33 default = {
34 app = {
35 listen = "127.0.0.1:8050";
36 enable_youtube_fallback = false;
37 reload_instance_list_interval = "60s";
38 };
39 api = {
40 enabled = true;
41 url = "https://api.invidious.io/instances.json";
42 filter_regions = true;
43 allowed_regions = [
44 "AT"
45 "DE"
46 "CH"
47 ];
48 };
49 healthcheck = {
50 path = "/";
51 allowed_status_codes = [
52 200
53 ];
54 timeout = "1s";
55 interval = "10s";
56 filter_by_response_time = {
57 enabled = true;
58 qty_of_top_results = 3;
59 };
60 minimum_ratio = 0.2;
61 remove_no_ratio = true;
62 text_not_present = "YouTube is currently trying to block Invidious instances";
63 };
64 };
65 description = ''
66 Configuration for invidious-router.
67 Check https://gitlab.com/gaincoder/invidious-router#configuration
68 for configuration options.
69 '';
70 };
71 package = lib.mkOption {
72 type = lib.types.package;
73 default = pkgs.invidious-router;
74 defaultText = lib.literalExpression "pkgs.invidious-router";
75 description = ''
76 The invidious-router package to use.
77 '';
78 };
79 nginx = {
80 enable = lib.mkEnableOption ''
81 Automatic nginx proxy configuration
82 '';
83 domain = lib.mkOption {
84 type = lib.types.str;
85 example = "invidious-router.example.com";
86 description = ''
87 The domain on which invidious-router should be served.
88 '';
89 };
90 extraDomains = lib.mkOption {
91 type = lib.types.listOf lib.types.str;
92 default = [];
93 description = ''
94 Additional domains to serve invidious-router on.
95 '';
96 };
97 };
98 };
99 config = lib.mkIf cfg.enable {
100 systemd.services.invidious-router = {
101 wantedBy = ["multi-user.target"];
102 serviceConfig = {
103 Restart = "on-failure";
104 ExecStart = "${lib.getExe cfg.package} --configfile ${configFile}";
105 DynamicUser = "yes";
106 };
107 };
108
109 services.nginx.virtualHosts = lib.mkIf cfg.nginx.enable {
110 ${cfg.nginx.domain} = {
111 locations."/" = {
112 recommendedProxySettings = true;
113 proxyPass = "http://${cfg.address}:${toString cfg.port}";
114 };
115 enableACME = true;
116 forceSSL = true;
117 serverAliases = cfg.nginx.extraDomains;
118 };
119 };
120 };
121}