about summary refs log tree commit diff
path: root/nixos/modules/services/misc/invidious-router.nix
blob: 7a90c6ab9ddc03a8b15e50769cf0845fc04fa958 (plain) (blame)
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
{
  config,
  lib,
  pkgs,
  ...
}: let
  cfg = config.services.invidious-router;
  settingsFormat = pkgs.formats.yaml {};
  configFile = settingsFormat.generate "config.yaml" cfg.settings;
in {
  meta.maintainers = [lib.maintainers.sils];

  options.services.invidious-router = {
    enable = lib.mkEnableOption "the invidious-router service";
    port = lib.mkOption {
      type = lib.types.port;
      default = 8050;
      description = ''
        Port to bind to.
      '';
    };
    address = lib.mkOption {
      type = lib.types.str;
      default = "127.0.0.1";
      description = ''
        Address on which invidious-router should listen on.
      '';
    };
    settings = lib.mkOption {
      type = lib.types.submodule {
        freeformType = settingsFormat.type;
      };
      default = {
        app = {
          listen = "127.0.0.1:8050";
          enable_youtube_fallback = false;
          reload_instance_list_interval = "60s";
        };
        api = {
          enabled = true;
          url = "https://api.invidious.io/instances.json";
          filter_regions = true;
          allowed_regions = [
            "AT"
            "DE"
            "CH"
          ];
        };
        healthcheck = {
          path = "/";
          allowed_status_codes = [
            200
          ];
          timeout = "1s";
          interval = "10s";
          filter_by_response_time = {
            enabled = true;
            qty_of_top_results = 3;
          };
          minimum_ratio = 0.2;
          remove_no_ratio = true;
          text_not_present = "YouTube is currently trying to block Invidious instances";
        };
      };
      description = ''
        Configuration for invidious-router.
        Check https://gitlab.com/gaincoder/invidious-router#configuration
        for configuration options.
      '';
    };
    package = lib.mkOption {
      type = lib.types.package;
      default = pkgs.invidious-router;
      defaultText = lib.literalExpression "pkgs.invidious-router";
      description = ''
        The invidious-router package to use.
      '';
    };
    nginx = {
      enable = lib.mkEnableOption ''
        Automatic nginx proxy configuration
      '';
      domain = lib.mkOption {
        type = lib.types.str;
        example = "invidious-router.example.com";
        description = ''
          The domain on which invidious-router should be served.
        '';
      };
      extraDomains = lib.mkOption {
        type = lib.types.listOf lib.types.str;
        default = [];
        description = ''
          Additional domains to serve invidious-router on.
        '';
      };
    };
  };
  config = lib.mkIf cfg.enable {
    systemd.services.invidious-router = {
      wantedBy = ["multi-user.target"];
      serviceConfig = {
        Restart = "on-failure";
        ExecStart = "${lib.getExe cfg.package} --configfile ${configFile}";
        DynamicUser = "yes";
      };
    };

    services.nginx.virtualHosts = lib.mkIf cfg.nginx.enable {
      ${cfg.nginx.domain} = {
        locations."/" = {
          recommendedProxySettings = true;
          proxyPass = "http://${cfg.address}:${toString cfg.port}";
        };
        enableACME = true;
        forceSSL = true;
        serverAliases = cfg.nginx.extraDomains;
      };
    };
  };
}