1{
2 lib,
3 stdenv,
4 callPackage,
5 ecl,
6 coreutils,
7 fetchurl,
8 ps,
9 strace,
10 texinfo,
11 which,
12 writableTmpDirAsHomeHook,
13 writeText,
14 zstd,
15 version,
16 # Set this to a lisp binary to use a custom bootstrap lisp compiler for SBCL.
17 # Leave as null to use the default. This is useful for local development of
18 # SBCL, because you can use your existing stock SBCL as a bootstrap. On Hydra
19 # of course we can’t do that because SBCL hasn’t been built yet, so we use
20 # ECL but that’s much slower.
21 bootstrapLisp ? null,
22}:
23
24let
25 versionMap = {
26 # Necessary for Nyxt
27 "2.4.6".sha256 = "sha256-pImQeELa4JoXJtYphb96VmcKrqLz7KH7cCO8pnw/MJE=";
28 # Necessary for stumpwm
29 "2.4.10".sha256 = "sha256-zus5a2nSkT7uBIQcKva+ylw0LOFGTD/j5FPy3hDF4vg=";
30 # By unofficial and very loose convention we keep the latest version of
31 # SBCL, and the previous one in case someone quickly needs to roll back.
32 "2.5.5".sha256 = "sha256-ZQJnCvs2G6m+RKL6/pr5tZ57JK5QmnkaZrVIHylVlQs=";
33 "2.5.7".sha256 = "sha256-xPr+t5VpnVvP+QhQkazHYtz15V+FI1Yl89eu8SyJ0dM=";
34 };
35 # Collection of pre-built SBCL binaries for platforms that need them for
36 # bootstrapping. Ideally these are to be avoided. If ECL (or any other
37 # non-binary-distributed Lisp) can run on any of these systems, that entry
38 # should be removed from this list.
39 bootstrapBinaries = rec {
40 i686-linux = {
41 version = "1.2.7";
42 system = "x86-linux";
43 sha256 = "07f3bz4br280qvn85i088vpzj9wcz8wmwrf665ypqx181pz2ai3j";
44 };
45 armv7l-linux = {
46 version = "1.2.14";
47 system = "armhf-linux";
48 sha256 = "0sp5445rbvms6qvzhld0kwwvydw51vq5iaf4kdqsf2d9jvaz3yx5";
49 };
50 armv6l-linux = armv7l-linux;
51 x86_64-freebsd = {
52 version = "1.2.7";
53 system = "x86-64-freebsd";
54 sha256 = "14k42xiqd2rrim4pd5k5pjcrpkac09qnpynha8j1v4jngrvmw7y6";
55 };
56 x86_64-solaris = {
57 version = "1.2.7";
58 system = "x86-64-solaris";
59 sha256 = "05c12fmac4ha72k1ckl6i780rckd7jh4g5s5hiic7fjxnf1kx8d0";
60 };
61 };
62 sbclBootstrap = callPackage ./bootstrap.nix {
63 cfg = bootstrapBinaries.${stdenv.hostPlatform.system};
64 };
65 bootstrapLisp' =
66 if bootstrapLisp != null then
67 bootstrapLisp
68 else if (builtins.hasAttr stdenv.hostPlatform.system bootstrapBinaries) then
69 "${sbclBootstrap}/bin/sbcl --disable-debugger --no-userinit --no-sysinit"
70 else
71 "${lib.getExe ecl} --norc";
72
73in
74
75stdenv.mkDerivation (self: {
76 pname = "sbcl";
77 inherit version;
78
79 src = fetchurl {
80 # Changing the version shouldn’t change the source for the
81 # derivation. Override the src entirely if desired.
82 url = "mirror://sourceforge/project/sbcl/sbcl/${version}/sbcl-${version}-source.tar.bz2";
83 inherit (versionMap.${version}) sha256;
84 };
85
86 nativeBuildInputs = [
87 texinfo
88 ]
89 ++ lib.optionals self.doCheck (
90 [
91 which
92 writableTmpDirAsHomeHook
93 ]
94 ++ lib.optionals (builtins.elem stdenv.system strace.meta.platforms) [
95 strace
96 ]
97 ++ lib.optionals (lib.versionOlder "2.4.10" self.version) [
98 ps
99 ]
100 );
101 buildInputs = lib.optionals self.coreCompression (
102 # Declare at the point of actual use in case the caller wants to override
103 # buildInputs to sidestep this.
104 assert lib.assertMsg (!self.purgeNixReferences) ''
105 Cannot enable coreCompression when purging Nix references, because compression requires linking in zstd
106 '';
107 [ zstd ]
108 );
109
110 threadSupport = (
111 stdenv.hostPlatform.isx86
112 || "aarch64-linux" == stdenv.hostPlatform.system
113 || "aarch64-darwin" == stdenv.hostPlatform.system
114 );
115 # Meant for sbcl used for creating binaries portable to non-NixOS via save-lisp-and-die.
116 # Note that the created binaries still need `patchelf --set-interpreter ...`
117 # to get rid of ${glibc} dependency.
118 purgeNixReferences = false;
119 coreCompression = true;
120 markRegionGC = self.threadSupport;
121 disableImmobileSpace = false;
122 linkableRuntime = stdenv.hostPlatform.isx86;
123
124 # I don’t know why these are failing (on ofBorg), and I’d rather just disable
125 # them and move forward with the succeeding tests than block testing
126 # altogether. One by one hopefully we can fix these (on ofBorg,
127 # upstream--somehow some way) in due time.
128 disabledTestFiles =
129 lib.optionals (lib.versionOlder "2.5.2" self.version) [ "debug.impure.lisp" ]
130 ++
131 lib.optionals
132 (builtins.elem stdenv.hostPlatform.system [
133 "x86_64-linux"
134 "aarch64-linux"
135 ])
136 [
137 "foreign-stack-alignment.impure.lisp"
138 # Floating point tests are fragile
139 # https://sourceforge.net/p/sbcl/mailman/message/58728554/
140 "compiler.pure.lisp"
141 "float.pure.lisp"
142 ]
143 ++ lib.optionals (stdenv.hostPlatform.system == "aarch64-linux") [
144 # This is failing on aarch64-linux on ofBorg. Not on my local machine nor on
145 # a VM on my laptop. Not sure what’s wrong.
146 "traceroot.impure.lisp"
147 # Heisentest, sometimes fails on ofBorg, would rather just disable it than
148 # have it block a release.
149 "futex-wait.test.sh"
150 ];
151 patches =
152 # Support the NIX_SBCL_DYNAMIC_SPACE_SIZE envvar. Upstream SBCL didn’t want
153 # to include this (see
154 # "https://sourceforge.net/p/sbcl/mailman/sbcl-devel/thread/2cf20df7-01d0-44f2-8551-0df01fe55f1a%400brg.net/"),
155 # but for Nix envvars are sufficiently useful that it’s worth maintaining
156 # this functionality downstream.
157 if lib.versionOlder "2.5.2" self.version then
158 [
159 ./dynamic-space-size-envvar-2.5.3-feature.patch
160 ./dynamic-space-size-envvar-2.5.3-tests.patch
161 ]
162 else
163 [
164 ./dynamic-space-size-envvar-2.5.2-feature.patch
165 ./dynamic-space-size-envvar-2.5.2-tests.patch
166 ];
167
168 sbclPatchPhase =
169 lib.optionalString (self.disabledTestFiles != [ ]) ''
170 (cd tests ; rm -f ${lib.concatStringsSep " " self.disabledTestFiles})
171 ''
172 + lib.optionalString self.purgeNixReferences ''
173 # This is the default location to look for the core; by default in $out/lib/sbcl
174 sed 's@^\(#define SBCL_HOME\) .*$@\1 "/no-such-path"@' \
175 -i src/runtime/runtime.c
176 ''
177 + ''
178 (
179 shopt -s nullglob
180 # Tests need patching regardless of purging of paths from the final
181 # binary. There are some tricky files in nested directories which should
182 # definitely NOT be patched this way, hence just a single * (and no
183 # globstar).
184 substituteInPlace ${if self.purgeNixReferences then "tests" else "{tests,src/code}"}/*.{lisp,sh} \
185 --replace-quiet /usr/bin/env "${coreutils}/bin/env" \
186 --replace-quiet /bin/uname "${coreutils}/bin/uname" \
187 --replace-quiet /bin/sh "${stdenv.shell}"
188 )
189 # Official source release tarballs will have a version.lispexpr, but if you
190 # want to override { src = ... } it might not exist. It’s required for
191 # building, so create a mock version as a backup.
192 if [[ ! -a version.lisp-expr ]]; then
193 echo '"${self.version}.nixos"' > version.lisp-expr
194 fi
195 '';
196
197 preConfigurePhases = "sbclPatchPhase";
198
199 enableFeatures =
200 assert lib.assertMsg (
201 self.markRegionGC -> self.threadSupport
202 ) "SBCL mark region GC requires thread support";
203 lib.optional self.threadSupport "sb-thread"
204 ++ lib.optional self.linkableRuntime "sb-linkable-runtime"
205 ++ lib.optional self.coreCompression "sb-core-compression"
206 ++ lib.optional stdenv.hostPlatform.isAarch32 "arm"
207 ++ lib.optional self.markRegionGC "mark-region-gc";
208
209 disableFeatures =
210 lib.optional (!self.threadSupport) "sb-thread"
211 ++ lib.optionals self.disableImmobileSpace [
212 "immobile-space"
213 "immobile-code"
214 "compact-instance-header"
215 ];
216
217 buildArgs = [
218 "--prefix=$out"
219 "--xc-host=${lib.escapeShellArg bootstrapLisp'}"
220 ]
221 ++ builtins.map (x: "--with-${x}") self.enableFeatures
222 ++ builtins.map (x: "--without-${x}") self.disableFeatures
223 ++ lib.optionals (stdenv.hostPlatform.system == "aarch64-darwin") [
224 "--arch=arm64"
225 ];
226
227 # Fails to find `O_LARGEFILE` otherwise.
228 env.NIX_CFLAGS_COMPILE = "-D_GNU_SOURCE";
229
230 buildPhase = ''
231 runHook preBuild
232
233 export INSTALL_ROOT=$out
234 sh make.sh ${lib.concatStringsSep " " self.buildArgs}
235 (cd doc/manual ; make info)
236
237 runHook postBuild
238 '';
239
240 # Tests on ofBorg’s x86_64-darwin platforms are so unstable that a random one
241 # will fail every other run. There’s a deeper problem here; we might as well
242 # disable them entirely so at least the other platforms get to benefit from
243 # testing.
244 doCheck = stdenv.hostPlatform.system != "x86_64-darwin";
245
246 # From the INSTALL docs
247 checkPhase = ''
248 runHook preCheck
249
250 (cd tests && sh run-tests.sh)
251
252 runHook postCheck
253 '';
254
255 installPhase = ''
256 runHook preInstall
257
258 sh install.sh
259
260 ''
261 + lib.optionalString (!self.purgeNixReferences) ''
262 cp -r src $out/lib/sbcl
263 cp -r contrib $out/lib/sbcl
264 cat >$out/lib/sbcl/sbclrc <<EOF
265 (setf (logical-pathname-translations "SYS")
266 '(("SYS:SRC;**;*.*.*" #P"$out/lib/sbcl/src/**/*.*")
267 ("SYS:CONTRIB;**;*.*.*" #P"$out/lib/sbcl/contrib/**/*.*")))
268 EOF
269 ''
270 + ''
271 runHook postInstall
272 '';
273
274 setupHook = lib.optional self.purgeNixReferences (
275 writeText "setupHook.sh" ''
276 addEnvHooks "$targetOffset" _setSbclHome
277 _setSbclHome() {
278 export SBCL_HOME='@out@/lib/sbcl/'
279 }
280 ''
281 );
282
283 meta = with lib; {
284 description = "Common Lisp compiler";
285 homepage = "https://sbcl.org";
286 license = licenses.publicDomain; # and FreeBSD
287 mainProgram = "sbcl";
288 teams = [ lib.teams.lisp ];
289 platforms = attrNames bootstrapBinaries ++ [
290 # These aren’t bootstrapped using the binary distribution but compiled
291 # using a separate (lisp) host
292 "x86_64-darwin"
293 "x86_64-linux"
294 "aarch64-darwin"
295 "aarch64-linux"
296 ];
297 };
298})