···11# lisp-modules {#lisp}
2233This document describes the Nixpkgs infrastructure for building Common Lisp
44-libraries that use ASDF (Another System Definition Facility). It lives in
55-`pkgs/development/lisp-modules`.
44+systems that use [ASDF](https://asdf.common-lisp.dev/) (Another System
55+Definition Facility). It lives in `pkgs/development/lisp-modules`.
6677## Overview {#lisp-overview}
8899The main entry point of the API are the Common Lisp implementation packages
1010-(e.g. `abcl`, `ccl`, `clasp-common-lisp`, `clisp` `ecl`, `sbcl`)
1111-themselves. They have the `pkgs` and `withPackages` attributes, which can be
1212-used to discover available packages and to build wrappers, respectively.
1010+themselves (e.g. `abcl`, `ccl`, `clasp-common-lisp`, `clisp`, `ecl`,
1111+`sbcl`). They have the `pkgs` and `withPackages` attributes, which can be used
1212+to discover available packages and to build wrappers, respectively.
13131414-The `pkgs` attribute set contains packages that were automatically imported from
1515-Quicklisp, and any other manually defined ones. Not every package works for all
1616-the CL implementations (e.g. `nyxt` only makes sense for `sbcl`).
1414+The `pkgs` attribute set contains packages that were automatically
1515+[imported](#lisp-importing-packages-from-quicklisp) from Quicklisp, and any
1616+other [manually defined](#lisp-defining-packages-inside) ones. Not every package
1717+works for all the CL implementations (e.g. `nyxt` only makes sense for `sbcl`).
17181818-The `withPackages` function is of primary utility. It is used to build runnable
1919-wrappers, with a pinned and pre-built ASDF FASL available in the `ASDF`
2020-environment variable, and `CL_SOURCE_REGISTRY`/`ASDF_OUTPUT_TRANSLATIONS`
2121-configured to find the desired systems on runtime.
2222-2323-With a few exceptions, the primary thing that the infrastructure does is to run
2424-`asdf:load-system` for each system specified in the `systems` argument to
2525-`build-asdf-system`, and save the FASLs to the Nix store. Then, it makes these
2626-FASLs available to wrappers. Any other use-cases, such as producing SBCL
2727-executables with `sb-ext:save-lisp-and-die`, are achieved via overriding the
2828-`buildPhase` etc.
1919+The `withPackages` function is of primary utility. It is used to build
2020+[runnable wrappers](#lisp-building-wrappers), with a pinned and pre-built
2121+[ASDF FASL](#lisp-loading-asdf) available in the `ASDF` environment variable,
2222+and `CL_SOURCE_REGISTRY`/`ASDF_OUTPUT_TRANSLATIONS` configured to
2323+[find the desired systems on runtime](#lisp-loading-systems).
29243025In addition, Lisps have the `withOverrides` function, which can be used to
3131-substitute any package in the scope of their `pkgs`. This will be useful
3232-together with `overrideLispAttrs` when dealing with slashy ASDF systems, because
3333-they should stay in the main package and be build by specifying the `systems`
2626+[substitute](#lisp-including-external-pkg-in-scope) any package in the scope of
2727+their `pkgs`. This will also be useful together with `overrideLispAttrs` when
2828+[dealing with slashy systems](#lisp-dealing-with-slashy-systems), because they
2929+should stay in the main package and be built by specifying the `systems`
3430argument to `build-asdf-system`.
35313632## The 90% use case example {#lisp-use-case-example}
···4238Then, in a shell:
43394440```
4545-$ result/bin/sbcl
4141+$ sbcl
4642* (load (sb-ext:posix-getenv "ASDF"))
4743* (asdf:load-system 'alexandria)
4844```
···5349let
5450 sbcl' = sbcl.withPackages (ps: [ ps.alexandria ]);
5551in mkShell {
5656- buildInputs = [ sbcl' ];
5252+ packages = [ sbcl' ];
5753}
5854```
5955···67636864## Importing packages from Quicklisp {#lisp-importing-packages-from-quicklisp}
69657070-The library is able to very quickly import all the packages distributed by
7171-Quicklisp by parsing its `releases.txt` and `systems.txt` files. These files are
7272-available from [http://beta.quicklisp.org/dist/quicklisp.txt].
6666+To save some work of writing Nix expressions, there is a script that imports all
6767+the packages distributed by Quicklisp into `imported.nix`. This works by parsing
6868+its `releases.txt` and `systems.txt` files, which are published every couple of
6969+months on [quicklisp.org](http://beta.quicklisp.org/dist/quicklisp.txt).
73707471The import process is implemented in the `import` directory as Common Lisp
7575-functions in the `org.lispbuilds.nix` ASDF system. To run the script, one can
7272+code in the `org.lispbuilds.nix` ASDF system. To run the script, one can
7673execute `ql-import.lisp`:
77747875```
7676+cd pkgs/development/lisp-modules
7977nix-shell --run 'sbcl --script ql-import.lisp'
8078```
81798280The script will:
838184821. Download the latest Quicklisp `systems.txt` and `releases.txt` files
8585-2. Generate an SQLite database of all QL systems in `packages.sqlite`
8383+2. Generate a temporary SQLite database of all QL systems in `packages.sqlite`
86843. Generate an `imported.nix` file from the database
87858888-The maintainer's job there is to:
8686+(The `packages.sqlite` file can be deleted at will, because it is regenerated
8787+each time the script runs.)
89889090-1. Re-run the `ql-import.lisp` script
9191-2. Add missing native dependencies in `ql.nix`
9292-3. For packages that still don't build, package them manually in `packages.nix`
8989+The maintainer's job is to:
9090+9191+1. Re-run the `ql-import.lisp` script when there is a new Quicklisp release
9292+2. [Add any missing native dependencies](#lisp-quicklisp-adding-native-dependencies) in `ql.nix`
9393+3. For packages that still don't build, [package them manually](#lisp-defining-packages-inside) in `packages.nix`
93949495Also, the `imported.nix` file **must not be edited manually**! It should only be
9595-generated as described in this section.
9696+generated as described in this section (by running `ql-import.lisp`).
96979798### Adding native dependencies {#lisp-quicklisp-adding-native-dependencies}
9899···108109109110The previous implementation of `lisp-modules` didn't fully trust the Quicklisp
110111data, because there were times where the dependencies specified were not
111111-complete, and caused broken builds. It instead used a `nix-shell` environment to
112112+complete and caused broken builds. It instead used a `nix-shell` environment to
112113discover real dependencies by using the ASDF APIs.
113114114115The current implementation has chosen to trust this data, because it's faster to
···126127127128During Quicklisp import:
128129129129-- `+` in names are converted to `_plus{_,}`: `cl+ssl`->`cl_plus_ssl`, `alexandria+`->`alexandria_plus`
130130-- `.` to `_dot_`: `iolib.base`->`iolib_dot_base`
130130+- `+` in names is converted to `_plus{_,}`: `cl+ssl`->`cl_plus_ssl`, `alexandria+`->`alexandria_plus`
131131+- `.` in names is converted to `_dot_`: `iolib.base`->`iolib_dot_base`
131132- names starting with a number have a `_` prepended (`3d-vectors`->`_3d-vectors`)
132133- `_` in names is converted to `__` for reversibility
133134134135135136## Defining packages manually inside Nixpkgs {#lisp-defining-packages-inside}
136137137137-New packages, that for some reason are not in Quicklisp, and so cannot be
138138-auto-imported, can be written in the `packages.nix` file.
138138+Packages that for some reason are not in Quicklisp, and so cannot be
139139+auto-imported, or don't work straight from the import, are defined in the
140140+`packages.nix` file.
139141140142In that file, use the `build-asdf-system` function, which is a wrapper around
141143`mkDerivation` for building ASDF systems. Various other hacks are present, such
142144as `build-with-compile-into-pwd` for systems which create files during
143143-compilation.
145145+compilation (such as cl-unicode).
144146145145-The `build-asdf-system` function is documented with comments in
146146-`nix-cl.nix`. Also, `packages.nix` is full of examples of how to use it.
147147+The `build-asdf-system` function is documented
148148+[here](#lisp-defining-packages-outside). Also, `packages.nix` is full of
149149+examples of how to use it.
147150148151## Defining packages manually outside Nixpkgs {#lisp-defining-packages-outside}
149152150153Lisp derivations (`abcl`, `sbcl` etc.) also export the `buildASDFSystem`
151151-function, which is the same as `build-asdf-system`, except for the `lisp`
152152-argument which is set to the given CL implementation.
154154+function, which is similar to `build-asdf-system` from `packages.nix`, but is
155155+part of the public API.
156156+157157+It takes the following arguments:
158158+159159+- `pname`: the package name
160160+- `version`: the package version
161161+- `src`: the package source
162162+- `patches`: patches to apply to the source before build
163163+- `nativeLibs`: native libraries used by CFFI and grovelling
164164+- `javaLibs`: Java libraries for ABCL
165165+- `lispLibs`: dependencies on other packages build with `buildASDFSystem`
166166+- `systems`: list of systems to build
153167154168It can be used to define packages outside Nixpkgs, and, for example, add them
155155-into the package scope with `withOverrides` which will be discussed later on.
169169+into the package scope with `withOverrides`.
156170157171### Including an external package in scope {#lisp-including-external-pkg-in-scope}
158172···198212})
199213```
200214201201-## Overriding packages in scope {#lisp-overriding-packages-in-scope}
202202-203203-Packages can be woven into a new scope by using `withOverrides`:
204204-205205-```
206206-let
207207- sbcl' = sbcl.withOverrides (self: super: {
208208- alexandria = super.alexandria.overrideLispAttrs (oldAttrs: rec {
209209- pname = "alexandria";
210210- version = "1.4";
211211- src = fetchFromGitLab {
212212- domain = "gitlab.common-lisp.net";
213213- owner = "alexandria";
214214- repo = "alexandria";
215215- rev = "v${version}";
216216- hash = "sha256-1Hzxt65dZvgOFIljjjlSGgKYkj+YBLwJCACi5DZsKmQ=";
217217- };
218218- });
219219- });
220220-in builtins.elemAt sbcl'.pkgs.bordeaux-threads.lispLibs 0
221221-```
222222-223215### Dealing with slashy systems {#lisp-dealing-with-slashy-systems}
224216225217Slashy (secondary) systems should not exist in their own packages! Instead, they
···240232})
241233```
242234243243-See the respective section on using `withOverrides` for how to weave it back
244244-into `ecl.pkgs`.
235235+See the [respective section](#lisp-including-external-pkg-in-scope) on using
236236+`withOverrides` for how to weave it back into `ecl.pkgs`.
245237246238Note that sometimes the slashy systems might not only have more dependencies
247239than the main one, but create a circular dependency between `.asd`
···253245implementations (`abcl`, `ecl`, `sbcl` etc.):
254246255247```
256256-sbcl.withPackages (ps: [ ps.alexandria ps.bordeaux-threads ])
248248+nix-shell -p 'sbcl.withPackages (ps: [ ps.alexandria ps.bordeaux-threads ])'
257249```
258250259259-Such a wrapper can then be executed like this:
251251+Such a wrapper can then be used like this:
260252261253```
262262-result/bin/sbcl
254254+$ sbcl
255255+* (load (sb-ext:posix-getenv "ASDF"))
256256+* (asdf:load-system 'alexandria)
257257+* (asdf:load-system 'bordeaux-threads)
263258```
264259265260### Loading ASDF {#lisp-loading-asdf}