nixpkgs/nixos/modules/services/networking/xray.nix
Silvan Mosberger 374e6bcc40 treewide: Format all Nix files
Format all Nix files using the officially approved formatter,
making the CI check introduced in the previous commit succeed:

  nix-build ci -A fmt.check

This is the next step of the of the [implementation](https://github.com/NixOS/nixfmt/issues/153)
of the accepted [RFC 166](https://github.com/NixOS/rfcs/pull/166).

This commit will lead to merge conflicts for a number of PRs,
up to an estimated ~1100 (~33%) among the PRs with activity in the past 2
months, but that should be lower than what it would be without the previous
[partial treewide format](https://github.com/NixOS/nixpkgs/pull/322537).

Merge conflicts caused by this commit can now automatically be resolved while rebasing using the
[auto-rebase script](8616af08d9/maintainers/scripts/auto-rebase).

If you run into any problems regarding any of this, please reach out to the
[formatting team](https://nixos.org/community/teams/formatting/) by
pinging @NixOS/nix-formatting.
2025-04-01 20:10:43 +02:00

108 lines
2.5 KiB
Nix

{
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.
'';
};
package = mkPackageOption pkgs "xray" { };
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" ];
script = ''
exec "${cfg.package}/bin/xray" -config "$CREDENTIALS_DIRECTORY/config.json"
'';
serviceConfig = {
DynamicUser = true;
LoadCredential = "config.json:${settingsFile}";
CapabilityBoundingSet = "CAP_NET_ADMIN CAP_NET_BIND_SERVICE";
AmbientCapabilities = "CAP_NET_ADMIN CAP_NET_BIND_SERVICE";
NoNewPrivileges = true;
};
};
};
}