78 lines
2.2 KiB
Nix
Raw Normal View History

2022-03-22 06:03:48 +03:00
{ config, lib, pkgs, ... }:
with lib; {
options = {
services.xray = {
enable = mkOption {
type = types.bool;
default = false;
description = ''
Whether to run xray server.
2022-11-21 02:53:20 +03:00
Either <literal>settingsFile</literal> or <literal>config</literal> must be specified.
2022-03-22 06:03:48 +03:00
'';
};
package = mkOption {
type = types.package;
2022-11-21 02:53:20 +03:00
default = pkgs.xray;
defaultText = literalExpression "pkgs.xray";
2022-03-22 06:03:48 +03:00
description = ''
Which xray package to use.
'';
};
2022-11-21 02:53:20 +03:00
settingsFile = mkOption {
2022-03-22 06:03:48 +03:00
type = types.nullOr types.str;
default = null;
example = "/etc/xray/config.json";
description = ''
The absolute path to the configuration file.
2022-11-21 02:53:20 +03:00
Either <literal>settingsFile</literal> or <literal>config</literal> must be specified.
2022-03-22 06:03:48 +03:00
'';
};
config = 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.
2022-11-21 02:53:20 +03:00
Either `settingsFile` or `config` must be specified.
2022-03-22 06:03:48 +03:00
'';
};
};
};
config = let
cfg = config.services.xray;
2022-11-21 02:53:20 +03:00
settingsFile = if cfg.settingsFile != null
then cfg.settingsFile
2022-03-22 06:03:48 +03:00
else pkgs.writeTextFile {
name = "xray.json";
text = builtins.toJSON cfg.config;
checkPhase = ''
${cfg.package}/bin/xray run -test -format=json -config $out
'';
};
in mkIf cfg.enable {
assertions = [
{
2022-11-21 02:53:20 +03:00
assertion = (cfg.settingsFile == null) != (cfg.config == null);
message = "Either but not both `settingsFile` and `config` should be specified for xray.";
2022-03-22 06:03:48 +03:00
}
];
systemd.services.xray = {
description = "Xray Daemon";
after = [ "network.target" ];
wantedBy = [ "multi-user.target" ];
serviceConfig = {
2022-11-21 02:53:20 +03:00
ExecStart = "${cfg.package}/bin/xray run -format=json -config ${settingsFile}";
2022-03-22 06:03:48 +03:00
};
};
};
}