2022-08-23 15:48:54 +08:00
|
|
|
{
|
|
|
|
config,
|
|
|
|
lib,
|
|
|
|
pkgs,
|
|
|
|
...
|
|
|
|
}:
|
|
|
|
|
|
|
|
with lib;
|
|
|
|
|
|
|
|
{
|
|
|
|
options = {
|
|
|
|
|
|
|
|
services.xray = {
|
|
|
|
enable = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
|
|
|
description = ''
|
|
|
|
Whether to run xray server.
|
|
|
|
|
|
|
|
Either `settingsFile` or `settings` must be specified.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2023-11-27 01:19:27 +01:00
|
|
|
package = mkPackageOption pkgs "xray" { };
|
2022-08-23 15:48:54 +08:00
|
|
|
|
|
|
|
settingsFile = mkOption {
|
|
|
|
type = types.nullOr types.path;
|
|
|
|
default = null;
|
|
|
|
example = "/etc/xray/config.json";
|
|
|
|
description = ''
|
|
|
|
The absolute path to the configuration file.
|
|
|
|
|
|
|
|
Either `settingsFile` or `settings` must be specified.
|
|
|
|
|
|
|
|
See <https://www.v2fly.org/en_US/config/overview.html>.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
settings = mkOption {
|
|
|
|
type = types.nullOr (types.attrsOf types.unspecified);
|
|
|
|
default = null;
|
|
|
|
example = {
|
|
|
|
inbounds = [
|
|
|
|
{
|
|
|
|
port = 1080;
|
|
|
|
listen = "127.0.0.1";
|
|
|
|
protocol = "http";
|
|
|
|
}
|
|
|
|
];
|
|
|
|
outbounds = [
|
|
|
|
{
|
|
|
|
protocol = "freedom";
|
|
|
|
}
|
|
|
|
];
|
|
|
|
};
|
|
|
|
description = ''
|
|
|
|
The configuration object.
|
|
|
|
|
|
|
|
Either `settingsFile` or `settings` must be specified.
|
|
|
|
|
|
|
|
See <https://www.v2fly.org/en_US/config/overview.html>.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
config =
|
|
|
|
let
|
|
|
|
cfg = config.services.xray;
|
|
|
|
settingsFile =
|
|
|
|
if cfg.settingsFile != null then
|
|
|
|
cfg.settingsFile
|
|
|
|
else
|
|
|
|
pkgs.writeTextFile {
|
|
|
|
name = "xray.json";
|
|
|
|
text = builtins.toJSON cfg.settings;
|
|
|
|
checkPhase = ''
|
|
|
|
${cfg.package}/bin/xray -test -config $out
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
in
|
|
|
|
mkIf cfg.enable {
|
|
|
|
assertions = [
|
|
|
|
{
|
|
|
|
assertion = (cfg.settingsFile == null) != (cfg.settings == null);
|
|
|
|
message = "Either but not both `settingsFile` and `settings` should be specified for xray.";
|
|
|
|
}
|
|
|
|
];
|
|
|
|
|
|
|
|
systemd.services.xray = {
|
|
|
|
description = "xray Daemon";
|
|
|
|
after = [ "network.target" ];
|
|
|
|
wantedBy = [ "multi-user.target" ];
|
2024-12-28 14:20:43 +05:00
|
|
|
script = ''
|
|
|
|
exec "${cfg.package}/bin/xray" -config "$CREDENTIALS_DIRECTORY/config.json"
|
|
|
|
'';
|
2022-08-23 15:48:54 +08:00
|
|
|
serviceConfig = {
|
|
|
|
DynamicUser = true;
|
2024-12-28 14:20:43 +05:00
|
|
|
LoadCredential = "config.json:${settingsFile}";
|
2023-06-06 03:12:48 +00:00
|
|
|
CapabilityBoundingSet = "CAP_NET_ADMIN CAP_NET_BIND_SERVICE";
|
|
|
|
AmbientCapabilities = "CAP_NET_ADMIN CAP_NET_BIND_SERVICE";
|
|
|
|
NoNewPrivileges = true;
|
2025-04-01 20:10:43 +02:00
|
|
|
};
|
2022-08-23 15:48:54 +08:00
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|