├── .gitignore ├── LICENSE ├── README.md ├── flake.nix └── lib └── default.nix /.gitignore: -------------------------------------------------------------------------------- 1 | # nix build artifacts 2 | result 3 | 4 | # cached devShells 5 | .direnv 6 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2022 seth 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # packwiz2nix 2 | 3 | packwiz2nix brings all of the benefits of Nix to [packwiz](https://packwiz.infra.link/), helping you create 4 | [fixed-output derivations](https://nixos.org/manual/nix/stable/language/advanced-attributes.html#adv-attr-outputHash) from 5 | your already existing packwiz modpack! 6 | 7 | ## Getting started 8 | 9 | ### For users 10 | 11 | It is recommended to use [Infinidoge's](https://github.com/Infinidoge) [nix-minecraft](https://github.com/Infinidoge/nix-minecraft) 12 | module as it allows you to symlink packages into a minecraft server's directory. 13 | 14 | There is a convenience function called `mkModLinks` that can automate the creation of symlinks for a server like so: 15 | 16 | ```nix 17 | { 18 | nix-minecraft, 19 | packwiz2nix, 20 | pkgs, 21 | yourModpack, 22 | ... 23 | }: let 24 | inherit (packwiz2nix.lib) mkPackwizPackages mkModLinks; 25 | # replace "/checksums.json" with the path to the modpack's checksums file 26 | mods = mkPackwizPackages pkgs (yourModpack + "/checksums.json"); 27 | in { 28 | imports = [ 29 | nix-minecraft.nixosModules.minecraft-servers 30 | ]; 31 | 32 | nixpkgs.overlays = [nix-minecraft.overlay]; 33 | 34 | services.minecraft-servers = { 35 | enable = true; 36 | eula = true; 37 | 38 | servers.my_server = { 39 | enable = true; 40 | package = pkgs.quiltServers.quilt-1_19_4-0_18_10; 41 | symlinks = mkModLinks mods; 42 | }; 43 | }; 44 | } 45 | ``` 46 | 47 | ### For modpack developers 48 | 49 | packwiz2nix is quick to set up, all you have to do is add this to the `apps` attribute of a flake: 50 | 51 | ```nix 52 | { 53 | inputs = { 54 | nixpkgs.url = "nixpkgs/nixos-unstable"; 55 | flake-utils.url = "github:numtide/flake-utils"; 56 | packwiz2nix.url = "github:getchoo/packwiz2nix"; 57 | }; 58 | 59 | outputs = { 60 | nixpkgs, 61 | flake-utils, 62 | packwiz2nix, 63 | ... 64 | }: 65 | flake-utils.lib.eachDefaultSystem (system: let 66 | pkgs = import nixpkgs {inherit system;}; 67 | in { 68 | apps = { 69 | # replace ./mods with the path to your .pw.toml files 70 | generate-checksums = packwiz2nix.lib.mkChecksumsApp pkgs ./mods; 71 | }; 72 | }); 73 | } 74 | ``` 75 | 76 | An example of this can be found in my [personal modpack](https://github.com/getchoo/modpack/blob/main/flake.nix) 77 | 78 | ## Maintainer Guide 79 | 80 | packwiz2nix also offers some more advanced features if maintainers wish to integrate 81 | this project further into their distribution. 82 | 83 | ### Create traditional modpacks 84 | 85 | One of these features is the ability to create traditional modpacks anyone can use. So far, 86 | only modpacks for MultiMC and Prism Launcher are supported. 87 | 88 | #### MultiMC/Prism Launcher 89 | 90 | `mkMultiMCPack` allows you to create a package attribute that will result in a zipfile any user of 91 | Prism Launcher and MultiMC can import. For example: 92 | 93 | ```nix 94 | { 95 | packages = let 96 | inherit (packwiz2nix.lib) mkMultiMCPack mkPackwizPackages; 97 | mods = mkPackwizPackages pkgs ./checksums.json; 98 | in rec { 99 | myModpack = mkMultiMCPack { 100 | inherit pkgs mods; 101 | # optionally include externa. files in modpack. 102 | # instance related files should be placed here, 103 | # as made in step 1 and 3 of this guide: 104 | # https://packwiz.infra.link/tutorials/installing/packwiz-installer/#creating-a-multimc-instance-for-your-modpack 105 | filesDir = ./files; 106 | name = "myModpack"; 107 | }; 108 | 109 | default = myModpack; 110 | }; 111 | } 112 | ``` 113 | 114 | ## Gotchas! 115 | 116 | There are two main things you should keep in mind with this project currently: 117 | 118 | - No Curseforge support 119 | 120 | - Packwiz does not keep the download URL for Curseforge mods in it's TOML files 121 | due to Curseforge's ToS, which is not acceptable in the current method used to 122 | generate checksums and create the final derivations for mods. 123 | 124 | - This is the biggest concern as it affects end users the most 125 | 126 | - Checksums must be generated (**modpack developers, make sure you read this**) 127 | - Packwiz uses SHA1 by default to verify mod files, which fetchers in nix such as `builtins.fetchurl` 128 | and `pkgs.fetchurl` do not support. This prevents us from using them, and requires a separate 129 | checksum file (using SHA256) to be generated and updated along with the modpack. 130 | - There are [plans](https://github.com/packwiz/packwiz/issues/156) to change this in the future, 131 | which would allow for this process to be removed 132 | 133 | ## Related Projects 134 | 135 | - [Infinidoge/nix-minecraft](https://github.com/Infinidoge/nix-minecraft) 136 | - [nix-community/mineflake](https://github.com/nix-community/mineflake) 137 | -------------------------------------------------------------------------------- /flake.nix: -------------------------------------------------------------------------------- 1 | { 2 | description = "a tool for generating packages from packwiz modpacks"; 3 | 4 | outputs = _: {lib = import ./lib;}; 5 | } 6 | -------------------------------------------------------------------------------- /lib/default.nix: -------------------------------------------------------------------------------- 1 | let 2 | inherit 3 | (builtins) 4 | attrNames 5 | concatStringsSep 6 | hashFile 7 | fetchurl 8 | fromJSON 9 | listToAttrs 10 | mapAttrs 11 | readDir 12 | readFile 13 | replaceStrings 14 | toFile 15 | toJSON 16 | ; 17 | 18 | # loads data from a toml json given 19 | # the directory (dir) and filename (name) 20 | # string -> string -> attrset 21 | fromMod = dir: name: fromTOML (readFile "${dir}/${name}"); 22 | 23 | # replaces `.pw.toml` extensions with `.jar` 24 | # to correct the store paths of jarfiles 25 | # string -> string 26 | fixupName = replaceStrings [".pw.toml"] [".jar"]; 27 | 28 | # *pkgs*.fetchurl wrapper that downloads a 29 | # jarfile mod. pkgs.fetchurl is used over builtins 30 | # here since we have a checksum and can take advantage 31 | # of fixed output derivations 32 | # attrset -> string -> attrset -> store path 33 | mkMod = pkgs: name: mod: 34 | pkgs.fetchurl { 35 | name = fixupName name; 36 | inherit (mod) url sha256; 37 | }; 38 | 39 | # maps each mod in our checksums.json format 40 | # to the store path of a fixed output derivation 41 | # attrset -> attrset 42 | genMods = pkgs: 43 | mapAttrs (mkMod pkgs); 44 | 45 | # this is probably what you're looking for if 46 | # you're a developer trying to use this in your modpack. 47 | # this is where you create a checksums file for end users 48 | # to put into mkPackwizPackages, so make sure you keep it up to 49 | # date! 50 | # 51 | # `dir` is a path to the folder containing your .pw.toml files 52 | # files for mods. make sure they are the only files in the folder 53 | # 54 | # path -> file 55 | mkChecksums = dir: let 56 | mods = readDir dir; 57 | 58 | getChecksum = name: url: 59 | hashFile "sha256" (fetchurl { 60 | name = fixupName name; 61 | inherit url; 62 | }); 63 | 64 | toWrite = 65 | toJSON 66 | (mapAttrs (mod: _: let 67 | data = fromMod dir mod; 68 | in { 69 | inherit (data.download) url; 70 | sha256 = getChecksum mod data.download.url; 71 | }) 72 | mods); 73 | in 74 | toFile "checksums-json" toWrite; 75 | 76 | # this is probably what you're looking for if 77 | # you're an end user trying to implement a modpack in 78 | # your module. 79 | # 80 | # `pkgs` is an instance of nixpkgs for your system, 81 | # must at least contain `fetchurl`. 82 | # 83 | # `checksums` is a json file from an upstream modpack 84 | # containing the names, urls, and sha256sums of mods 85 | # 86 | # attrset -> path -> attrset 87 | mkPackwizPackages = pkgs: checksums: genMods pkgs (fromJSON (readFile checksums)); 88 | in { 89 | inherit mkChecksums mkPackwizPackages; 90 | 91 | # this another function modpack developers will be looking 92 | # for. it allows you to build a modpack compatible with MultiMC 93 | # and Prism Launcher, containing external files. 94 | # 95 | # `pkgs` is an instance of nixpkgs with at least lib, runCommand, 96 | # and zip 97 | # 98 | # `mods` is the result of mkPackwizPackages 99 | # 100 | # `filesDir` is the root path of all external files you want 101 | # included in your zipped modpack 102 | # 103 | # `name` is a string containing the name of your modpack 104 | mkMultiMCPack = { 105 | pkgs, 106 | mods, 107 | filesDir ? "", 108 | name, 109 | }: let 110 | inherit (pkgs) fetchurl lib runCommand zip; 111 | inherit (lib) forEach; 112 | 113 | packwiz-installer-bootstrap = fetchurl { 114 | url = "https://github.com/packwiz/packwiz-installer-bootstrap/releases/download/v0.0.3/packwiz-installer-bootstrap.jar"; 115 | sha256 = "sha256-qPuyTcYEJ46X9GiOgtPZGjGLmO/AjV2/y8vKtkQ9EWw="; 116 | }; 117 | 118 | forNames = attrset: forEach (attrNames attrset); 119 | commandSep = concatStringsSep "; "; 120 | modFiles = let 121 | commands = forNames mods (mod: "cp -r ${mods.${mod}} $out/decompressed/.minecraft/mods/"); 122 | in 123 | commandSep commands; 124 | 125 | regularFiles = 126 | if filesDir != "" 127 | then 128 | (let 129 | files = readDir filesDir; 130 | commands = forNames files (file: "cp -r ${filesDir}/${file} $out/decompressed/"); 131 | in 132 | commandSep commands) 133 | else "echo 'not copying external files...'"; 134 | in 135 | runCommand name {} '' 136 | mkdir -p $out/decompressed/.minecraft/mods 137 | ${regularFiles} 138 | ${modFiles} 139 | cp ${packwiz-installer-bootstrap} $out/decompressed/.minecraft/packwiz-installer-bootstrap.jar 140 | 141 | cd $out/decompressed 142 | ${zip}/bin/zip -r ../${name}.zip {*,.*} 143 | ''; 144 | 145 | # this creates an `apps` attribute for a flake 146 | # which runs a bash script to generate a checksums 147 | # file 148 | # 149 | # `pkgs` is an instance of nixpkgs for your system, 150 | # must at least contain `writeShellScriptBin` 151 | # 152 | # `dir` is a path to the folder containing your .pw.toml files 153 | # files for mods. make sure they are the only files in the folder 154 | # 155 | # attrset -> path -> attrset 156 | mkChecksumsApp = pkgs: dir: let 157 | inherit (pkgs) writeShellScriptBin; 158 | checksums = mkChecksums dir; 159 | name = "generate-checksums"; 160 | script = writeShellScriptBin name '' 161 | cat ${checksums} > checksums.json 162 | ''; 163 | in { 164 | type = "app"; 165 | program = script.outPath + "/bin/${name}"; 166 | }; 167 | 168 | # this creates an attrset value for 169 | # minecraft-servers.servers..symlinks 170 | # attrset -> attrset 171 | mkModLinks = mods: let 172 | fixup = map (name: { 173 | name = "mods/" + fixupName name; 174 | value = mods.${name}; 175 | }) (attrNames mods); 176 | in 177 | listToAttrs fixup; 178 | } 179 | --------------------------------------------------------------------------------