fork/nix-minecraft

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

yemoutools/fetchModrinthModpack: update to work with more .mrpack files (#206)1beacd3

lazymc
5.9 KiB203 linesraw
1{
2  lib,
3  stdenvNoCC,
4  jq,
5  moreutils,
6  curl,
7  cacert,
8  unzip,
9  coreutils,
10}:
11
12let
13  fetchModrinthModpack =
14    {
15      # Provide a path to the modpack source directory (containing index.json),
16      # a local .mrpack archive, or a URL to a .mrpack archive.
17      src ? null,
18      url ? null,
19      packHash ? "",
20      # Either 'server', 'client' or 'both' (to get all files)
21      side ? "server",
22      ...
23    }@args:
24    let
25      srcNull = src == null;
26      urlNull = url == null;
27      srcPath = if srcNull then "" else src;
28      urlPath = if urlNull then "" else url;
29      drv = fetchModrinthModpack args;
30    in
31
32    assert lib.assertMsg (
33      srcNull != urlNull # equivalent of (src != null) xor (url != null)
34    ) "Either 'src' or 'url' must be provided to fetchModrinthModpack";
35
36    assert lib.assertMsg (builtins.elem side [
37      "server"
38      "client"
39      "both"
40    ]) "'side' must be one of: server, client, both";
41
42    stdenvNoCC.mkDerivation (
43      {
44        pname = args.pname or "modrinth-pack";
45        version = args.version or "";
46
47        dontUnpack = true;
48
49        buildInputs = [
50          jq
51          moreutils
52          curl
53          cacert
54          unzip
55          coreutils
56        ];
57
58        buildPhase = ''
59          set -euo pipefail
60          runHook preBuild
61
62          mkdir -p pack-src
63
64          if [ "${if !urlNull then "1" else "0"}" = "1" ]; then
65            curl -L "${urlPath}" > modpack.mrpack
66            unzip -q modpack.mrpack -d pack-src
67          elif [ -d "${srcPath}" ]; then
68            cp -r "${srcPath}"/. pack-src/
69          else
70            unzip -q "${srcPath}" -d pack-src
71          fi
72
73          # Current mrpack files use modrinth.index.json but keep index.json for older mrpacks
74          index_json_path=""
75          if [ -f "pack-src/modrinth.index.json" ]; then
76            index_json_path="pack-src/modrinth.index.json"
77          elif [ -f "pack-src/index.json" ]; then
78            index_json_path="pack-src/index.json"
79          else
80            echo "Couldn't find either 'modrinth.index.json' or 'index.json'" >&2
81            exit 1
82          fi
83
84          # Make sure the extracted mrpack has permissions
85          if [ "$(stat -c '%a' "$index_json_path")" == 0 ]; then
86            find ./pack-src/ -type d -exec chmod 755 {} \;
87            find ./pack-src/ -type f -exec chmod 644 {} \;
88          fi
89
90          while IFS= read -r file; do
91            if [ "${side}" != "both" ]; then
92              envState=$(echo "$file" | jq -r --arg side "${side}" '.env[$side] // "required"')
93              if [ "$envState" = "unsupported" ]; then
94                continue
95              fi
96            fi
97
98            path=$(echo "$file" | jq -r '.path')
99            url=$(echo "$file" | jq -r '.downloads[0]')
100            mkdir -p "$(dirname "$path")"
101            curl -L "$url" > "$path"
102
103            if echo "$file" | jq -e '.hashes.sha512 != null' > /dev/null; then
104              expected=$(echo "$file" | jq -r '.hashes.sha512')
105              actual=$(${coreutils}/bin/sha512sum "$path" | cut -d' ' -f1)
106            elif echo "$file" | jq -e '.hashes.sha1 != null' > /dev/null; then
107              expected=$(echo "$file" | jq -r '.hashes.sha1')
108              actual=$(${coreutils}/bin/sha1sum "$path" | cut -d' ' -f1)
109            else
110              echo "No supported hash for '$path' (supported: sha512, sha1)" >&2
111              exit 1
112            fi
113
114            if [ "$actual" != "$expected" ]; then
115              echo "Hash mismatch for '$path'" >&2
116              echo "expected: $expected" >&2
117              echo "actual:   $actual" >&2
118              exit 1
119            fi
120          done < <(jq -c '.files[]' "$index_json_path")
121
122          if [ -d pack-src/overrides ]; then
123            cp -r pack-src/overrides/. .
124          fi
125
126          if [ "${side}" = "server" ] && [ -d pack-src/server-overrides ]; then
127            cp -r pack-src/server-overrides/. .
128          fi
129
130          if [ "${side}" = "client" ] && [ -d pack-src/client-overrides ]; then
131            cp -r pack-src/client-overrides/. .
132          fi
133
134          if [ "${side}" = "both" ]; then
135            if [ -d pack-src/server-overrides ]; then
136              cp -r pack-src/server-overrides/. .
137            fi
138            if [ -d pack-src/client-overrides ]; then
139              cp -r pack-src/client-overrides/. .
140            fi
141          fi
142
143          # Keep the source manifest in output for passthru consumers.
144          cp "$index_json_path" ./index.json
145
146          runHook postBuild
147        '';
148
149        installPhase = ''
150          runHook preInstall
151
152          # Fix non-determinism
153          rm -rf env-vars
154          jq -Sc '.' index.json | sponge index.json
155
156          mkdir -p "$out"
157          cp * -r "$out/"
158
159          runHook postInstall
160        '';
161
162        passthru = {
163          # Modrinth index manifest as a nix expression.
164          manifest = builtins.fromJSON (builtins.readFile "${drv}/index.json");
165
166          # Adds an attribute set of files to the derivation.
167          # Useful to add server-specific mods not part of the pack.
168          addFiles =
169            files:
170            stdenvNoCC.mkDerivation {
171              inherit (drv) pname version;
172              src = null;
173              dontUnpack = true;
174              dontConfig = true;
175              dontBuild = true;
176              dontFixup = true;
177
178              installPhase = ''
179                cp -as "${drv}" $out
180                chmod u+w -R $out
181              ''
182              + lib.concatLines (
183                lib.mapAttrsToList (name: file: ''
184                  mkdir -p "$out/$(dirname "${name}")"
185                  cp -as "${file}" "$out/${name}"
186                '') files
187              );
188
189              passthru = { inherit (drv) manifest; };
190              meta = drv.meta or { };
191            };
192        };
193
194        dontFixup = true;
195
196        outputHashMode = "recursive";
197        outputHashAlgo = "sha256";
198        outputHash = packHash;
199      }
200      // args
201    );
202in
203fetchModrinthModpack