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
21, makeWrapper
22, openssl
23, pcre2
24, pcre
25, pkg-config
26, readline
27, tzdata
28, which
29, zlib
30}:
31
32# We need to keep around at least the latest version released with a stable
33# NixOS
34let
35 archs = {
36 x86_64-linux = "linux-x86_64";
37 i686-linux = "linux-i686";
38 x86_64-darwin = "darwin-universal";
39 aarch64-darwin = "darwin-universal";
40 aarch64-linux = "linux-aarch64";
41 };
42
43 arch = archs.${stdenv.system} or (throw "system ${stdenv.system} not supported");
44 isAarch64Darwin = stdenv.system == "aarch64-darwin";
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 , doCheck ? true
80 , extraBuildInputs ? [ ]
81 , buildFlags ? [ "all" "docs" "release=1"]
82 }:
83 lib.fix (compiler: stdenv.mkDerivation (finalAttrs: {
84 pname = "crystal";
85 inherit buildFlags doCheck version;
86
87 src = fetchFromGitHub {
88 owner = "crystal-lang";
89 repo = "crystal";
90 rev = version;
91 inherit sha256;
92 };
93
94 patches = [
95 (substituteAll {
96 src = ./tzdata.patch;
97 inherit tzdata;
98 })
99 ]
100 ++ lib.optionals (lib.versionOlder version "1.2.0") [
101 # add support for DWARF5 debuginfo, fixes builds on recent compilers
102 # the PR is 8 commits from 2019, so just fetch the whole thing
103 # and hope it doesn't change
104 (fetchpatch {
105 url = "https://github.com/crystal-lang/crystal/pull/11399.patch";
106 sha256 = "sha256-CjNpkQQ2UREADmlyLUt7zbhjXf0rTjFhNbFYLwJKkc8=";
107 })
108 ];
109
110 outputs = [ "out" "lib" "bin" ];
111
112 postPatch = ''
113 export TMP=$(mktemp -d)
114 export HOME=$TMP
115 export TMPDIR=$TMP
116 mkdir -p $HOME/test
117
118 # Add dependency of crystal to docs to avoid issue on flag changes between releases
119 # https://github.com/crystal-lang/crystal/pull/8792#issuecomment-614004782
120 substituteInPlace Makefile \
121 --replace 'docs: ## Generate standard library documentation' 'docs: crystal ## Generate standard library documentation'
122
123 mkdir -p $TMP/crystal
124
125 substituteInPlace spec/std/file_spec.cr \
126 --replace '/bin/ls' '${coreutils}/bin/ls' \
127 --replace '/usr/share' "$TMP/crystal" \
128 --replace '/usr' "$TMP" \
129 --replace '/tmp' "$TMP"
130
131 substituteInPlace spec/std/process_spec.cr \
132 --replace '/bin/cat' '${coreutils}/bin/cat' \
133 --replace '/bin/ls' '${coreutils}/bin/ls' \
134 --replace '/usr/bin/env' '${coreutils}/bin/env' \
135 --replace '"env"' '"${coreutils}/bin/env"' \
136 --replace '/usr' "$TMP" \
137 --replace '/tmp' "$TMP"
138
139 substituteInPlace spec/std/system_spec.cr \
140 --replace '`hostname`' '`${hostname}/bin/hostname`'
141
142 # See https://github.com/crystal-lang/crystal/issues/8629
143 substituteInPlace spec/std/socket/udp_socket_spec.cr \
144 --replace 'it "joins and transmits to multicast groups"' 'pending "joins and transmits to multicast groups"'
145
146 '' + lib.optionalString (stdenv.isDarwin && lib.versionAtLeast version "1.3.0" && lib.versionOlder version "1.7.0") ''
147 # See https://github.com/NixOS/nixpkgs/pull/195606#issuecomment-1356491277
148 substituteInPlace spec/compiler/loader/unix_spec.cr \
149 --replace 'it "parses file paths"' 'pending "parses file paths"'
150 '';
151
152 # Defaults are 4
153 preBuild = ''
154 export CRYSTAL_WORKERS=$NIX_BUILD_CORES
155 export threads=$NIX_BUILD_CORES
156 export CRYSTAL_CACHE_DIR=$TMP
157 export MACOSX_DEPLOYMENT_TARGET=10.11
158 '';
159
160
161 strictDeps = true;
162 nativeBuildInputs = [ binary makeWrapper which pkg-config llvmPackages.llvm ];
163 buildInputs = [
164 boehmgc
165 (if lib.versionAtLeast version "1.8" then pcre2 else pcre)
166 libevent
167 libyaml
168 zlib
169 libxml2
170 openssl
171 ] ++ extraBuildInputs
172 ++ lib.optionals stdenv.isDarwin [ libiconv ];
173
174 makeFlags = [
175 "CRYSTAL_CONFIG_VERSION=${version}"
176 "progress=1"
177 ];
178
179 LLVM_CONFIG = "${llvmPackages.llvm.dev}/bin/llvm-config";
180
181 FLAGS = [
182 "--single-module" # needed for deterministic builds
183 ] ++ lib.optionals (lib.versionAtLeast version "1.3.0" && lib.versionOlder version "1.6.1") [
184 # ffi is only used by the interpreter and its spec are broken on < 1.6.1
185 "-Dwithout_ffi"
186 ];
187
188 # This makes sure we don't keep depending on the previous version of
189 # crystal used to build this one.
190 CRYSTAL_LIBRARY_PATH = "${placeholder "lib"}/crystal";
191
192 # We *have* to add `which` to the PATH or crystal is unable to build
193 # stuff later if which is not available.
194 installPhase = ''
195 runHook preInstall
196
197 install -Dm755 .build/crystal $bin/bin/crystal
198 wrapProgram $bin/bin/crystal \
199 --suffix PATH : ${lib.makeBinPath [ pkg-config llvmPackages.clang which ]} \
200 --suffix CRYSTAL_PATH : lib:$lib/crystal \
201 --suffix CRYSTAL_LIBRARY_PATH : ${
202 lib.makeLibraryPath finalAttrs.buildInputs
203 }
204 install -dm755 $lib/crystal
205 cp -r src/* $lib/crystal/
206
207 install -dm755 $out/share/doc/crystal/api
208 cp -r docs/* $out/share/doc/crystal/api/
209 cp -r samples $out/share/doc/crystal/
210
211 install -Dm644 etc/completion.bash $out/share/bash-completion/completions/crystal
212 install -Dm644 etc/completion.zsh $out/share/zsh/site-functions/_crystal
213
214 install -Dm644 man/crystal.1 $out/share/man/man1/crystal.1
215
216 install -Dm644 -t $out/share/licenses/crystal LICENSE README.md
217
218 mkdir -p $out
219 ln -s $bin/bin $out/bin
220 ln -s $lib $out/lib
221
222 runHook postInstall
223 '';
224
225 enableParallelBuilding = true;
226
227 dontStrip = true;
228
229 checkTarget = "compiler_spec";
230
231 preCheck = ''
232 export LIBRARY_PATH=${lib.makeLibraryPath nativeCheckInputs}:$LIBRARY_PATH
233 export PATH=${lib.makeBinPath nativeCheckInputs}:$PATH
234 '';
235
236 passthru.buildBinary = binary;
237 passthru.buildCrystalPackage = callPackage ./build-package.nix {
238 crystal = compiler;
239 };
240
241 meta = with lib; {
242 inherit (binary.meta) platforms;
243 description = "A compiled language with Ruby like syntax and type inference";
244 homepage = "https://crystal-lang.org/";
245 license = licenses.asl20;
246 maintainers = with maintainers; [ david50407 manveru peterhoeg ];
247 };
248 }))
249 );
250
251in
252rec {
253 binaryCrystal_1_2 = genericBinary {
254 version = "1.2.2";
255 sha256s = {
256 x86_64-linux = "sha256-sW5nhihW/6Dkq95i3vJNWs2D1CtQhujhxVbgQCAas6E=";
257 aarch64-darwin = "sha256-4VB4yYGl1/YeYSsHOZq7fdeQ8IQMfloAPhEU0iKrvxs=";
258 x86_64-darwin = "sha256-4VB4yYGl1/YeYSsHOZq7fdeQ8IQMfloAPhEU0iKrvxs=";
259 aarch64-linux = "sha256-QgPKUDFyodqY1+b85AybSpbbr0RmfISdNpB08Wf34jo=";
260 };
261 };
262
263 crystal_1_2 = generic {
264 version = "1.2.2";
265 sha256 = "sha256-nyOXhsutVBRdtJlJHe2dALl//BUXD1JeeQPgHU4SwiU=";
266 binary = binaryCrystal_1_2;
267 extraBuildInputs = [ libatomic_ops ];
268 };
269
270 crystal_1_7 = generic {
271 version = "1.7.3";
272 sha256 = "sha256-ULhLGHRIZbsKhaMvNhc+W74BwNgfEjHcMnVNApWY+EE=";
273 binary = binaryCrystal_1_2;
274 };
275
276 crystal_1_8 = generic {
277 version = "1.8.1";
278 sha256 = "sha256-t+1vM1m62UftCvfa90Dg6nqt6Zseh/GP/Gc1VfOa4+c=";
279 binary = binaryCrystal_1_2;
280 };
281
282 crystal = crystal_1_8;
283}