summaryrefslogtreecommitdiffstats
path: root/nixos/modules/services/web-apps/rsshub.nix
blob: f59948fb60851efe8086b95ea5a177bc56d6bce5 (plain)
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
{
  lib,
  config,
  pkgs,
  ...
}:

let
  cfg = config.services.rsshub;
in
{
  options.services.rsshub = {
    enable = lib.mkEnableOption "RSSHub service";

    package = lib.mkPackageOption pkgs "rsshub" { };

    openFirewall = lib.mkOption {
      type = lib.types.bool;
      default = false;
      description = "Whether to open the firewall for the specified port.";
    };

    settings = lib.mkOption {
      type = lib.types.submodule {
        freeformType = lib.types.attrsOf lib.types.str;
        options = {
          LISTEN_INADDR_ANY = lib.mkOption {
            type = lib.types.bool;
            default = false;
            description = "Listen to any address";
            apply = x: if x then "1" else "0";
          };
          PORT = lib.mkOption {
            type = lib.types.port;
            default = 1200;
            description = "Listen on port.";
            apply = toString;
          };
          NO_LOGFILES = lib.mkOption {
            type = lib.types.bool;
            default = true;
            description = "Print logs into stderr.";
            apply = x: if x then "1" else "0";
          };
        };
      };
      default = { };
      example = lib.literalExpression ''
        {
          REQUEST_TIMEOUT = "3000";
          REQUEST_RETRY = "10";
          CHROMIUM_EXECUTABLE_PATH = lib.getExe pkgs.chromium;
        }
      '';
      description = ''
        Environment variables for RSSHub.
        See <https://docs.rsshub.app/deploy/config> for available options.
      '';
    };

    secretFiles = lib.mkOption {
      type = lib.types.listOf lib.types.path;
      default = [ ];
      example = lib.literalExpression ''
        [ config.sops.secrets.rsshub.path ]
      '';
      description = ''
        Environment variables stored in files for secrets.
        See <https://docs.rsshub.app/deploy/config> for available options.
      '';
    };

    redis = {
      enable = lib.mkEnableOption "Redis for RSSHub";
      createLocally = lib.mkOption {
        type = lib.types.bool;
        default = true;
        description = "Create and use a local Redis instance. Sets `services.redis.servers.rsshub`.";
      };
      host = lib.mkOption {
        type = lib.types.str;
        default = "localhost";
        description = "The Redis host.";
      };
      port = lib.mkOption {
        type = lib.types.port;
        default = 6379;
        description = "The Redis port.";
      };
    };
  };

  config = lib.mkIf cfg.enable {
    services.redis.servers.rsshub = lib.mkIf (cfg.redis.enable && cfg.redis.createLocally) {
      enable = true;
      port = cfg.redis.port;
    };

    services.rsshub.settings = lib.mkIf cfg.redis.enable {
      CACHE_TYPE = "redis";
      REDIS_URL = "redis://${cfg.redis.host}:${toString cfg.redis.port}";
    };

    systemd.services.rsshub = {
      description = "RSSHub - Everything is RSSible";
      wantedBy = [ "multi-user.target" ];
      after = lib.optional (cfg.redis.enable && cfg.redis.createLocally) "redis-rsshub.service";
      requires = lib.optional (cfg.redis.enable && cfg.redis.createLocally) "redis-rsshub.service";

      environment = cfg.settings;

      serviceConfig = {
        Type = "simple";
        User = "rsshub";
        Group = "rsshub";
        DynamicUser = true;
        StateDirectory = "rsshub";
        EnvironmentFile = cfg.secretFiles;
        ExecStart = lib.getExe cfg.package;
        Restart = "on-failure";
        RestartSec = "10s";

        # Hardening
        NoNewPrivileges = true;
        PrivateTmp = true;
        ProtectSystem = "strict";
        ProtectHome = true;
        ProtectKernelTunables = true;
        ProtectKernelModules = true;
        ProtectControlGroups = true;
      };
    };

    networking.firewall.allowedTCPPorts = lib.mkIf cfg.openFirewall [ (lib.toInt cfg.settings.PORT) ];
  };

  meta.maintainers = with lib.maintainers; [ vonfry ];
}