Merge pull request #268619 from tweag/lib-descriptions
[NixPkgs.git] / pkgs / games / dwarf-fortress / dfhack / default.nix
blob914ccbb6ab334433c0026f593d7450698b3d9be4
1 { stdenv
2 , buildEnv
3 , lib
4 , fetchFromGitHub
5 , fetchpatch
6 , cmake
7 , writeScriptBin
8 , perl
9 , XMLLibXML
10 , XMLLibXSLT
11 , zlib
12 , ruby
13 , enableStoneSense ? false
14 , allegro5
15 , libGLU
16 , libGL
17 , SDL
18 , dfVersion
21 with lib;
23 let
24   dfhack-releases = {
25     "0.44.10" = {
26       dfHackRelease = "0.44.10-r2";
27       sha256 = "19bxsghxzw3bilhr8sm4axz7p7z8lrvbdsd1vdjf5zbg04rs866i";
28       xmlRev = "321bd48b10c4c3f694cc801a7dee6be392c09b7b";
29       prerelease = false;
30     };
31     "0.44.11" = {
32       dfHackRelease = "0.44.11-beta2.1";
33       sha256 = "1jgwcqg9m1ybv3szgnklp6zfpiw5mswla464dlj2gfi5v82zqbv2";
34       xmlRev = "f27ebae6aa8fb12c46217adec5a812cd49a905c8";
35       prerelease = true;
36     };
37     "0.44.12" = {
38       dfHackRelease = "0.44.12-r1";
39       sha256 = "0j03lq6j6w378z6cvm7jspxc7hhrqm8jaszlq0mzfvap0k13fgyy";
40       xmlRev = "23500e4e9bd1885365d0a2ef1746c321c1dd5094";
41       prerelease = false;
42     };
43     "0.47.02" = {
44       dfHackRelease = "0.47.02-alpha0";
45       sha256 = "19lgykgqm0si9vd9hx4zw8b5m9188gg8r1a6h25np2m2ziqwbjj9";
46       xmlRev = "23500e4e9bd1885365d0a2ef1746c321c1dd509a";
47       prerelease = true;
48     };
49     "0.47.04" = {
50       dfHackRelease = "0.47.04-r5";
51       sha256 = "sha256-0s+/LKbqsS/mrxKPDeniqykE5+Gy3ZzCa8yEDzMyssY=";
52       xmlRev = "be0444cc165a1abff053d5893dc1f780f06526b7";
53       prerelease = false;
54     };
55     "0.47.05" = {
56       dfHackRelease = "0.47.05-r7";
57       sha256 = "sha256-vBKUTSjfCnalkBzfjaIKcxUuqsGGOTtoJC1RHJIDlNc=";
58       xmlRev = "f5019a5c6f19ef05a28bd974c3e8668b78e6e2a4";
59       prerelease = false;
60     };
62   };
64   release =
65     if lib.isAttrs dfVersion
66     then dfVersion
67     else if hasAttr dfVersion dfhack-releases
68     then getAttr dfVersion dfhack-releases
69     else throw "[DFHack] Unsupported Dwarf Fortress version: ${dfVersion}";
71   version = release.dfHackRelease;
73   # revision of library/xml submodule
74   xmlRev = release.xmlRev;
76   arch =
77     if stdenv.hostPlatform.system == "x86_64-linux" then "64"
78     else if stdenv.hostPlatform.system == "i686-linux" then "32"
79     else throw "Unsupported architecture";
81   fakegit = writeScriptBin "git" ''
82     #! ${stdenv.shell}
83     if [ "$*" = "describe --tags --long" ]; then
84       echo "${version}-unknown"
85     elif [ "$*" = "describe --tags --abbrev=8 --long" ]; then
86       echo "${version}-unknown"
87     elif [ "$*" = "describe --tags --abbrev=8 --exact-match" ]; then
88       echo "${version}"
89     elif [ "$*" = "rev-parse HEAD" ]; then
90       if [ "$(dirname "$(pwd)")" = "xml" ]; then
91         echo "${xmlRev}"
92       else
93         echo "refs/tags/${version}"
94       fi
95     elif [ "$*" = "rev-parse HEAD:library/xml" ]; then
96       echo "${xmlRev}"
97     else
98       exit 1
99     fi
100   '';
102   stdenv.mkDerivation {
103     pname = "dfhack";
104     inherit version;
106     # Beware of submodules
107     src = fetchFromGitHub {
108       owner = "DFHack";
109       repo = "dfhack";
110       rev = release.dfHackRelease;
111       sha256 = release.sha256;
112       fetchSubmodules = true;
113     };
115     patches = lib.optional (lib.versionOlder version "0.44.12-r3") (fetchpatch {
116       name = "fix-stonesense.patch";
117       url = "https://github.com/DFHack/stonesense/commit/f5be6fe5fb192f01ae4551ed9217e97fd7f6a0ae.patch";
118       extraPrefix = "plugins/stonesense/";
119       stripLen = 1;
120       hash = "sha256-wje6Mkct29eyMOcJnbdefwBOLJko/s4JcJe52ojuW+8=";
121     }) ++ lib.optional (lib.versionOlder version "0.47.04-r1") (fetchpatch {
122       name = "fix-protobuf.patch";
123       url = "https://github.com/DFHack/dfhack/commit/7bdf958518d2892ee89a7173224a069c4a2190d8.patch";
124       hash = "sha256-p+mKhmYbnhWKNiGPMjbYO505Gcg634n0nudqH0NX3KY=";
125     });
127     # gcc 11 fix
128     CXXFLAGS = lib.optionalString (lib.versionOlder version "0.47.05-r3") "-fpermissive";
130     # As of
131     # https://github.com/DFHack/dfhack/commit/56e43a0dde023c5a4595a22b29d800153b31e3c4,
132     # dfhack gets its goodies from the directory above the Dwarf_Fortress
133     # executable, which leads to stock Dwarf Fortress and not the built
134     # environment where all the dfhack resources are symlinked to (typically
135     # ~/.local/share/df_linux). This causes errors like `tweak is not a
136     # recognized command` to be reported and dfhack to lose some of its
137     # functionality.
138     postPatch = ''
139       sed -i 's@cached_path = path_string.*@cached_path = getenv("DF_DIR");@' library/Process-linux.cpp
140     '';
142     nativeBuildInputs = [ cmake perl XMLLibXML XMLLibXSLT fakegit ];
143     # We don't use system libraries because dfhack needs old C++ ABI.
144     buildInputs = [ zlib SDL ]
145       ++ lib.optionals enableStoneSense [ allegro5 libGLU libGL ];
147     preConfigure = ''
148       # Trick build system into believing we have .git
149       mkdir -p .git/modules/library/xml
150       touch .git/index .git/modules/library/xml/index
151     '';
153     cmakeFlags = [ "-DDFHACK_BUILD_ARCH=${arch}" "-DDOWNLOAD_RUBY=OFF" ]
154       ++ lib.optionals enableStoneSense [ "-DBUILD_STONESENSE=ON" "-DSTONESENSE_INTERNAL_SO=OFF" ];
156     # dfhack expects an unversioned libruby.so to be present in the hack
157     # subdirectory for ruby plugins to function.
158     postInstall = ''
159       ln -s ${ruby}/lib/libruby-*.so $out/hack/libruby.so
160     '';
162     passthru = { inherit dfVersion; };
164     meta = with lib; {
165       description = "Memory hacking library for Dwarf Fortress and a set of tools that use it";
166       homepage = "https://github.com/DFHack/dfhack/";
167       license = licenses.zlib;
168       platforms = [ "x86_64-linux" "i686-linux" ];
169       maintainers = with maintainers; [ robbinch a1russell abbradar numinit ncfavier ];
170     };
171   }