Create nix-modpack

This commit is contained in:
Jan-Bulthuis 2025-05-18 06:28:46 +02:00
parent 71ba1088f2
commit c1651cdff0
4 changed files with 138 additions and 0 deletions

View File

@ -1,2 +1,27 @@
# nix-modpack
Create a modpack server or MultiMC client from a packwiz toml.
## Creating a server
A server derivation can be created as follows:
```nix
nix-modpack.packages.${system}.mkServer {
packUrl = "<url to pack.toml>";
server = nix-minecraft.legacyPackages.${system}.${server}
}
```
When this is added as a package to a flake, it can be started with `nix run`.
## Creating a client
A client derivation can be created as follows:
```nix
nix-modpack.packages.${system}.mkClient {
packUrl = "<url to pack.toml>";
gameVersion = "<minecraft version>";
loaderUid = "<loader identifier>";
loaderVersion = "<loader version>";
}
```
To use it you need to `nix build` it before importing the resulting `modpack.zip` into prism launcher.
The `loaderUid` depends on the modloader, for NeoForge it is `net.neoforged`. It can be found experimentally by exporting an empty instance with the loader installed from prism launcher and inspecting the mmc-pack.json inside of it.

30
flake.nix Normal file
View File

@ -0,0 +1,30 @@
{
description = "Create nix derivations from a packwiz modpack";
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
};
outputs =
inputs:
let
lib = inputs.nixpkgs.lib;
eachSystem =
fn: lib.genAttrs lib.systems.flakeExposed (system: fn (import inputs.nixpkgs { inherit system; }));
in
{
packages = eachSystem (
pkgs:
let
bootstrap = pkgs.fetchurl {
url = "https://github.com/packwiz/packwiz-installer-bootstrap/releases/download/v0.0.3/packwiz-installer-bootstrap.jar";
sha256 = "qPuyTcYEJ46X9GiOgtPZGjGLmO/AjV2/y8vKtkQ9EWw=";
};
in
{
mkServer = pkgs.callPackage ./packages/mkServer.nix { inherit bootstrap; };
mkClient = pkgs.callPackage ./packages/mkClient.nix { inherit bootstrap; };
}
);
};
}

66
packages/mkClient.nix Normal file
View File

@ -0,0 +1,66 @@
{
pkgs,
bootstrap,
...
}:
{
packUrl,
gameVersion,
loaderUid,
loaderVersion,
}:
pkgs.stdenvNoCC.mkDerivation {
name = "minecraft-client";
dontUnpack = true;
mmcpack = ''
{
"components": [
{
"important": true,
"uid": "net.minecraft",
"version": "${gameVersion}"
},
{
"uid": "${loaderUid}",
"version": "${loaderVersion}"
}
],
"formatVersion": 1
}
'';
instancecfg = pkgs.writeText "instance.cfg" ''
[General]
ConfigVersion=1.2
InstanceType=OneSix
OverrideCommands=true
PreLaunchCommand=\"$INST_JAVA\" -jar packwiz-installer-bootstrap.jar ${packUrl}
'';
buildPhase = ''
# Remove env-vars
rm env-vars
# Add the mmc-pack
echo $mmcpack > ./mmc-pack.json
# Add the instance-cfg
cp $instancecfg ./instance.cfg
# Add the bootstrap jar
mkdir ./minecraft
cp ${bootstrap} ./minecraft/packwiz-installer-bootstrap.jar
# Zip everything together
${pkgs.zip}/bin/zip -r modpack.zip ./*
'';
installPhase = ''
mkdir $out
mv modpack.zip $out/
'';
}

17
packages/mkServer.nix Normal file
View File

@ -0,0 +1,17 @@
{
lib,
pkgs,
bootstrap,
...
}:
{
packUrl,
server,
jre ? pkgs.jre,
}:
pkgs.writeShellScriptBin "minecraft-server" ''
${lib.getExe jre} -jar ${bootstrap} -g -s server ${packUrl}
${server}/bin/${server.name}
''