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