nixpkgs mirror (for testing)
github.com/NixOS/nixpkgs
nix
1{
2 lib,
3 stdenv,
4 fetchFromGitHub,
5 gradle,
6 jdk17,
7 makeBinaryWrapper,
8 openssl,
9 libdeflate,
10 jre_headless,
11 writeScript,
12 nixosTests,
13
14 # native (openssl + libdeflate) compression and crypto implementations
15 withVelocityNative ? builtins.elem stdenv.hostPlatform.system [
16 "x86_64-linux"
17 "aarch64-linux"
18 ],
19}:
20let
21 gradle_jdk17 = gradle.override {
22 javaToolchains = [ jdk17 ];
23 };
24 velocityNativePlatform =
25 {
26 x86_64-linux = "linux_x86_64";
27 aarch64-linux = "linux_aarch64";
28 }
29 .${stdenv.hostPlatform.system} or (
30 if withVelocityNative then
31 throw "velocity native is not supported on ${stdenv.hostPlatform.system}"
32 else
33 null
34 );
35in
36stdenv.mkDerivation (finalAttrs: {
37 pname = "velocity";
38 version = "3.4.0-unstable-2025-06-11";
39
40 src = fetchFromGitHub {
41 owner = "PaperMC";
42 repo = "Velocity";
43 rev = "669fda298c670c55686f34d868383052b192518d";
44 hash = "sha256-UI6SqVAaM4NANf9cGvsIgYO1jSkWDOk5ysyufrPWTgg=";
45 };
46
47 nativeBuildInputs = [
48 gradle_jdk17
49 makeBinaryWrapper
50 ];
51
52 buildInputs = lib.optionals withVelocityNative [
53 # libraries for velocity-native
54 openssl
55 libdeflate
56
57 # needed for building velocity-native jni
58 jdk17
59 ];
60
61 strictDeps = true;
62
63 mitmCache = gradle_jdk17.fetchDeps {
64 inherit (finalAttrs) pname;
65 data = ./deps.json;
66 };
67
68 patches = [
69 ./fix-version.patch # remove build-time dependency on git and use version string from a env var instead
70 ./disable-javadocs.patch # disable building java docs because they cause build failures
71 ];
72
73 # tests require velocity native
74 doCheck = withVelocityNative;
75
76 postPatch = ''
77 rm -rf native/src/main/resources/{linux_x86_64,linux_aarch64,macos_aarch64}/*
78 '';
79
80 # based on native/build-support/compile-linux-{compress,crypt}.sh
81 preBuild =
82 let
83 CFLAGS = "-O2 -fPIC -shared -Wl,-z,noexecstack -Wall -Werror -fomit-frame-pointer";
84 in
85 lib.optionalString withVelocityNative ''
86 $CC ${CFLAGS} native/src/main/c/jni_util.c native/src/main/c/jni_cipher_openssl.c \
87 -o native/src/main/resources/${velocityNativePlatform}/velocity-cipher-ossl30x.so \
88 -lcrypto
89
90 $CC ${CFLAGS} native/src/main/c/jni_util.c native/src/main/c/jni_zlib_deflate.c native/src/main/c/jni_zlib_inflate.c \
91 -o native/src/main/resources/${velocityNativePlatform}/velocity-compress.so \
92 -ldeflate
93 '';
94
95 installPhase = ''
96 runHook preInstall
97
98 mkdir -p $out/bin $out/share/velocity
99 cp proxy/build/libs/velocity-proxy-${builtins.head (builtins.split "-" finalAttrs.version)}-SNAPSHOT-all.jar $out/share/velocity/velocity.jar
100
101 makeWrapper ${lib.getExe jre_headless} "$out/bin/velocity" \
102 --append-flags "-jar $out/share/velocity/velocity.jar"
103
104 ${lib.optionalString withVelocityNative ''
105 # Nix doesn't pick up references in compressed JAR file
106 mkdir $out/nix-support
107 echo ${lib.getLib openssl} >> $out/nix-support/runtime-dependencies
108 echo ${lib.getLib libdeflate} >> $out/nix-support/runtime-dependencies
109 ''}
110
111 runHook postInstall
112 '';
113
114 env.BUILD_VERSION = "nixpkgs-${finalAttrs.version}";
115
116 passthru = {
117 updateScript = writeScript "update-velocity" ''
118 #!/usr/bin/env nix-shell
119 #!nix-shell -i bash -p common-updater-scripts
120
121 tmpdir="$(mktemp -d)"
122 git clone --depth=1 "${finalAttrs.src.gitRepoUrl}" "$tmpdir"
123
124 pushd "$tmpdir"
125
126 main_version=$(awk 'match($0,/version=([0-9.]+)/,r) { print r[1] }' gradle.properties)
127 commit_date=$(git show -s --pretty='format:%cs')
128 commit_hash=$(git show -s --pretty='format:%H')
129
130 popd
131 rm -rf "$tmpdir"
132
133 update-source-version "$UPDATE_NIX_ATTR_PATH" "$main_version-unstable-$commit_date" --rev="$commit_hash"
134 $(nix-build -A velocity.mitmCache.updateScript)
135 '';
136 tests.velocity = nixosTests.velocity;
137 };
138
139 meta = {
140 description = "Modern, next-generation Minecraft server proxy";
141 homepage = "https://papermc.io/software/velocity";
142 license = with lib.licenses; [
143 gpl3Only
144 mit
145 ];
146 sourceProvenance = with lib.sourceTypes; [
147 fromSource
148 binaryBytecode # java deps
149 ];
150 maintainers = with lib.maintainers; [ Tert0 ];
151 platforms = lib.platforms.linux;
152 mainProgram = "velocity";
153 };
154})