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