Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

v2ray: init at 4.21.3 #66089

Merged
merged 4 commits into from
Dec 3, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions maintainers/maintainer-list.nix
Original file line number Diff line number Diff line change
Expand Up @@ -6062,6 +6062,16 @@
githubId = 307899;
name = "Gurkan Gur";
};
servalcatty = {
email = "servalcat@pm.me";
github = "servalcatty";
servalcatty marked this conversation as resolved.
Show resolved Hide resolved
githubid = 51969817;
name = "Serval";
keys = [{
longkeyid = "rsa4096/0x4A2AAAA382F8294C";
fingerprint = "A317 37B3 693C 921B 480C C629 4A2A AAA3 82F8 294C";
}];
};
sfrijters = {
email = "sfrijters@gmail.com";
github = "sfrijters";
Expand Down
1 change: 1 addition & 0 deletions nixos/modules/module-list.nix
Original file line number Diff line number Diff line change
Expand Up @@ -724,6 +724,7 @@
./services/networking/tvheadend.nix
./services/networking/unbound.nix
./services/networking/unifi.nix
./services/networking/v2ray.nix
./services/networking/vsftpd.nix
./services/networking/wakeonlan.nix
./services/networking/websockify.nix
Expand Down
81 changes: 81 additions & 0 deletions nixos/modules/services/networking/v2ray.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
{ config, lib, pkgs, ... }:

with lib;

{
options = {

services.v2ray = {
enable = mkOption {
type = types.bool;
default = false;
description = ''
Whether to run v2ray server.

Either <literal>configFile</literal> or <literal>config</literal> must be specified.
'';
};

configFile = mkOption {
type = types.nullOr types.str;
default = null;
example = "/etc/v2ray/config.json";
description = ''
The absolute path to the configuration file.

Either <literal>configFile</literal> or <literal>config</literal> must be specified.

See <link xlink:href="https://v2ray.com/en/configuration/overview.html"/>.
'';
};

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.

Either `configFile` or `config` must be specified.
servalcatty marked this conversation as resolved.
Show resolved Hide resolved

See <link xlink:href="https://v2ray.com/en/configuration/overview.html"/>.
'';
};
};

};

config = let
cfg = config.services.v2ray;
configFile = if cfg.configFile != null
then cfg.configFile
else (pkgs.writeText "v2ray.json" (builtins.toJSON cfg.config));

in mkIf cfg.enable {
assertions = [
{
assertion = (cfg.configFile == null) != (cfg.config == null);
message = "Either but not both `configFile` and `config` should be specified for v2ray.";
}
];

systemd.services.v2ray = {
description = "v2ray Daemon";
after = [ "network.target" ];
wantedBy = [ "multi-user.target" ];
path = [ pkgs.v2ray ];
script = ''
exec v2ray -config ${configFile}
'';
};
};
}
36 changes: 36 additions & 0 deletions pkgs/tools/networking/v2ray/default.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{ callPackage, fetchFromGitHub, fetchurl
, assetOverrides ? {}
, ... } @ args:

callPackage ./generic.nix (rec {
version = "4.21.3";

src = fetchFromGitHub {
owner = "v2ray";
repo = "v2ray-core";
rev = "v${version}";
sha256 = "0z45nrjnalrvpprq7g4zrjbrdkc3d3lhs4ci8hb8m69f92asiwbs";
};

assets = {
# MIT licensed
"geoip.dat" = let
geoipRev = "20190516.1";
geoipSha256 = "14h4rq7rlcl1397pwpylfgwpk3fiscpzqb04c4wd5lxkfvk5f02r";
in fetchurl {
url = "https://github.com/v2ray/geoip/releases/download/${geoipRev}/geoip.dat";
sha256 = geoipSha256;
};

# MIT licensed
"geosite.dat" = let
geositeRev = "20191121.1";
geositeSha256 = "0ijmvy43pvm69w38djf114j8swni7wfq5ry9wdpv9dj0rzb59m74";
in fetchurl {
url = "https://github.com/v2ray/domain-list-community/releases/download/${geositeRev}/dlc.dat";
sha256 = geositeSha256;
};

} // assetOverrides;

} // args)
50 changes: 50 additions & 0 deletions pkgs/tools/networking/v2ray/generic.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
{ lib, linkFarm, buildGoModule, runCommand, makeWrapper

# Version specific args
, version, src, assets
, ... }:

let
assetsDrv = linkFarm "v2ray-assets" (lib.mapAttrsToList (name: path: {
inherit name path;
}) assets);

core = buildGoModule rec {
pname = "v2ray-core";
inherit version src;

modSha256 = "11gsncy3449a7y6w6pr7acqabyj2q2a1q52f8fcl5cdz1vjbmmxi";

buildPhase = ''
runHook preBuild

go build -o v2ray v2ray.com/core/main
go build -o v2ctl v2ray.com/core/infra/control/main

runHook postBuild
'';

installPhase = ''
install -Dm755 v2ray v2ctl -t $out/bin
'';
};

in runCommand "v2ray-${version}" {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like this could be put into a postFixup phase.

@kalbasit for his go knowledge

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I make this separated to avoid rebuilding the core when assets updated. Assets are data files (like builtin plugins) and I think they should more extensible.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that's fair... just seems like a lot going on within a single file. But I don't feel super strongly either way.

inherit version;

buildInputs = [ assetsDrv core ];
nativeBuildInputs = [ makeWrapper ];

meta = {
homepage = "https://www.v2ray.com/en/index.html";
description = "A platform for building proxies to bypass network restrictions";
license = with lib.licenses; [ mit ];
maintainers = with lib.maintainers; [ servalcatty ];
};

} ''
for file in ${core}/bin/*; do
makeWrapper "$file" "$out/bin/$(basename "$file")" \
--set-default V2RAY_LOCATION_ASSET ${assetsDrv}
done
''
53 changes: 53 additions & 0 deletions pkgs/tools/networking/v2ray/update.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#!/usr/bin/env nix-shell
#!nix-shell -i bash -p curl jq
set -eo pipefail

version_nix=$(dirname "$0")/default.nix
deps_nix=$(dirname "$0")/deps.nix

old_core_rev="v$(sed -En 's/.*\bversion = "(.*?)".*/\1/p' "$version_nix")"
old_geoip_rev=$(sed -En 's/.*\bgeoipRev = "(.*?)".*/\1/p' "$version_nix")
old_geosite_rev=$(sed -En 's/.*\bgeositeRev = "(.*?)".*/\1/p' "$version_nix")
echo "Current version:" >&2
echo "core: $old_core_rev, geoip: $old_geoip_rev, geosite: $old_geosite_rev" >&2

function fetch_latest_rev {
curl "https://api.github.com/repos/v2ray/$1/releases" |
jq '.[0].tag_name' --raw-output
}

core_rev=$(fetch_latest_rev 'v2ray-core')
geoip_rev=$(fetch_latest_rev 'geoip')
geosite_rev=$(fetch_latest_rev 'domain-list-community')
echo "Latest version:" >&2
echo "core: $core_rev, geoip: $geoip_rev, geosite: $geosite_rev" >&2

if [[ $core_rev != $old_core_rev ]]; then
echo "Prefetching core..." >&2
{ read hash; read store_path; } < <(
nix-prefetch-url --unpack --print-path "https://github.com/v2ray/v2ray-core/archive/$core_rev.zip"
)

sed --in-place \
-e "s/\bversion = \".*\"/version = \"$(echo "$core_rev" | tail -c+2)\"/" \
-e "s/\bsha256 = \".*\"/sha256 = \"$hash\"/" \
"$version_nix"
fi

if [[ $geoip_rev != $old_geoip_rev ]]; then
echo "Prefetching geoip..." >&2
hash=$(nix-prefetch-url "https://github.com/v2ray/geoip/releases/download/$geoip_rev/geoip.dat")
sed --in-place \
-e "s/\bgeoipRev = \".*\"/geoipRev = \"$geoip_rev\"/" \
-e "s/\bgeoipSha256 = \".*\"/geoipSha256 = \"$hash\"/" \
"$version_nix"
fi

if [[ $geosite_rev != $old_geosite_rev ]]; then
echo "Prefetching geosite..." >&2
hash=$(nix-prefetch-url "https://github.com/v2ray/domain-list-community/releases/download/$geosite_rev/dlc.dat")
sed --in-place \
-e "s/\bgeositeRev = \".*\"/geositeRev = \"$geosite_rev\"/" \
-e "s/\bgeositeSha256 = \".*\"/geositeSha256 = \"$hash\"/" \
"$version_nix"
fi
2 changes: 2 additions & 0 deletions pkgs/top-level/all-packages.nix
Original file line number Diff line number Diff line change
Expand Up @@ -6905,6 +6905,8 @@ in

uwsgi = callPackage ../servers/uwsgi { };

v2ray = callPackage ../tools/networking/v2ray { };

vacuum = callPackage ../applications/networking/instant-messengers/vacuum {};

vampire = callPackage ../applications/science/logic/vampire {};
Expand Down