fork/nix-minecraft

git clone https://git.julia.blue/fork/nix-minecraft

Nathan Regnerneoforge-servers: initcc15499

master
3.8 KiB124 linesraw
1{
2  lib,
3  fetchurl,
4  jre_headless,
5  linkFarm,
6  makeWrapper,
7  minecraft-server,
8  runCommand,
9  stdenvNoCC,
10  udev,
11  writeShellApplication,
12  zip,
13
14  build,
15  gameVersion,
16  libraryLocks,
17}:
18let
19  inherit (lib)
20    attrValues
21    concatLists
22    concatStringsSep
23    elemAt
24    map
25    mapAttrs
26    splitString
27    ;
28  specifierPath =
29    specifier:
30    let
31      components = builtins.match "^([^:]+):([^:]+):([^@:]+).*" specifier;
32      groupId = elemAt components 0;
33      artifactId = elemAt components 1;
34      version = elemAt components 2;
35    in
36    concatStringsSep "/" (
37      (splitString "." groupId)
38      ++ [
39        artifactId
40        version
41      ]
42    );
43  mkLibrary = specifier: rec {
44    name = "${specifierPath specifier}/${path.name}";
45    path = fetchurl libraryLocks.${specifier};
46  };
47  repository = linkFarm "neoforge${build.version}-libraries" (
48    (map mkLibrary build.libraries)
49    ++ [
50      {
51        name = "net/minecraft/server/${minecraft-server.version}/server-${minecraft-server.version}.jar";
52        path = minecraft-server.src;
53      }
54    ]
55  );
56  installer-unwrapped = fetchurl build.src;
57  installer =
58    let
59      name = "neoforge-${build.version}-offline-installer";
60
61      # Add server mappings to the jar so we can perform an offline install.
62      # https://github.com/neoforged/LegacyInstaller/blob/e95a1687b24d6c1e4ba87c98ddcd42b83eeba555/src/main/java/net/minecraftforge/installer/actions/FatInstallerAction.java#L79
63      # TODO: Can we just add mappings to the classpath rather than directly to the jar?
64      fatJar = runCommand "${name}" { nativeBuildInputs = [ zip ]; } ''
65        install -m 644 -D "${installer-unwrapped}" "$out"
66
67        server_mappings="maven/minecraft/${minecraft-server.version}/server_mappings.txt"
68        install -m 644 -D ${fetchurl gameVersion.mappings} "$server_mappings"
69
70        zip "$out" "$server_mappings"
71      '';
72
73      wrapper = writeShellApplication {
74        inherit name;
75        runtimeInputs = [ jre_headless ];
76        # Unfortunately, library dependencies cannot just be added to the classpath. The
77        # [installer](https://github.com/neoforged/LegacyInstaller/blob/e95a1687b24d6c1e4ba87c98ddcd42b83eeba555/src/main/java/net/minecraftforge/installer/Downloader.java#L114),
78        # [and NeoForge itself](https://github.com/neoforged/FancyModLoader/blob/610108bbd862a87c1266076a2592dfbc732e19c4/loader/src/main/java/net/neoforged/fml/loading/LibraryFinder.java#L28)
79        # require a `libraries` directory.
80        text = ''
81          mkdir -p "$1/libraries"
82          cp -r --no-preserve=all ${repository}/* "$1/libraries"
83          java -jar ${fatJar} --offline --installServer "$1"
84        '';
85      };
86    in
87    wrapper;
88in
89stdenvNoCC.mkDerivation rec {
90  pname = "neoforge";
91  version = "${gameVersion.version}-${build.version}";
92  dontUnpack = true;
93
94  preferLocalBuild = true;
95
96  buildInputs = [ makeWrapper ];
97
98  buildPhase = ''
99    ${lib.getExe installer} $out
100    args="$out/libraries/net/neoforged/neoforge/${build.version}/unix_args.txt"
101    substituteInPlace "$args" \
102      --replace-fail "-DlibraryDirectory=libraries" "-DlibraryDirectory=$out/libraries" \
103      --replace-fail "libraries/" "$out/libraries/"
104    makeWrapper "${jre_headless}/bin/java" "$out/bin/${meta.mainProgram}" \
105      ${lib.optionalString stdenvNoCC.hostPlatform.isLinux "--prefix LD_LIBRARY_PATH : ${lib.makeLibraryPath [ udev ]}"} \
106      --append-flags "@$args"
107  '';
108
109  passthru = {
110    inherit repository installer installer-unwrapped;
111    updateScript = ./update.py;
112    gameVersion = gameVersion.version;
113    loaderVersion = build.version;
114  };
115
116  meta = with lib; {
117    description = "Minecraft Server";
118    homepage = "https://minecraft.net";
119    license = licenses.unfreeRedistributable;
120    platforms = platforms.unix;
121    maintainers = with maintainers; [ infinidoge ];
122    mainProgram = "minecraft-server";
123  };
124}