traefik: add djds as maintainer (#377217)
[NixPkgs.git] / pkgs / development / compilers / crystal / default.nix
blob702a6ec37dbb2f88b49967758b95d828e0b695c0
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 , llvmPackages_18
23 , makeWrapper
24 , openssl
25 , pcre2
26 , pcre
27 , pkg-config
28 , installShellFiles
29 , readline
30 , tzdata
31 , which
32 , zlib
35 # We need to keep around at least the latest version released with a stable
36 # NixOS
37 let
38   archs = {
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";
44   };
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"
55     else
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";
61     inherit version;
63     src = fetchurl {
64       url = binaryUrl version rel;
65       sha256 = sha256s.${stdenv.system};
66     };
68     buildCommand = ''
69       mkdir -p $out
70       tar --strip-components=1 -C $out -xf ${src}
71       patchShebangs $out/bin/crystal
72     '';
74     meta.platforms = lib.attrNames sha256s;
75   };
77   generic =
78     { version
79     , sha256
80     , binary
81     , llvmPackages
82     , doCheck ? true
83     , extraBuildInputs ? [ ]
84     , buildFlags ? [ "all" "docs" "release=1"]
85     }:
86     stdenv.mkDerivation (finalAttrs: {
87       pname = "crystal";
88       inherit buildFlags doCheck version;
90       src = fetchFromGitHub {
91         owner = "crystal-lang";
92         repo = "crystal";
93         rev = version;
94         inherit sha256;
95       };
97       patches = [
98           (substituteAll {
99             src = ./tzdata.patch;
100             inherit tzdata;
101           })
102         ]
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
107         (fetchpatch {
108           url = "https://github.com/crystal-lang/crystal/pull/11399.patch";
109           sha256 = "sha256-CjNpkQQ2UREADmlyLUt7zbhjXf0rTjFhNbFYLwJKkc8=";
110         })
111       ];
113       outputs = [ "out" "lib" "bin" ];
115       postPatch = ''
116         export TMP=$(mktemp -d)
117         export HOME=$TMP
118         export TMPDIR=$TMP
119         mkdir -p $HOME/test
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++")]'
158       '';
160       # Defaults are 4
161       preBuild = ''
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
166       '';
169       strictDeps = true;
170       nativeBuildInputs = [ binary makeWrapper which pkg-config llvmPackages.llvm installShellFiles ];
171       buildInputs = [
172         boehmgc
173         (if lib.versionAtLeast version "1.8" then pcre2 else pcre)
174         libevent
175         libyaml
176         zlib
177         libxml2
178         openssl
179       ] ++ extraBuildInputs
180       ++ lib.optionals stdenv.hostPlatform.isDarwin [ libiconv ];
182       makeFlags = [
183         "CRYSTAL_CONFIG_VERSION=${version}"
184         "progress=1"
185       ];
187       LLVM_CONFIG = "${llvmPackages.llvm.dev}/bin/llvm-config";
189       FLAGS = [
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
193         "-Dwithout_ffi"
194       ];
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.
202       installPhase = ''
203         runHook preInstall
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
211           } \
212           --suffix CRYSTAL_LIBRARY_PATH : ${
213             lib.makeLibraryPath finalAttrs.buildInputs
214           }
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
228         mkdir -p $out
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
234         ln -s $lib $out/lib
236         runHook postInstall
237       '';
239       enableParallelBuilding = true;
241       dontStrip = true;
243       checkTarget = "compiler_spec";
245       preCheck = ''
246         export LIBRARY_PATH=${lib.makeLibraryPath nativeCheckInputs}:$LIBRARY_PATH
247         export PATH=${lib.makeBinPath nativeCheckInputs}:$PATH
248       '';
250       passthru.buildBinary = binary;
251       passthru.buildCrystalPackage = callPackage ./build-package.nix {
252         crystal = finalAttrs.finalPackage;
253       };
254       passthru.llvmPackages = llvmPackages;
256       meta = with lib; {
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 ];
263       };
264     });
266 rec {
267   binaryCrystal_1_2 = genericBinary {
268     version = "1.2.2";
269     sha256s = {
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=";
274     };
275   };
277   binaryCrystal_1_10 = genericBinary {
278     version = "1.10.1";
279     sha256s = {
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=";
284     };
285   };
287   crystal_1_2 = generic {
288     version = "1.2.2";
289     sha256 = "sha256-nyOXhsutVBRdtJlJHe2dALl//BUXD1JeeQPgHU4SwiU=";
290     binary = binaryCrystal_1_2;
291     llvmPackages = llvmPackages_13;
292     extraBuildInputs = [ libatomic_ops ];
293   };
295   crystal_1_7 = generic {
296     version = "1.7.3";
297     sha256 = "sha256-ULhLGHRIZbsKhaMvNhc+W74BwNgfEjHcMnVNApWY+EE=";
298     binary = binaryCrystal_1_2;
299     llvmPackages = llvmPackages_13;
300   };
302   crystal_1_8 = generic {
303     version = "1.8.2";
304     sha256 = "sha256-YAORdipzpC9CrFgZUFlFfjzlJQ6ZeA2ekVu8IfPOxR8=";
305     binary = binaryCrystal_1_2;
306     llvmPackages = llvmPackages_15;
307   };
309   crystal_1_9 = generic {
310     version = "1.9.2";
311     sha256 = "sha256-M1oUFs7/8ljszga3StzLOLM1aA4fSfVPQlsbuDHGd84=";
312     binary = binaryCrystal_1_2;
313     llvmPackages = llvmPackages_15;
314   };
316   crystal_1_11 = generic {
317     version = "1.11.2";
318     sha256 = "sha256-BBEDWqFtmFUNj0kuGBzv71YHO3KjxV4d2ySTCD4HhLc=";
319     binary = binaryCrystal_1_10;
320     llvmPackages = llvmPackages_15;
321   };
323   crystal_1_12 = generic {
324     version = "1.12.1";
325     sha256 = "sha256-Q6uI9zPZ3IOGyUuWdC179GPktPGFPRbRWKtOF4YWCBw=";
326     binary = binaryCrystal_1_10;
327     llvmPackages = llvmPackages_18;
328   };
330   crystal_1_14 = generic {
331     version = "1.14.1";
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
336   };
339   crystal = crystal_1_14;