35 # We need to keep around at least the latest version released with a stable
39 x86_64-linux = "linux-x86_64";
40 i686-linux = "linux-i686";
41 x86_64-darwin = "darwin-universal";
42 aarch64-darwin = "darwin-universal";
43 aarch64-linux = "linux-aarch64";
46 arch = archs.${stdenv.system} or (throw "system ${stdenv.system} not supported");
48 nativeCheckInputs = [ git gmp openssl readline libxml2 libyaml libffi ];
50 binaryUrl = version: rel:
51 if arch == archs.aarch64-linux then
52 "https://dev.alpinelinux.org/archive/crystal/crystal-${version}-aarch64-alpine-linux-musl.tar.gz"
53 else if arch == archs.x86_64-darwin && lib.versionOlder version "1.2.0" then
54 "https://github.com/crystal-lang/crystal/releases/download/${version}/crystal-${version}-${toString rel}-darwin-x86_64.tar.gz"
56 "https://github.com/crystal-lang/crystal/releases/download/${version}/crystal-${version}-${toString rel}-${arch}.tar.gz";
58 genericBinary = { version, sha256s, rel ? 1 }:
59 stdenv.mkDerivation rec {
60 pname = "crystal-binary";
64 url = binaryUrl version rel;
65 sha256 = sha256s.${stdenv.system};
70 tar --strip-components=1 -C $out -xf ${src}
71 patchShebangs $out/bin/crystal
74 meta.platforms = lib.attrNames sha256s;
83 , extraBuildInputs ? [ ]
84 , buildFlags ? [ "all" "docs" "release=1"]
86 stdenv.mkDerivation (finalAttrs: {
88 inherit buildFlags doCheck version;
90 src = fetchFromGitHub {
91 owner = "crystal-lang";
103 ++ lib.optionals (lib.versionOlder version "1.2.0") [
104 # add support for DWARF5 debuginfo, fixes builds on recent compilers
105 # the PR is 8 commits from 2019, so just fetch the whole thing
106 # and hope it doesn't change
108 url = "https://github.com/crystal-lang/crystal/pull/11399.patch";
109 sha256 = "sha256-CjNpkQQ2UREADmlyLUt7zbhjXf0rTjFhNbFYLwJKkc8=";
113 outputs = [ "out" "lib" "bin" ];
116 export TMP=$(mktemp -d)
121 # Add dependency of crystal to docs to avoid issue on flag changes between releases
122 # https://github.com/crystal-lang/crystal/pull/8792#issuecomment-614004782
123 substituteInPlace Makefile \
124 --replace 'docs: ## Generate standard library documentation' 'docs: crystal ## Generate standard library documentation'
126 mkdir -p $TMP/crystal
128 substituteInPlace spec/std/file_spec.cr \
129 --replace '/bin/ls' '${coreutils}/bin/ls' \
130 --replace '/usr/share' "$TMP/crystal" \
131 --replace '/usr' "$TMP" \
132 --replace '/tmp' "$TMP"
134 substituteInPlace spec/std/process_spec.cr \
135 --replace '/bin/cat' '${coreutils}/bin/cat' \
136 --replace '/bin/ls' '${coreutils}/bin/ls' \
137 --replace '/usr/bin/env' '${coreutils}/bin/env' \
138 --replace '"env"' '"${coreutils}/bin/env"' \
139 --replace '/usr' "$TMP" \
140 --replace '/tmp' "$TMP"
142 substituteInPlace spec/std/system_spec.cr \
143 --replace '`hostname`' '`${hostname}/bin/hostname`'
145 # See https://github.com/crystal-lang/crystal/issues/8629
146 substituteInPlace spec/std/socket/udp_socket_spec.cr \
147 --replace 'it "joins and transmits to multicast groups"' 'pending "joins and transmits to multicast groups"'
149 '' + lib.optionalString (stdenv.hostPlatform.isDarwin && lib.versionAtLeast version "1.3.0" && lib.versionOlder version "1.7.0") ''
150 # See https://github.com/NixOS/nixpkgs/pull/195606#issuecomment-1356491277
151 substituteInPlace spec/compiler/loader/unix_spec.cr \
152 --replace 'it "parses file paths"' 'pending "parses file paths"'
153 '' + lib.optionalString (stdenv.cc.isClang && (stdenv.cc.libcxx != null)) ''
154 # Darwin links against libc++ not libstdc++. Newer versions of clang (12+) require
155 # libc++abi to be linked explicitly (see https://github.com/NixOS/nixpkgs/issues/166205).
156 substituteInPlace src/llvm/lib_llvm.cr \
157 --replace '@[Link("stdc++")]' '@[Link("c++")]'
162 export CRYSTAL_WORKERS=$NIX_BUILD_CORES
163 export threads=$NIX_BUILD_CORES
164 export CRYSTAL_CACHE_DIR=$TMP
165 export MACOSX_DEPLOYMENT_TARGET=10.11
170 nativeBuildInputs = [ binary makeWrapper which pkg-config llvmPackages.llvm installShellFiles ];
173 (if lib.versionAtLeast version "1.8" then pcre2 else pcre)
179 ] ++ extraBuildInputs
180 ++ lib.optionals stdenv.hostPlatform.isDarwin [ libiconv ];
183 "CRYSTAL_CONFIG_VERSION=${version}"
187 LLVM_CONFIG = "${llvmPackages.llvm.dev}/bin/llvm-config";
190 "--single-module" # needed for deterministic builds
191 ] ++ lib.optionals (lib.versionAtLeast version "1.3.0" && lib.versionOlder version "1.6.1") [
192 # ffi is only used by the interpreter and its spec are broken on < 1.6.1
196 # This makes sure we don't keep depending on the previous version of
197 # crystal used to build this one.
198 CRYSTAL_LIBRARY_PATH = "${placeholder "lib"}/crystal";
200 # We *have* to add `which` to the PATH or crystal is unable to build
201 # stuff later if which is not available.
205 install -Dm755 .build/crystal $bin/bin/crystal
206 wrapProgram $bin/bin/crystal \
207 --suffix PATH : ${lib.makeBinPath [ pkg-config llvmPackages.clang which ]} \
208 --suffix CRYSTAL_PATH : lib:$lib/crystal \
209 --suffix PKG_CONFIG_PATH : ${
210 lib.makeSearchPathOutput "dev" "lib/pkgconfig" finalAttrs.buildInputs
212 --suffix CRYSTAL_LIBRARY_PATH : ${
213 lib.makeLibraryPath finalAttrs.buildInputs
215 install -dm755 $lib/crystal
216 cp -r src/* $lib/crystal/
218 install -dm755 $out/share/doc/crystal/api
219 cp -r docs/* $out/share/doc/crystal/api/
220 cp -r samples $out/share/doc/crystal/
222 installShellCompletion --cmd ${finalAttrs.meta.mainProgram} etc/completion.*
224 installManPage man/crystal.1
226 install -Dm644 -t $out/share/licenses/crystal LICENSE README.md
229 ln -s $bin/bin $out/bin
230 ln -s $bin/share/bash-completion $out/share/bash-completion
231 ln -s $bin/share/zsh $out/share/zsh
232 # fish completion was introduced in 1.6.0
233 test -f etc/completion.fish && ln -s $bin/share/fish $out/share/fish
239 enableParallelBuilding = true;
243 checkTarget = "compiler_spec";
246 export LIBRARY_PATH=${lib.makeLibraryPath nativeCheckInputs}:$LIBRARY_PATH
247 export PATH=${lib.makeBinPath nativeCheckInputs}:$PATH
250 passthru.buildBinary = binary;
251 passthru.buildCrystalPackage = callPackage ./build-package.nix {
252 crystal = finalAttrs.finalPackage;
254 passthru.llvmPackages = llvmPackages;
257 inherit (binary.meta) platforms;
258 description = "Compiled language with Ruby like syntax and type inference";
259 mainProgram = "crystal";
260 homepage = "https://crystal-lang.org/";
261 license = licenses.asl20;
262 maintainers = with maintainers; [ david50407 manveru peterhoeg donovanglover ];
267 binaryCrystal_1_2 = genericBinary {
270 x86_64-linux = "sha256-sW5nhihW/6Dkq95i3vJNWs2D1CtQhujhxVbgQCAas6E=";
271 aarch64-darwin = "sha256-4VB4yYGl1/YeYSsHOZq7fdeQ8IQMfloAPhEU0iKrvxs=";
272 x86_64-darwin = "sha256-4VB4yYGl1/YeYSsHOZq7fdeQ8IQMfloAPhEU0iKrvxs=";
273 aarch64-linux = "sha256-QgPKUDFyodqY1+b85AybSpbbr0RmfISdNpB08Wf34jo=";
277 binaryCrystal_1_10 = genericBinary {
280 x86_64-linux = "sha256-F0LjdV02U9G6B8ApHxClF/o5KvhxMNukSX7Z2CwSNIs=";
281 aarch64-darwin = "sha256-5kkObQl0VIO6zqQ8TYl0JzYyUmwfmPE9targpfwseSQ=";
282 x86_64-darwin = "sha256-5kkObQl0VIO6zqQ8TYl0JzYyUmwfmPE9targpfwseSQ=";
283 aarch64-linux = "sha256-AzFz+nrU/HJmCL1hbCKXf5ej/uypqV1GJPVLQ4J3778=";
287 crystal_1_2 = generic {
289 sha256 = "sha256-nyOXhsutVBRdtJlJHe2dALl//BUXD1JeeQPgHU4SwiU=";
290 binary = binaryCrystal_1_2;
291 llvmPackages = llvmPackages_13;
292 extraBuildInputs = [ libatomic_ops ];
295 crystal_1_7 = generic {
297 sha256 = "sha256-ULhLGHRIZbsKhaMvNhc+W74BwNgfEjHcMnVNApWY+EE=";
298 binary = binaryCrystal_1_2;
299 llvmPackages = llvmPackages_13;
302 crystal_1_8 = generic {
304 sha256 = "sha256-YAORdipzpC9CrFgZUFlFfjzlJQ6ZeA2ekVu8IfPOxR8=";
305 binary = binaryCrystal_1_2;
306 llvmPackages = llvmPackages_15;
309 crystal_1_9 = generic {
311 sha256 = "sha256-M1oUFs7/8ljszga3StzLOLM1aA4fSfVPQlsbuDHGd84=";
312 binary = binaryCrystal_1_2;
313 llvmPackages = llvmPackages_15;
316 crystal_1_11 = generic {
318 sha256 = "sha256-BBEDWqFtmFUNj0kuGBzv71YHO3KjxV4d2ySTCD4HhLc=";
319 binary = binaryCrystal_1_10;
320 llvmPackages = llvmPackages_15;
323 crystal_1_12 = generic {
325 sha256 = "sha256-Q6uI9zPZ3IOGyUuWdC179GPktPGFPRbRWKtOF4YWCBw=";
326 binary = binaryCrystal_1_10;
327 llvmPackages = llvmPackages_18;
330 crystal_1_14 = generic {
332 sha256 = "sha256-cQWK92BfksOW8GmoXn4BmPGJ7CLyLAeKccOffQMh5UU=";
333 binary = binaryCrystal_1_10;
334 llvmPackages = llvmPackages_18;
335 doCheck = false; # Some compiler spec problems on x86-64_linux with the .0 release
339 crystal = crystal_1_14;