fork/nix-minecraft

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

Gabriel Fontesadd fetchPackwizModpack (#35)7adce0d

develop
3.4 KiB118 linesraw
1{ lib, stdenvNoCC, fetchurl, jre_headless, jq, moreutils, curl, cacert }:
2
3let
4  fetchPackwizModpack =
5    { pname ? "packwiz-pack"
6    , version ? ""
7    , url
8    , packHash ? ""
9      # Either 'server' or 'both' (to get client mods as well)
10    , side ? "server"
11
12      # The derivation passes through a 'manifest' expression, that includes
13      # useful metadata (such as MC version).
14      # By default, if you access it, IFD will be used. If you want to use
15      # 'manifest' without IFD, you can alternatively pass a manifestHash, that
16      # allows us to fetch it with builtins.fetchurl instead.
17    , manifestHash ? null
18    , ...
19    }@args:
20
21    stdenvNoCC.mkDerivation (finalAttrs: {
22      inherit pname version;
23
24      packwizInstaller = fetchurl rec {
25        pname = "packwiz-installer";
26        version = "0.5.8";
27        url = "https://github.com/packwiz/${pname}/releases/download/v${version}/${pname}.jar";
28        hash = "sha256-+sFi4ODZoMQGsZ8xOGZRir3a0oQWXjmRTGlzcXO/gPc=";
29      };
30
31      packwizInstallerBootstrap = fetchurl rec {
32        pname = "packwiz-installer-bootstrap";
33        version = "0.0.3";
34        url = "https://github.com/packwiz/${pname}/releases/download/v${version}/${pname}.jar";
35        hash = "sha256-qPuyTcYEJ46X9GiOgtPZGjGLmO/AjV2/y8vKtkQ9EWw=";
36      };
37
38      dontUnpack = true;
39
40      buildInputs = [ jre_headless jq moreutils curl cacert ];
41
42      buildPhase = ''
43        curl -L "${url}" > pack.toml
44        java -jar "$packwizInstallerBootstrap" \
45          --bootstrap-main-jar "$packwizInstaller" \
46          --bootstrap-no-update \
47          --no-gui \
48          --side "${side}" \
49          "${url}"
50      '';
51
52      installPhase = ''
53        runHook preInstall
54
55        # Fix non-determinism
56        rm env-vars -r
57        jq -Sc '.' packwiz.json | sponge packwiz.json
58
59        mkdir -p $out
60        cp * -r $out/
61
62        runHook postInstall
63      '';
64
65      passthru =
66        let
67          drv = fetchPackwizModpack args;
68        in
69        {
70          # Pack manifest as a nix expression
71          # If manifestHash is not null, then we can do this without IFD.
72          # Otherwise, fallback to IFD.
73          manifest = lib.importTOML (
74            if manifestHash != null then
75              builtins.fetchurl
76                {
77                  inherit url;
78                  sha256 = manifestHash;
79                }
80            else
81              "${drv}/pack.toml"
82          );
83
84          # Adds an attribute set of files to the derivation.
85          # Useful to add server-specific mods not part of the pack.
86          addFiles = files:
87            stdenvNoCC.mkDerivation {
88              inherit (drv) pname version;
89              src = null;
90              dontUnpack = true;
91              dontConfig = true;
92              dontBuild = true;
93              dontFixup = true;
94
95              installPhase = ''
96                cp -as "${drv}" $out
97                chmod u+w -R $out
98              '' + lib.concatLines (lib.mapAttrsToList
99                (name: file: ''
100                  mkdir -p "$out/$(dirname "${name}")"
101                  cp -as "${file}" "$out/${name}"
102                '')
103                files
104              );
105
106              passthru = { inherit (drv) manifest; };
107              meta = drv.meta or { };
108            };
109        };
110
111      dontFixup = true;
112
113      outputHashMode = "recursive";
114      outputHashAlgo = "sha256";
115      outputHash = packHash;
116    } // args);
117in
118fetchPackwizModpack