|
1 | 1 | { config, pkgs, lib, ... }: |
2 | 2 |
|
3 | | -with lib; |
4 | | - |
| 3 | +let |
| 4 | + cfg = config.services.v2raya; |
| 5 | + inherit (lib.options) mkOption mkEnableOption mkPackageOption; |
| 6 | + inherit (lib) types maintainers mkIf optionals getExe literalExpression; |
| 7 | +in |
5 | 8 | { |
6 | 9 | options = { |
7 | 10 | services.v2raya = { |
8 | | - enable = options.mkEnableOption (mdDoc "the v2rayA service"); |
| 11 | + enable = mkEnableOption "the v2rayA service"; |
| 12 | + |
| 13 | + package = mkPackageOption pkgs "v2raya" { }; |
| 14 | + |
| 15 | + listenAddress = mkOption { |
| 16 | + type = types.str; |
| 17 | + default = "0.0.0.0"; |
| 18 | + example = "127.0.0.1"; |
| 19 | + description = '' |
| 20 | + The listening address of v2rayA. |
| 21 | + ''; |
| 22 | + }; |
| 23 | + |
| 24 | + listenPort = mkOption { |
| 25 | + type = types.port; |
| 26 | + default = 2017; |
| 27 | + description = '' |
| 28 | + The listening port of v2rayA. |
| 29 | + ''; |
| 30 | + }; |
| 31 | + |
| 32 | + configPath = mkOption { |
| 33 | + type = types.path; |
| 34 | + default = "/etc/v2raya"; |
| 35 | + description = '' |
| 36 | + The directory containing the configuration file of |
| 37 | + v2rayA. |
| 38 | + ''; |
| 39 | + }; |
| 40 | + |
| 41 | + v2rayPath = mkOption { |
| 42 | + type = with types; nullOr path; |
| 43 | + default = null; |
| 44 | + example = literalExpression ''"''${pkgs.xray}/bin/xray"''; |
| 45 | + description = '' |
| 46 | + v2ray executable file path. If set to `null`, will |
| 47 | + use v2ray provided in v2rayA wrapper. It can be |
| 48 | + modified to the file path of other v2ray branches |
| 49 | + such as xray. |
| 50 | + ''; |
| 51 | + }; |
| 52 | + |
| 53 | + logFile = mkOption { |
| 54 | + type = types.path; |
| 55 | + default = "/var/log/v2raya/v2raya.log"; |
| 56 | + description = '' |
| 57 | + The path of v2rayA log file. |
| 58 | + ''; |
| 59 | + }; |
| 60 | + |
| 61 | + environment = mkOption { |
| 62 | + type = types.attrsOf lib.types.str; |
| 63 | + default = { }; |
| 64 | + example = literalExpression '' |
| 65 | + { |
| 66 | + V2RAYA_PLUGINLISTENPORT = "32346"; |
| 67 | + V2RAYA_PASSCHECKROOT = "1"; |
| 68 | + V2RAYA_FORCE_IPV6_ON = "1"; |
| 69 | + } |
| 70 | + ''; |
| 71 | + description = '' |
| 72 | + Environment variables added to v2raya systemd service. |
| 73 | + ''; |
| 74 | + }; |
9 | 75 | }; |
10 | 76 | }; |
11 | 77 |
|
12 | | - config = mkIf config.services.v2raya.enable { |
13 | | - environment.systemPackages = [ pkgs.v2raya ]; |
| 78 | + config = mkIf cfg.enable { |
| 79 | + environment.systemPackages = [ cfg.package ]; |
14 | 80 |
|
15 | | - systemd.services.v2raya = |
16 | | - let |
17 | | - nftablesEnabled = config.networking.nftables.enable; |
18 | | - iptablesServices = [ |
| 81 | + systemd.services.v2raya = { |
| 82 | + unitConfig = { |
| 83 | + Description = "v2rayA service"; |
| 84 | + Documentation = "https://github.com/v2rayA/v2rayA/wiki"; |
| 85 | + After = [ |
| 86 | + "network.target" |
| 87 | + "network-online.target" |
| 88 | + "nss-lookup.target" |
19 | 89 | "iptables.service" |
20 | | - ] ++ optional config.networking.enableIPv6 "ip6tables.service"; |
21 | | - tableServices = if nftablesEnabled then [ "nftables.service" ] else iptablesServices; |
22 | | - in |
23 | | - { |
24 | | - unitConfig = { |
25 | | - Description = "v2rayA service"; |
26 | | - Documentation = "https://github.com/v2rayA/v2rayA/wiki"; |
27 | | - After = [ |
28 | | - "network.target" |
29 | | - "nss-lookup.target" |
30 | | - ] ++ tableServices; |
31 | | - Wants = [ "network.target" ]; |
32 | | - }; |
33 | | - |
34 | | - serviceConfig = { |
35 | | - User = "root"; |
36 | | - ExecStart = "${getExe pkgs.v2raya} --log-disable-timestamp"; |
37 | | - Environment = [ "V2RAYA_LOG_FILE=/var/log/v2raya/v2raya.log" ]; |
38 | | - LimitNPROC = 500; |
39 | | - LimitNOFILE = 1000000; |
40 | | - Restart = "on-failure"; |
41 | | - Type = "simple"; |
42 | | - }; |
43 | | - |
44 | | - wantedBy = [ "multi-user.target" ]; |
45 | | - path = with pkgs; [ iptables bash iproute2 ]; # required by v2rayA TProxy functionality |
| 90 | + "ip6tables.service" |
| 91 | + "nftables.service" |
| 92 | + ]; |
| 93 | + Wants = [ "network.target" ]; |
46 | 94 | }; |
| 95 | + |
| 96 | + serviceConfig = { |
| 97 | + User = "root"; |
| 98 | + ExecStart = "${getExe cfg.package} --log-disable-timestamp"; |
| 99 | + LimitNPROC = 500; |
| 100 | + LimitNOFILE = 1000000; |
| 101 | + Restart = "on-failure"; |
| 102 | + Type = "simple"; |
| 103 | + }; |
| 104 | + |
| 105 | + environment = { |
| 106 | + V2RAYA_ADDRESS = "${cfg.listenAddress}:${toString cfg.listenPort}"; |
| 107 | + V2RAYA_CONFIG = cfg.configPath; |
| 108 | + V2RAYA_V2RAY_BIN = cfg.v2rayPath; |
| 109 | + V2RAYA_LOG_FILE = cfg.logFile; |
| 110 | + } // cfg.environment; |
| 111 | + |
| 112 | + wantedBy = [ "multi-user.target" ]; |
| 113 | + path = with pkgs; [ iptables bash iproute2 ]; # required by v2rayA TProxy functionality |
| 114 | + }; |
47 | 115 | }; |
48 | 116 |
|
49 | 117 | meta.maintainers = with maintainers; [ elliot ]; |
|
0 commit comments