|
| 1 | +# Minecraft sound asset pack. |
| 2 | +# |
| 3 | +# !!! LICENSING / DO NOT UPLOAD !!! |
| 4 | +# The `.ogg` files this derivation produces are Mojang's proprietary Minecraft |
| 5 | +# assets (governed by the Minecraft EULA). This is a fixed-output derivation |
| 6 | +# that DOWNLOADS them from Mojang's official CDN at build time on the building |
| 7 | +# machine; it does not vendor them in this repo. The resulting store path |
| 8 | +# therefore contains Mojang content and MUST NOT be pushed to any shared or |
| 9 | +# public binary cache (e.g. `indexable-inc.cachix.org`), nor included in any |
| 10 | +# image closure that gets cached or distributed. Each machine that wants |
| 11 | +# sounds builds this path locally from Mojang. Keep it out of CI cache pushes. |
| 12 | +{ |
| 13 | + lib, |
| 14 | + stdenvNoCC, |
| 15 | + fetchurl, |
| 16 | + cacert, |
| 17 | + curl, |
| 18 | + jq, |
| 19 | +}: |
| 20 | +let |
| 21 | + lock = lib.importJSON ./sounds/lock.json; |
| 22 | + |
| 23 | + # The asset index lists every sound object with its content hash. Pinned by |
| 24 | + # sha1 (the hash Mojang publishes for it), so this fetch is reproducible. |
| 25 | + assetIndex = fetchurl { |
| 26 | + inherit (lock.assetIndex) url hash; |
| 27 | + }; |
| 28 | + |
| 29 | + # Sound names whose leading path segment matches are dropped. Defaults to the |
| 30 | + # multi-megabyte background music and music-disc tracks, which a sound-effect |
| 31 | + # player never needs. Widen `excludePrefixes` in lock.json to trim more. |
| 32 | + excludeRegex = "^(" + lib.concatStringsSep "|" lock.excludePrefixes + ")/"; |
| 33 | +in |
| 34 | +stdenvNoCC.mkDerivation { |
| 35 | + pname = "minecraft-sound-assets"; |
| 36 | + version = lock.minecraftVersion; |
| 37 | + |
| 38 | + dontUnpack = true; |
| 39 | + strictDeps = true; |
| 40 | + nativeBuildInputs = [ |
| 41 | + curl |
| 42 | + jq |
| 43 | + ]; |
| 44 | + |
| 45 | + SSL_CERT_FILE = "${cacert}/etc/ssl/certs/ca-bundle.crt"; |
| 46 | + |
| 47 | + # Fixed-output derivation: network access is permitted because the whole |
| 48 | + # assembled tree is content-addressed by `outputHash`, which is the integrity |
| 49 | + # guarantee for every downloaded file together. Refresh it with |
| 50 | + # `nix run .#update-sounds` after bumping the pinned Minecraft version. |
| 51 | + outputHashMode = "recursive"; |
| 52 | + outputHashAlgo = "sha256"; |
| 53 | + outputHash = lock.packHash; |
| 54 | + |
| 55 | + buildPhase = '' |
| 56 | + runHook preBuild |
| 57 | +
|
| 58 | + list="$TMPDIR/sounds.tsv" |
| 59 | + jq -r --arg ex ${lib.escapeShellArg excludeRegex} ' |
| 60 | + .objects |
| 61 | + | to_entries[] |
| 62 | + | select((.key | startswith("minecraft/sounds/")) and (.key | endswith(".ogg"))) |
| 63 | + | (.key | ltrimstr("minecraft/sounds/")) as $name |
| 64 | + | select(($name | test($ex)) | not) |
| 65 | + | "\(.value.hash) \($name)" |
| 66 | + ' ${assetIndex} > "$list" |
| 67 | +
|
| 68 | + echo "downloading $(wc -l < "$list") Minecraft sounds from Mojang's CDN..." |
| 69 | +
|
| 70 | + mkdir -p "$out/sounds" |
| 71 | + cut -d' ' -f2 "$list" | xargs -n1 dirname | sort -u | while read -r dir; do |
| 72 | + mkdir -p "$out/sounds/$dir" |
| 73 | + done |
| 74 | +
|
| 75 | + export out |
| 76 | + xargs -P 16 -n 2 bash -c ' |
| 77 | + hash="$1"; name="$2" |
| 78 | + curl -sSfL --retry 3 \ |
| 79 | + "https://resources.download.minecraft.net/''${hash:0:2}/$hash" \ |
| 80 | + -o "$out/sounds/$name" |
| 81 | + ' _ < "$list" |
| 82 | +
|
| 83 | + runHook postBuild |
| 84 | + ''; |
| 85 | + |
| 86 | + dontInstall = true; |
| 87 | + |
| 88 | + meta = { |
| 89 | + description = "Minecraft sound effects (downloaded from Mojang's CDN at build time)"; |
| 90 | + # License intentionally omitted: these are Mojang's proprietary assets. |
| 91 | + # See the DO NOT UPLOAD banner at the top of this file. |
| 92 | + }; |
| 93 | +} |
0 commit comments