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