2 @c This is part of the GNU Emacs Lisp Reference Manual.
3 @c Copyright (C) 2010-2018 Free Software Foundation, Inc.
4 @c See the file elisp.texi for copying conditions.
6 @chapter Preparing Lisp code for distribution
10 Emacs provides a standard way to distribute Emacs Lisp code to
11 users. A @dfn{package} is a collection of one or more files,
12 formatted and bundled in such a way that users can easily download,
13 install, uninstall, and upgrade it.
15 The following sections describe how to create a package, and how to
16 put it in a @dfn{package archive} for others to download.
17 @xref{Packages,,, emacs, The GNU Emacs Manual}, for a description of
18 user-level features of the packaging system.
21 * Packaging Basics:: The basic concepts of Emacs Lisp packages.
22 * Simple Packages:: How to package a single .el file.
23 * Multi-file Packages:: How to package multiple files.
24 * Package Archives:: Maintaining package archives.
27 @node Packaging Basics
28 @section Packaging Basics
29 @cindex package attributes
31 @cindex package version
33 @cindex package dependencies
35 A package is either a @dfn{simple package} or a @dfn{multi-file
36 package}. A simple package is stored in a package archive as a single
37 Emacs Lisp file, while a multi-file package is stored as a tar file
38 (containing multiple Lisp files, and possibly non-Lisp files such as a
41 In ordinary usage, the difference between simple packages and
42 multi-file packages is relatively unimportant; the Package Menu
43 interface makes no distinction between them. However, the procedure
44 for creating them differs, as explained in the following sections.
46 Each package (whether simple or multi-file) has certain
51 A short word (e.g., @samp{auctex}). This is usually also the symbol
52 prefix used in the program (@pxref{Coding Conventions}).
55 A version number, in a form that the function @code{version-to-list}
56 understands (e.g., @samp{11.86}). Each release of a package should be
57 accompanied by an increase in the version number so that it will be
58 recognized as an upgrade by users querying the package archive.
60 @item Brief description
61 This is shown when the package is listed in the Package Menu. It
62 should occupy a single line, ideally in 36 characters or less.
64 @item Long description
65 This is shown in the buffer created by @kbd{C-h P}
66 (@code{describe-package}), following the package's brief description
67 and installation status. It normally spans multiple lines, and should
68 fully describe the package's capabilities and how to begin using it
72 A list of other packages (possibly including minimal acceptable
73 version numbers) on which this package depends. The list may be
74 empty, meaning this package has no dependencies. Otherwise,
75 installing this package also automatically installs its dependencies,
76 recursively; if any dependency cannot be found, the package cannot be
80 @cindex content directory, package
81 Installing a package, either via the command @code{package-install-file},
82 or via the Package Menu, creates a subdirectory of
83 @code{package-user-dir} named @file{@var{name}-@var{version}}, where
84 @var{name} is the package's name and @var{version} its version
85 (e.g., @file{~/.emacs.d/elpa/auctex-11.86/}). We call this the
86 package's @dfn{content directory}. It is where Emacs puts the
87 package's contents (the single Lisp file for a simple package, or the
88 files extracted from a multi-file package).
90 @cindex package autoloads
91 Emacs then searches every Lisp file in the content directory for
92 autoload magic comments (@pxref{Autoload}). These autoload
93 definitions are saved to a file named @file{@var{name}-autoloads.el}
94 in the content directory. They are typically used to autoload the
95 principal user commands defined in the package, but they can also
96 perform other tasks, such as adding an element to
97 @code{auto-mode-alist} (@pxref{Auto Major Mode}). Note that a package
98 typically does @emph{not} autoload every function and variable defined
99 within it---only the handful of commands typically called to begin
100 using the package. Emacs then byte-compiles every Lisp file in the
103 After installation, the installed package is @dfn{loaded}: Emacs
104 adds the package's content directory to @code{load-path}, and
105 evaluates the autoload definitions in @file{@var{name}-autoloads.el}.
107 Whenever Emacs starts up, it automatically calls the function
108 @code{package-initialize} to load installed packages. This is done
109 after loading the init file and abbrev file (if any) and before
110 running @code{after-init-hook} (@pxref{Startup Summary}). Automatic
111 package loading is disabled if the user option
112 @code{package-enable-at-startup} is @code{nil}.
114 @deffn Command package-initialize &optional no-activate
115 This function initializes Emacs' internal record of which packages are
116 installed, and loads them. The user option @code{package-load-list}
117 specifies which packages to load; by default, all installed packages
118 are loaded. If called during startup, this function also sets
119 @code{package-enable-at-startup} to @code{nil}, to avoid accidentally
120 loading the packages twice. @xref{Package Installation,,, emacs, The
123 The optional argument @var{no-activate}, if non-@code{nil}, causes
124 Emacs to update its record of installed packages without actually
125 loading them; it is for internal use only.
128 @node Simple Packages
129 @section Simple Packages
130 @cindex single file package
131 @cindex simple package
133 A simple package consists of a single Emacs Lisp source file. The
134 file must conform to the Emacs Lisp library header conventions
135 (@pxref{Library Headers}). The package's attributes are taken from
136 the various headers, as illustrated by the following example:
140 ;;; superfrobnicator.el --- Frobnicate and bifurcate flanges
142 ;; Copyright (C) 2011 Free Software Foundation, Inc.
145 ;; Author: J. R. Hacker <jrh@@example.com>
147 ;; Package-Requires: ((flange "1.0"))
148 ;; Keywords: multimedia, frobnicate
149 ;; URL: http://example.com/jrhacker/superfrobnicate
155 ;; This package provides a minor mode to frobnicate and/or
156 ;; bifurcate any flanges you desire. To activate it, just type
160 (define-minor-mode superfrobnicator-mode
164 The name of the package is the same as the base name of the file, as
165 written on the first line. Here, it is @samp{superfrobnicator}.
167 The brief description is also taken from the first line. Here, it
168 is @samp{Frobnicate and bifurcate flanges}.
170 The version number comes from the @samp{Package-Version} header, if
171 it exists, or from the @samp{Version} header otherwise. One or the
172 other @emph{must} be present. Here, the version number is 1.3.
174 If the file has a @samp{;;; Commentary:} section, this section is
175 used as the long description. (When displaying the description, Emacs
176 omits the @samp{;;; Commentary:} line, as well as the leading comment
177 characters in the commentary itself.)
179 If the file has a @samp{Package-Requires} header, that is used as
180 the package dependencies. In the above example, the package depends
181 on the @samp{flange} package, version 1.0 or higher. @xref{Library
182 Headers}, for a description of the @samp{Package-Requires} header. If
183 the header is omitted, the package has no dependencies.
185 The @samp{Keywords} and @samp{URL} headers are optional, but recommended.
186 The command @code{describe-package} uses these to add links to its
187 output. The @samp{Keywords} header should contain at least one
188 standard keyword from the @code{finder-known-keywords} list.
190 The file ought to also contain one or more autoload magic comments,
191 as explained in @ref{Packaging Basics}. In the above example, a magic
192 comment autoloads @code{superfrobnicator-mode}.
194 @xref{Package Archives}, for a explanation of how to add a
195 single-file package to a package archive.
197 @node Multi-file Packages
198 @section Multi-file Packages
199 @cindex multi-file package
201 A multi-file package is less convenient to create than a single-file
202 package, but it offers more features: it can include multiple Emacs
203 Lisp files, an Info manual, and other file types (such as images).
205 Prior to installation, a multi-file package is stored in a package
206 archive as a tar file. The tar file must be named
207 @file{@var{name}-@var{version}.tar}, where @var{name} is the package
208 name and @var{version} is the version number. Its contents, once
209 extracted, must all appear in a directory named
210 @file{@var{name}-@var{version}}, the @dfn{content directory}
211 (@pxref{Packaging Basics}). Files may also extract into
212 subdirectories of the content directory.
214 One of the files in the content directory must be named
215 @file{@var{name}-pkg.el}. It must contain a single Lisp form,
216 consisting of a call to the function @code{define-package}, described
217 below. This defines the package's attributes: version, brief
218 description, and requirements.
220 For example, if we distribute version 1.3 of the superfrobnicator as
221 a multi-file package, the tar file would be
222 @file{superfrobnicator-1.3.tar}. Its contents would extract into the
223 directory @file{superfrobnicator-1.3}, and one of these would be the
224 file @file{superfrobnicator-pkg.el}.
226 @defun define-package name version &optional docstring requirements
227 This function defines a package. @var{name} is the package name, a
228 string. @var{version} is the version, as a string of a form that can
229 be understood by the function @code{version-to-list}. @var{docstring}
230 is the brief description.
232 @var{requirements} is a list of required packages and their versions.
233 Each element in this list should have the form @code{(@var{dep-name}
234 @var{dep-version})}, where @var{dep-name} is a symbol whose name is
235 the dependency's package name, and @var{dep-version} is the
236 dependency's version (a string).
239 If the content directory contains a file named @file{README}, this
240 file is used as the long description.
242 If the content directory contains a file named @file{dir}, this is
243 assumed to be an Info directory file made with @command{install-info}.
244 @xref{Invoking install-info, Invoking install-info, Invoking
245 install-info, texinfo, Texinfo}. The relevant Info files should also
246 be present in the content directory. In this case, Emacs will
247 automatically add the content directory to @code{Info-directory-list}
248 when the package is activated.
250 Do not include any @file{.elc} files in the package. Those are
251 created when the package is installed. Note that there is no way to
252 control the order in which files are byte-compiled.
254 Do not include any file named @file{@var{name}-autoloads.el}. This
255 file is reserved for the package's autoload definitions
256 (@pxref{Packaging Basics}). It is created automatically when the
257 package is installed, by searching all the Lisp files in the package
258 for autoload magic comments.
260 If the multi-file package contains auxiliary data files (such as
261 images), the package's Lisp code can refer to these files via the
262 variable @code{load-file-name} (@pxref{Loading}). Here is an example:
265 (defconst superfrobnicator-base (file-name-directory load-file-name))
267 (defun superfrobnicator-fetch-image (file)
268 (expand-file-name file superfrobnicator-base))
271 @node Package Archives
272 @section Creating and Maintaining Package Archives
273 @cindex package archive
275 Via the Package Menu, users may download packages from @dfn{package
276 archives}. Such archives are specified by the variable
277 @code{package-archives}, whose default value contains a single entry:
278 the archive hosted by the GNU project at @url{https://elpa.gnu.org}. This
279 section describes how to set up and maintain a package archive.
281 @cindex base location, package archive
282 @defopt package-archives
283 The value of this variable is an alist of package archives recognized
284 by the Emacs package manager.
286 Each alist element corresponds to one archive, and should have the
287 form @code{(@var{id} . @var{location})}, where @var{id} is the name of
288 the archive (a string) and @var{location} is its @dfn{base location}
291 If the base location starts with @samp{http:} or @samp{https:}, it
292 is treated as an HTTP(S) URL, and packages are downloaded from this
293 archive via HTTP(S) (as is the case for the default GNU archive).
295 Otherwise, the base location should be a directory name. In this
296 case, Emacs retrieves packages from this archive via ordinary file
297 access. Such local archives are mainly useful for testing.
300 A package archive is simply a directory in which the package files,
301 and associated files, are stored. If you want the archive to be
302 reachable via HTTP, this directory must be accessible to a web server.
303 How to accomplish this is beyond the scope of this manual.
305 A convenient way to set up and update a package archive is via the
306 @code{package-x} library. This is included with Emacs, but not loaded
307 by default; type @kbd{M-x load-library @key{RET} package-x @key{RET}} to
308 load it, or add @code{(require 'package-x)} to your init file.
309 @xref{Lisp Libraries,, Lisp Libraries, emacs, The GNU Emacs Manual}.
310 Once loaded, you can make use of the following:
312 @defopt package-archive-upload-base
313 The value of this variable is the base location of a package archive,
314 as a directory name. The commands in the @code{package-x} library
315 will use this base location.
317 The directory name should be absolute. You may specify a remote name,
318 such as @file{/ssh:foo@@example.com:/var/www/packages/}, if the
319 package archive is on a different machine. @xref{Remote Files,,
320 Remote Files, emacs, The GNU Emacs Manual}.
323 @deffn Command package-upload-file filename
324 This command prompts for @var{filename}, a file name, and uploads that
325 file to @code{package-archive-upload-base}. The file must be either a
326 simple package (a @file{.el} file) or a multi-file package (a
327 @file{.tar} file); otherwise, an error is raised. The package
328 attributes are automatically extracted, and the archive's contents
329 list is updated with this information.
331 If @code{package-archive-upload-base} does not specify a valid
332 directory, the function prompts interactively for one. If the
333 directory does not exist, it is created. The directory need not have
334 any initial contents (i.e., you can use this command to populate an
335 initially empty archive).
338 @deffn Command package-upload-buffer
339 This command is similar to @code{package-upload-file}, but instead of
340 prompting for a package file, it uploads the contents of the current
341 buffer. The current buffer must be visiting a simple package (a
342 @file{.el} file) or a multi-file package (a @file{.tar} file);
343 otherwise, an error is raised.
347 After you create an archive, remember that it is not accessible in the
348 Package Menu interface unless it is in @code{package-archives}.
350 @cindex package archive security
351 @cindex package signing
352 Maintaining a public package archive entails a degree of responsibility.
353 When Emacs users install packages from your archive, those packages
354 can cause Emacs to run arbitrary code with the permissions of the
355 installing user. (This is true for Emacs code in general, not just
356 for packages.) So you should ensure that your archive is
357 well-maintained and keep the hosting system secure.
359 One way to increase the security of your packages is to @dfn{sign}
360 them using a cryptographic key. If you have generated a
361 private/public gpg key pair, you can use gpg to sign the package like
364 @c FIXME EasyPG / package-x way to do this.
366 gpg -ba -o @var{file}.sig @var{file}
370 For a single-file package, @var{file} is the package Lisp file;
371 for a multi-file package, it is the package tar file.
372 You can also sign the archive's contents file in the same way.
373 Make the @file{.sig} files available in the same location as the packages.
374 You should also make your public key available for people to download;
375 e.g., by uploading it to a key server such as @url{http://pgp.mit.edu/}.
376 When people install packages from your archive, they can use
377 your public key to verify the signatures.
379 A full explanation of these matters is outside the scope of this
380 manual. For more information on cryptographic keys and signing,
381 @pxref{Top,, GnuPG, gnupg, The GNU Privacy Guard Manual}. Emacs comes
382 with an interface to GNU Privacy Guard, @pxref{Top,, EasyPG, epa,
383 Emacs EasyPG Assistant Manual}.