1{ stdenv
2, callPackage
3, fetchFromGitHub
4, fetchurl
5, fetchpatch
6, lib
7, substituteAll
8 # Dependencies
9, boehmgc
10, coreutils
11, git
12, gmp
13, hostname
14, libatomic_ops
15, libevent
16, libiconv
17, libxml2
18, libyaml
19, libffi
20, llvmPackages_13
21, llvmPackages_15
22, makeWrapper
23, openssl
24, pcre2
25, pcre
26, pkg-config
27, readline
28, tzdata
29, which
30, zlib
31}:
32
33# We need to keep around at least the latest version released with a stable
34# NixOS
35let
36 archs = {
37 x86_64-linux = "linux-x86_64";
38 i686-linux = "linux-i686";
39 x86_64-darwin = "darwin-universal";
40 aarch64-darwin = "darwin-universal";
41 aarch64-linux = "linux-aarch64";
42 };
43
44 arch = archs.${stdenv.system} or (throw "system ${stdenv.system} not supported");
45
46 nativeCheckInputs = [ git gmp openssl readline libxml2 libyaml libffi ];
47
48 binaryUrl = version: rel:
49 if arch == archs.aarch64-linux then
50 "https://dev.alpinelinux.org/archive/crystal/crystal-${version}-aarch64-alpine-linux-musl.tar.gz"
51 else if arch == archs.x86_64-darwin && lib.versionOlder version "1.2.0" then
52 "https://github.com/crystal-lang/crystal/releases/download/${version}/crystal-${version}-${toString rel}-darwin-x86_64.tar.gz"
53 else
54 "https://github.com/crystal-lang/crystal/releases/download/${version}/crystal-${version}-${toString rel}-${arch}.tar.gz";
55
56 genericBinary = { version, sha256s, rel ? 1 }:
57 stdenv.mkDerivation rec {
58 pname = "crystal-binary";
59 inherit version;
60
61 src = fetchurl {
62 url = binaryUrl version rel;
63 sha256 = sha256s.${stdenv.system};
64 };
65
66 buildCommand = ''
67 mkdir -p $out
68 tar --strip-components=1 -C $out -xf ${src}
69 patchShebangs $out/bin/crystal
70 '';
71
72 meta.platforms = lib.attrNames sha256s;
73 };
74
75 generic =
76 { version
77 , sha256
78 , binary
79 , llvmPackages
80 , doCheck ? true
81 , extraBuildInputs ? [ ]
82 , buildFlags ? [ "all" "docs" "release=1"]
83 }:
84 stdenv.mkDerivation (finalAttrs: {
85 pname = "crystal";
86 inherit buildFlags doCheck version;
87
88 src = fetchFromGitHub {
89 owner = "crystal-lang";
90 repo = "crystal";
91 rev = version;
92 inherit sha256;
93 };
94
95 patches = [
96 (substituteAll {
97 src = ./tzdata.patch;
98 inherit tzdata;
99 })
100 ]
101 ++ lib.optionals (lib.versionOlder version "1.2.0") [
102 # add support for DWARF5 debuginfo, fixes builds on recent compilers
103 # the PR is 8 commits from 2019, so just fetch the whole thing
104 # and hope it doesn't change
105 (fetchpatch {
106 url = "https://github.com/crystal-lang/crystal/pull/11399.patch";
107 sha256 = "sha256-CjNpkQQ2UREADmlyLUt7zbhjXf0rTjFhNbFYLwJKkc8=";
108 })
109 ];
110
111 outputs = [ "out" "lib" "bin" ];
112
113 postPatch = ''
114 export TMP=$(mktemp -d)
115 export HOME=$TMP
116 export TMPDIR=$TMP
117 mkdir -p $HOME/test
118
119 # Add dependency of crystal to docs to avoid issue on flag changes between releases
120 # https://github.com/crystal-lang/crystal/pull/8792#issuecomment-614004782
121 substituteInPlace Makefile \
122 --replace 'docs: ## Generate standard library documentation' 'docs: crystal ## Generate standard library documentation'
123
124 mkdir -p $TMP/crystal
125
126 substituteInPlace spec/std/file_spec.cr \
127 --replace '/bin/ls' '${coreutils}/bin/ls' \
128 --replace '/usr/share' "$TMP/crystal" \
129 --replace '/usr' "$TMP" \
130 --replace '/tmp' "$TMP"
131
132 substituteInPlace spec/std/process_spec.cr \
133 --replace '/bin/cat' '${coreutils}/bin/cat' \
134 --replace '/bin/ls' '${coreutils}/bin/ls' \
135 --replace '/usr/bin/env' '${coreutils}/bin/env' \
136 --replace '"env"' '"${coreutils}/bin/env"' \
137 --replace '/usr' "$TMP" \
138 --replace '/tmp' "$TMP"
139
140 substituteInPlace spec/std/system_spec.cr \
141 --replace '`hostname`' '`${hostname}/bin/hostname`'
142
143 # See https://github.com/crystal-lang/crystal/issues/8629
144 substituteInPlace spec/std/socket/udp_socket_spec.cr \
145 --replace 'it "joins and transmits to multicast groups"' 'pending "joins and transmits to multicast groups"'
146
147 '' + lib.optionalString (stdenv.isDarwin && lib.versionAtLeast version "1.3.0" && lib.versionOlder version "1.7.0") ''
148 # See https://github.com/NixOS/nixpkgs/pull/195606#issuecomment-1356491277
149 substituteInPlace spec/compiler/loader/unix_spec.cr \
150 --replace 'it "parses file paths"' 'pending "parses file paths"'
151 '' + lib.optionalString (stdenv.cc.isClang && (stdenv.cc.libcxx != null)) ''
152 # Darwin links against libc++ not libstdc++. Newer versions of clang (12+) require
153 # libc++abi to be linked explicitly (see https://github.com/NixOS/nixpkgs/issues/166205).
154 substituteInPlace src/llvm/lib_llvm.cr \
155 --replace '@[Link("stdc++")]' '@[Link("c++")]'
156 '';
157
158 # Defaults are 4
159 preBuild = ''
160 export CRYSTAL_WORKERS=$NIX_BUILD_CORES
161 export threads=$NIX_BUILD_CORES
162 export CRYSTAL_CACHE_DIR=$TMP
163 export MACOSX_DEPLOYMENT_TARGET=10.11
164 '';
165
166
167 strictDeps = true;
168 nativeBuildInputs = [ binary makeWrapper which pkg-config llvmPackages.llvm ];
169 buildInputs = [
170 boehmgc
171 (if lib.versionAtLeast version "1.8" then pcre2 else pcre)
172 libevent
173 libyaml
174 zlib
175 libxml2
176 openssl
177 ] ++ extraBuildInputs
178 ++ lib.optionals stdenv.isDarwin [ libiconv ];
179
180 makeFlags = [
181 "CRYSTAL_CONFIG_VERSION=${version}"
182 "progress=1"
183 ];
184
185 LLVM_CONFIG = "${llvmPackages.llvm.dev}/bin/llvm-config";
186
187 FLAGS = [
188 "--single-module" # needed for deterministic builds
189 ] ++ lib.optionals (lib.versionAtLeast version "1.3.0" && lib.versionOlder version "1.6.1") [
190 # ffi is only used by the interpreter and its spec are broken on < 1.6.1
191 "-Dwithout_ffi"
192 ];
193
194 # This makes sure we don't keep depending on the previous version of
195 # crystal used to build this one.
196 CRYSTAL_LIBRARY_PATH = "${placeholder "lib"}/crystal";
197
198 # We *have* to add `which` to the PATH or crystal is unable to build
199 # stuff later if which is not available.
200 installPhase = ''
201 runHook preInstall
202
203 install -Dm755 .build/crystal $bin/bin/crystal
204 wrapProgram $bin/bin/crystal \
205 --suffix PATH : ${lib.makeBinPath [ pkg-config llvmPackages.clang which ]} \
206 --suffix CRYSTAL_PATH : lib:$lib/crystal \
207 --suffix PKG_CONFIG_PATH : ${
208 lib.makeSearchPathOutput "dev" "lib/pkgconfig" finalAttrs.buildInputs
209 } \
210 --suffix CRYSTAL_LIBRARY_PATH : ${
211 lib.makeLibraryPath finalAttrs.buildInputs
212 }
213 install -dm755 $lib/crystal
214 cp -r src/* $lib/crystal/
215
216 install -dm755 $out/share/doc/crystal/api
217 cp -r docs/* $out/share/doc/crystal/api/
218 cp -r samples $out/share/doc/crystal/
219
220 install -Dm644 etc/completion.bash $out/share/bash-completion/completions/crystal
221 install -Dm644 etc/completion.zsh $out/share/zsh/site-functions/_crystal
222
223 install -Dm644 man/crystal.1 $out/share/man/man1/crystal.1
224
225 install -Dm644 -t $out/share/licenses/crystal LICENSE README.md
226
227 mkdir -p $out
228 ln -s $bin/bin $out/bin
229 ln -s $lib $out/lib
230
231 runHook postInstall
232 '';
233
234 enableParallelBuilding = true;
235
236 dontStrip = true;
237
238 checkTarget = "compiler_spec";
239
240 preCheck = ''
241 export LIBRARY_PATH=${lib.makeLibraryPath nativeCheckInputs}:$LIBRARY_PATH
242 export PATH=${lib.makeBinPath nativeCheckInputs}:$PATH
243 '';
244
245 passthru.buildBinary = binary;
246 passthru.buildCrystalPackage = callPackage ./build-package.nix {
247 crystal = finalAttrs.finalPackage;
248 };
249
250 meta = with lib; {
251 inherit (binary.meta) platforms;
252 description = "Compiled language with Ruby like syntax and type inference";
253 mainProgram = "crystal";
254 homepage = "https://crystal-lang.org/";
255 license = licenses.asl20;
256 maintainers = with maintainers; [ david50407 manveru peterhoeg donovanglover ];
257 };
258 });
259in
260rec {
261 binaryCrystal_1_2 = genericBinary {
262 version = "1.2.2";
263 sha256s = {
264 x86_64-linux = "sha256-sW5nhihW/6Dkq95i3vJNWs2D1CtQhujhxVbgQCAas6E=";
265 aarch64-darwin = "sha256-4VB4yYGl1/YeYSsHOZq7fdeQ8IQMfloAPhEU0iKrvxs=";
266 x86_64-darwin = "sha256-4VB4yYGl1/YeYSsHOZq7fdeQ8IQMfloAPhEU0iKrvxs=";
267 aarch64-linux = "sha256-QgPKUDFyodqY1+b85AybSpbbr0RmfISdNpB08Wf34jo=";
268 };
269 };
270
271 binaryCrystal_1_10 = genericBinary {
272 version = "1.10.1";
273 sha256s = {
274 x86_64-linux = "sha256-F0LjdV02U9G6B8ApHxClF/o5KvhxMNukSX7Z2CwSNIs=";
275 aarch64-darwin = "sha256-5kkObQl0VIO6zqQ8TYl0JzYyUmwfmPE9targpfwseSQ=";
276 x86_64-darwin = "sha256-5kkObQl0VIO6zqQ8TYl0JzYyUmwfmPE9targpfwseSQ=";
277 aarch64-linux = "sha256-AzFz+nrU/HJmCL1hbCKXf5ej/uypqV1GJPVLQ4J3778=";
278 };
279 };
280
281 crystal_1_2 = generic {
282 version = "1.2.2";
283 sha256 = "sha256-nyOXhsutVBRdtJlJHe2dALl//BUXD1JeeQPgHU4SwiU=";
284 binary = binaryCrystal_1_2;
285 llvmPackages = llvmPackages_13;
286 extraBuildInputs = [ libatomic_ops ];
287 };
288
289 crystal_1_7 = generic {
290 version = "1.7.3";
291 sha256 = "sha256-ULhLGHRIZbsKhaMvNhc+W74BwNgfEjHcMnVNApWY+EE=";
292 binary = binaryCrystal_1_2;
293 llvmPackages = llvmPackages_13;
294 };
295
296 crystal_1_8 = generic {
297 version = "1.8.2";
298 sha256 = "sha256-YAORdipzpC9CrFgZUFlFfjzlJQ6ZeA2ekVu8IfPOxR8=";
299 binary = binaryCrystal_1_2;
300 llvmPackages = llvmPackages_15;
301 };
302
303 crystal_1_9 = generic {
304 version = "1.9.2";
305 sha256 = "sha256-M1oUFs7/8ljszga3StzLOLM1aA4fSfVPQlsbuDHGd84=";
306 binary = binaryCrystal_1_2;
307 llvmPackages = llvmPackages_15;
308 };
309
310 crystal_1_11 = generic {
311 version = "1.11.2";
312 sha256 = "sha256-BBEDWqFtmFUNj0kuGBzv71YHO3KjxV4d2ySTCD4HhLc=";
313 binary = binaryCrystal_1_10;
314 llvmPackages = llvmPackages_15;
315 };
316
317 crystal = crystal_1_11;
318}