fork/nix-minecraft

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

Infinidogeflake: reformat884ba5b

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