Skip to content
Snippets Groups Projects
guix.texi 844 KiB
Newer Older
  • Learn to ignore specific revisions
  • Ludovic Courtès's avatar
    Ludovic Courtès committed
    @cindex G-expression
    @cindex build code quoting
    So we have ``derivations'', which represent a sequence of build actions
    to be performed to produce an item in the store (@pxref{Derivations}).
    
    Andreas Enge's avatar
    Andreas Enge committed
    These build actions are performed when asking the daemon to actually
    
    Ludovic Courtès's avatar
    Ludovic Courtès committed
    build the derivations; they are run by the daemon in a container
    (@pxref{Invoking guix-daemon}).
    
    @cindex strata of code
    
    Andreas Enge's avatar
    Andreas Enge committed
    It should come as no surprise that we like to write these build actions
    
    Ludovic Courtès's avatar
    Ludovic Courtès committed
    in Scheme.  When we do that, we end up with two @dfn{strata} of Scheme
    code@footnote{The term @dfn{stratum} in this context was coined by
    
    Manuel Serrano et al.@: in the context of their work on Hop.  Oleg
    Kiselyov, who has written insightful
    @url{http://okmij.org/ftp/meta-programming/#meta-scheme, essays and code
    on this topic}, refers to this kind of code generation as
    @dfn{staging}.}: the ``host code''---code that defines packages, talks
    to the daemon, etc.---and the ``build code''---code that actually
    performs build actions, such as making directories, invoking
    @command{make}, etc.
    
    Ludovic Courtès's avatar
    Ludovic Courtès committed
    
    To describe a derivation and its build actions, one typically needs to
    embed build code inside host code.  It boils down to manipulating build
    
    Andreas Enge's avatar
    Andreas Enge committed
    code as data, and the homoiconicity of Scheme---code has a direct
    
    Ludovic Courtès's avatar
    Ludovic Courtès committed
    representation as data---comes in handy for that.  But we need more than
    
    Andreas Enge's avatar
    Andreas Enge committed
    the normal @code{quasiquote} mechanism in Scheme to construct build
    
    Ludovic Courtès's avatar
    Ludovic Courtès committed
    expressions.
    
    The @code{(guix gexp)} module implements @dfn{G-expressions}, a form of
    S-expressions adapted to build expressions.  G-expressions, or
    
    Andreas Enge's avatar
    Andreas Enge committed
    @dfn{gexps}, consist essentially of three syntactic forms: @code{gexp},
    
    Ludovic Courtès's avatar
    Ludovic Courtès committed
    @code{ungexp}, and @code{ungexp-splicing} (or simply: @code{#~},
    
    Andreas Enge's avatar
    Andreas Enge committed
    @code{#$}, and @code{#$@@}), which are comparable to
    @code{quasiquote}, @code{unquote}, and @code{unquote-splicing},
    
    respectively (@pxref{Expression Syntax, @code{quasiquote},, guile,
    
    Andreas Enge's avatar
    Andreas Enge committed
    GNU Guile Reference Manual}).  However, there are major differences:
    
    Ludovic Courtès's avatar
    Ludovic Courtès committed
    
    @itemize
    @item
    Gexps are meant to be written to a file and run or manipulated by other
    processes.
    
    @item
    
    When a high-level object such as a package or derivation is unquoted
    inside a gexp, the result is as if its output file name had been
    introduced.
    
    Ludovic Courtès's avatar
    Ludovic Courtès committed
    @item
    Gexps carry information about the packages or derivations they refer to,
    and these dependencies are automatically added as inputs to the build
    processes that use them.
    @end itemize
    
    
    @cindex lowering, of high-level objects in gexps
    
    This mechanism is not limited to package and derivation
    objects: @dfn{compilers} able to ``lower'' other high-level objects to
    
    derivations or files in the store can be defined,
    such that these objects can also be inserted
    
    Andreas Enge's avatar
    Andreas Enge committed
    into gexps.  For example, a useful type of high-level objects that can be
    
    inserted in a gexp is ``file-like objects'', which make it easy to
    
    Andreas Enge's avatar
    Andreas Enge committed
    add files to the store and to refer to them in
    
    derivations and such (see @code{local-file} and @code{plain-file}
    below.)
    
    Ludovic Courtès's avatar
    Ludovic Courtès committed
    To illustrate the idea, here is an example of a gexp:
    
    @example
    (define build-exp
      #~(begin
          (mkdir #$output)
          (chdir #$output)
    
          (symlink (string-append #$coreutils "/bin/ls")
    
    Ludovic Courtès's avatar
    Ludovic Courtès committed
                   "list-files")))
    @end example
    
    This gexp can be passed to @code{gexp->derivation}; we obtain a
    derivation that builds a directory containing exactly one symlink to
    @file{/gnu/store/@dots{}-coreutils-8.22/bin/ls}:
    
    @example
    (gexp->derivation "the-thing" build-exp)
    @end example
    
    
    As one would expect, the @code{"/gnu/store/@dots{}-coreutils-8.22"} string is
    
    Ludovic Courtès's avatar
    Ludovic Courtès committed
    substituted to the reference to the @var{coreutils} package in the
    actual build code, and @var{coreutils} is automatically made an input to
    the derivation.  Likewise, @code{#$output} (equivalent to @code{(ungexp
    
    Andreas Enge's avatar
    Andreas Enge committed
    output)}) is replaced by a string containing the directory name of the
    output of the derivation.
    
    
    @cindex cross compilation
    In a cross-compilation context, it is useful to distinguish between
    references to the @emph{native} build of a package---that can run on the
    host---versus references to cross builds of a package.  To that end, the
    @code{#+} plays the same role as @code{#$}, but is a reference to a
    native package build:
    
    @example
    (gexp->derivation "vi"
       #~(begin
           (mkdir #$output)
           (system* (string-append #+coreutils "/bin/ln")
                    "-s"
                    (string-append #$emacs "/bin/emacs")
                    (string-append #$output "/bin/vi")))
    
       #:target "mips64el-linux-gnu")
    
    @end example
    
    @noindent
    In the example above, the native build of @var{coreutils} is used, so
    that @command{ln} can actually run on the host; but then the
    cross-compiled build of @var{emacs} is referenced.
    
    
    @cindex imported modules, for gexps
    @findex with-imported-modules
    Another gexp feature is @dfn{imported modules}: sometimes you want to be
    able to use certain Guile modules from the ``host environment'' in the
    gexp, so those modules should be imported in the ``build environment''.
    The @code{with-imported-modules} form allows you to express that:
    
    @example
    (let ((build (with-imported-modules '((guix build utils))
                   #~(begin
                       (use-modules (guix build utils))
                       (mkdir-p (string-append #$output "/bin"))))))
      (gexp->derivation "empty-dir"
                        #~(begin
                            #$build
                            (display "success!\n")
                            #t)))
    @end example
    
    @noindent
    In this example, the @code{(guix build utils)} module is automatically
    pulled into the isolated build environment of our gexp, such that
    @code{(use-modules (guix build utils))} works as expected.
    
    
    Ludovic Courtès's avatar
    Ludovic Courtès committed
    @cindex module closure
    @findex source-module-closure
    Usually you want the @emph{closure} of the module to be imported---i.e.,
    the module itself and all the modules it depends on---rather than just
    the module; failing to do that, attempts to use the module will fail
    because of missing dependent modules.  The @code{source-module-closure}
    procedure computes the closure of a module by looking at its source file
    headers, which comes in handy in this case:
    
    @example
    (use-modules (guix modules))   ;for 'source-module-closure'
    
    (with-imported-modules (source-module-closure
                             '((guix build utils)
                               (gnu build vm)))
      (gexp->derivation "something-with-vms"
                        #~(begin
                            (use-modules (guix build utils)
                                         (gnu build vm))
                            @dots{})))
    @end example
    
    
    @cindex extensions, for gexps
    @findex with-extensions
    In the same vein, sometimes you want to import not just pure-Scheme
    modules, but also ``extensions'' such as Guile bindings to C libraries
    or other ``full-blown'' packages.  Say you need the @code{guile-json}
    package available on the build side, here's how you would do it:
    
    @example
    (use-modules (gnu packages guile))  ;for 'guile-json'
    
    (with-extensions (list guile-json)
      (gexp->derivation "something-with-json"
                        #~(begin
                            (use-modules (json))
                            @dots{})))
    @end example
    
    
    The syntactic form to construct gexps is summarized below.
    
    Ludovic Courtès's avatar
    Ludovic Courtès committed
    
    @deffn {Scheme Syntax} #~@var{exp}
    @deffnx {Scheme Syntax} (gexp @var{exp})
    Return a G-expression containing @var{exp}.  @var{exp} may contain one
    or more of the following forms:
    
    @table @code
    @item #$@var{obj}
    @itemx (ungexp @var{obj})
    
    Introduce a reference to @var{obj}.  @var{obj} may have one of the
    supported types, for example a package or a
    
    Ludovic Courtès's avatar
    Ludovic Courtès committed
    derivation, in which case the @code{ungexp} form is replaced by its
    output file name---e.g., @code{"/gnu/store/@dots{}-coreutils-8.22}.
    
    
    If @var{obj} is a list, it is traversed and references to supported
    objects are substituted similarly.
    
    Ludovic Courtès's avatar
    Ludovic Courtès committed
    
    If @var{obj} is another gexp, its contents are inserted and its
    dependencies are added to those of the containing gexp.
    
    If @var{obj} is another kind of object, it is inserted as is.
    
    
    @item #$@var{obj}:@var{output}
    @itemx (ungexp @var{obj} @var{output})
    
    Ludovic Courtès's avatar
    Ludovic Courtès committed
    This is like the form above, but referring explicitly to the
    
    @var{output} of @var{obj}---this is useful when @var{obj} produces
    multiple outputs (@pxref{Packages with Multiple Outputs}).
    
    @item #+@var{obj}
    @itemx #+@var{obj}:output
    @itemx (ungexp-native @var{obj})
    @itemx (ungexp-native @var{obj} @var{output})
    Same as @code{ungexp}, but produces a reference to the @emph{native}
    build of @var{obj} when used in a cross compilation context.
    
    
    Ludovic Courtès's avatar
    Ludovic Courtès committed
    @item #$output[:@var{output}]
    @itemx (ungexp output [@var{output}])
    Insert a reference to derivation output @var{output}, or to the main
    output when @var{output} is omitted.
    
    This only makes sense for gexps passed to @code{gexp->derivation}.
    
    @item #$@@@var{lst}
    @itemx (ungexp-splicing @var{lst})
    Like the above, but splices the contents of @var{lst} inside the
    containing list.
    
    
    @item #+@@@var{lst}
    @itemx (ungexp-native-splicing @var{lst})
    Like the above, but refers to native builds of the objects listed in
    @var{lst}.
    
    
    Ludovic Courtès's avatar
    Ludovic Courtès committed
    @end table
    
    G-expressions created by @code{gexp} or @code{#~} are run-time objects
    of the @code{gexp?} type (see below.)
    @end deffn
    
    
    @deffn {Scheme Syntax} with-imported-modules @var{modules} @var{body}@dots{}
    Mark the gexps defined in @var{body}@dots{} as requiring @var{modules}
    
    in their execution environment.
    
    Each item in @var{modules} can be the name of a module, such as
    @code{(guix build utils)}, or it can be a module name, followed by an
    arrow, followed by a file-like object:
    
    @example
    `((guix build utils)
      (guix gcrypt)
      ((guix config) => ,(scheme-file "config.scm"
                                      #~(define-module @dots{}))))
    @end example
    
    @noindent
    In the example above, the first two modules are taken from the search
    path, and the last one is created from the given file-like object.
    
    
    This form has @emph{lexical} scope: it has an effect on the gexps
    directly defined in @var{body}@dots{}, but not on those defined, say, in
    procedures called from @var{body}@dots{}.
    @end deffn
    
    
    @deffn {Scheme Syntax} with-extensions @var{extensions} @var{body}@dots{}
    Mark the gexps defined in @var{body}@dots{} as requiring
    @var{extensions} in their build and execution environment.
    @var{extensions} is typically a list of package objects such as those
    defined in the @code{(gnu packages guile)} module.
    
    Concretely, the packages listed in @var{extensions} are added to the
    load path while compiling imported modules in @var{body}@dots{}; they
    are also added to the load path of the gexp returned by
    @var{body}@dots{}.
    @end deffn
    
    
    Ludovic Courtès's avatar
    Ludovic Courtès committed
    @deffn {Scheme Procedure} gexp? @var{obj}
    Return @code{#t} if @var{obj} is a G-expression.
    @end deffn
    
    G-expressions are meant to be written to disk, either as code building
    some derivation, or as plain files in the store.  The monadic procedures
    below allow you to do that (@pxref{The Store Monad}, for more
    information about monads.)
    
    @deffn {Monadic Procedure} gexp->derivation @var{name} @var{exp} @
    
           [#:system (%current-system)] [#:target #f] [#:graft? #t] @
    
    Ludovic Courtès's avatar
    Ludovic Courtès committed
           [#:hash #f] [#:hash-algo #f] @
           [#:recursive? #f] [#:env-vars '()] [#:modules '()] @
    
           [#:module-path @var{%load-path}] @
    
           [#:effective-version "2.2"] @
    
           [#:references-graphs #f] [#:allowed-references #f] @
    
           [#:disallowed-references #f] @
    
           [#:leaked-env-vars #f] @
    
           [#:script-name (string-append @var{name} "-builder")] @
    
           [#:local-build? #f] [#:substitutable? #t] [#:guile-for-build #f]
    
    Ludovic Courtès's avatar
    Ludovic Courtès committed
    Return a derivation @var{name} that runs @var{exp} (a gexp) with
    
    @var{guile-for-build} (a derivation) on @var{system}; @var{exp} is
    stored in a file called @var{script-name}.  When @var{target} is true,
    it is used as the cross-compilation target triplet for packages referred
    to by @var{exp}.
    
    @var{modules} is deprecated in favor of @code{with-imported-modules}.
    Its meaning is to
    make @var{modules} available in the evaluation context of @var{exp};
    
    @var{modules} is a list of names of Guile modules searched in
    @var{module-path} to be copied in the store, compiled, and made available in
    
    Ludovic Courtès's avatar
    Ludovic Courtès committed
    the load path during the execution of @var{exp}---e.g., @code{((guix
    build utils) (guix build gnu-build-system))}.
    
    
    @var{effective-version} determines the string to use when adding extensions of
    @var{exp} (see @code{with-extensions}) to the search path---e.g., @code{"2.2"}.
    
    
    @var{graft?} determines whether packages referred to by @var{exp} should be grafted when
    applicable.
    
    
    When @var{references-graphs} is true, it must be a list of tuples of one of the
    following forms:
    
    @example
    (@var{file-name} @var{package})
    (@var{file-name} @var{package} @var{output})
    (@var{file-name} @var{derivation})
    (@var{file-name} @var{derivation} @var{output})
    (@var{file-name} @var{store-item})
    @end example
    
    The right-hand-side of each element of @var{references-graphs} is automatically made
    an input of the build process of @var{exp}.  In the build environment, each
    @var{file-name} contains the reference graph of the corresponding item, in a simple
    text format.
    
    
    @var{allowed-references} must be either @code{#f} or a list of output names and packages.
    In the latter case, the list denotes store items that the result is allowed to
    refer to.  Any reference to another store item will lead to a build error.
    
    Similarly for @var{disallowed-references}, which can list items that must not be
    referenced by the outputs.
    
    @var{deprecation-warnings} determines whether to show deprecation warnings while
    compiling modules.  It can be @code{#f}, @code{#t}, or @code{'detailed}.
    
    
    The other arguments are as for @code{derivation} (@pxref{Derivations}).
    
    Ludovic Courtès's avatar
    Ludovic Courtès committed
    @end deffn
    
    
    @cindex file-like objects
    
    The @code{local-file}, @code{plain-file}, @code{computed-file},
    @code{program-file}, and @code{scheme-file} procedures below return
    @dfn{file-like objects}.  That is, when unquoted in a G-expression,
    these objects lead to a file in the store.  Consider this G-expression:
    
    #~(system* #$(file-append glibc "/sbin/nscd") "-f"
    
               #$(local-file "/tmp/my-nscd.conf"))
    @end example
    
    The effect here is to ``intern'' @file{/tmp/my-nscd.conf} by copying it
    to the store.  Once expanded, for instance @i{via}
    @code{gexp->derivation}, the G-expression refers to that copy under
    @file{/gnu/store}; thus, modifying or removing the file in @file{/tmp}
    does not have any effect on what the G-expression does.
    @code{plain-file} can be used similarly; it differs in that the file
    content is directly passed as a string.
    
    
    @deffn {Scheme Procedure} local-file @var{file} [@var{name}] @
    
       [#:recursive? #f] [#:select? (const #t)]
    
    Return an object representing local file @var{file} to add to the store; this
    
    object can be used in a gexp.  If @var{file} is a relative file name, it is looked
    up relative to the source file where this form appears.  @var{file} will be added to
    the store under @var{name}--by default the base name of @var{file}.
    
    
    When @var{recursive?} is true, the contents of @var{file} are added recursively; if @var{file}
    designates a flat file and @var{recursive?} is true, its contents are added, and its
    permission bits are kept.
    
    
    When @var{recursive?} is true, call @code{(@var{select?} @var{file}
    @var{stat})} for each directory entry, where @var{file} is the entry's
    absolute file name and @var{stat} is the result of @code{lstat}; exclude
    entries for which @var{select?} does not return true.
    
    
    This is the declarative counterpart of the @code{interned-file} monadic
    procedure (@pxref{The Store Monad, @code{interned-file}}).
    @end deffn
    
    
    @deffn {Scheme Procedure} plain-file @var{name} @var{content}
    Return an object representing a text file called @var{name} with the given
    
    @var{content} (a string or a bytevector) to be added to the store.
    
    
    This is the declarative counterpart of @code{text-file}.
    @end deffn
    
    
    @deffn {Scheme Procedure} computed-file @var{name} @var{gexp} @
    
              [#:options '(#:local-build? #t)]
    
    Return an object representing the store item @var{name}, a file or
    
    directory computed by @var{gexp}.  @var{options}
    
    is a list of additional arguments to pass to @code{gexp->derivation}.
    
    This is the declarative counterpart of @code{gexp->derivation}.
    @end deffn
    
    
    @deffn {Monadic Procedure} gexp->script @var{name} @var{exp} @
      [#:guile (default-guile)] [#:module-path %load-path]
    
    Ludovic Courtès's avatar
    Ludovic Courtès committed
    Return an executable script @var{name} that runs @var{exp} using
    
    @var{guile}, with @var{exp}'s imported modules in its search path.
    
    Look up @var{exp}'s modules in @var{module-path}.
    
    Ludovic Courtès's avatar
    Ludovic Courtès committed
    
    The example below builds a script that simply invokes the @command{ls}
    command:
    
    @example
    (use-modules (guix gexp) (gnu packages base))
    
    (gexp->script "list-files"
    
                  #~(execl #$(file-append coreutils "/bin/ls")
    
    Ludovic Courtès's avatar
    Ludovic Courtès committed
                           "ls"))
    @end example
    
    When ``running'' it through the store (@pxref{The Store Monad,
    
    @code{run-with-store}}), we obtain a derivation that produces an
    
    Ludovic Courtès's avatar
    Ludovic Courtès committed
    executable file @file{/gnu/store/@dots{}-list-files} along these lines:
    
    @example
    #!/gnu/store/@dots{}-guile-2.0.11/bin/guile -ds
    !#
    
    (execl "/gnu/store/@dots{}-coreutils-8.22"/bin/ls" "ls")
    
    Ludovic Courtès's avatar
    Ludovic Courtès committed
    @end example
    @end deffn
    
    
    @deffn {Scheme Procedure} program-file @var{name} @var{exp} @
    
              [#:guile #f] [#:module-path %load-path]
    
    Return an object representing the executable store item @var{name} that
    runs @var{gexp}.  @var{guile} is the Guile package used to execute that
    
    script.  Imported modules of @var{gexp} are looked up in @var{module-path}.
    
    
    This is the declarative counterpart of @code{gexp->script}.
    @end deffn
    
    
    @deffn {Monadic Procedure} gexp->file @var{name} @var{exp} @
    
                [#:set-load-path? #t] [#:module-path %load-path] @
    
    Ludovic Courtès's avatar
    Ludovic Courtès committed
    Return a derivation that builds a file @var{name} containing @var{exp}.
    
    When @var{splice?}  is true, @var{exp} is considered to be a list of
    expressions that will be spliced in the resulting file.
    
    
    When @var{set-load-path?} is true, emit code in the resulting file to
    set @code{%load-path} and @code{%load-compiled-path} to honor
    
    @var{exp}'s imported modules.  Look up @var{exp}'s modules in
    @var{module-path}.
    
    Ludovic Courtès's avatar
    Ludovic Courtès committed
    
    The resulting file holds references to all the dependencies of @var{exp}
    or a subset thereof.
    @end deffn
    
    @deffn {Scheme Procedure} scheme-file @var{name} @var{exp} [#:splice? #f]
    
    Return an object representing the Scheme file @var{name} that contains
    @var{exp}.
    
    This is the declarative counterpart of @code{gexp->file}.
    @end deffn
    
    
    @deffn {Monadic Procedure} text-file* @var{name} @var{text} @dots{}
    Return as a monadic value a derivation that builds a text file
    containing all of @var{text}.  @var{text} may list, in addition to
    
    strings, objects of any type that can be used in a gexp: packages,
    derivations, local file objects, etc.  The resulting store file holds
    references to all these.
    
    
    This variant should be preferred over @code{text-file} anytime the file
    to create will reference items from the store.  This is typically the
    case when building a configuration file that embeds store file names,
    like this:
    
    @example
    (define (profile.sh)
      ;; Return the name of a shell script in the store that
      ;; initializes the 'PATH' environment variable.
      (text-file* "profile.sh"
                  "export PATH=" coreutils "/bin:"
                  grep "/bin:" sed "/bin\n"))
    @end example
    
    In this example, the resulting @file{/gnu/store/@dots{}-profile.sh} file
    
    Ludovic Courtès's avatar
    Ludovic Courtès committed
    will reference @var{coreutils}, @var{grep}, and @var{sed}, thereby
    
    preventing them from being garbage-collected during its lifetime.
    @end deffn
    
    @deffn {Scheme Procedure} mixed-text-file @var{name} @var{text} @dots{}
    Return an object representing store file @var{name} containing
    @var{text}.  @var{text} is a sequence of strings and file-like objects,
    as in:
    
    @example
    (mixed-text-file "profile"
                     "export PATH=" coreutils "/bin:" grep "/bin")
    @end example
    
    This is the declarative counterpart of @code{text-file*}.
    @end deffn
    
    
    @deffn {Scheme Procedure} file-union @var{name} @var{files}
    Return a @code{<computed-file>} that builds a directory containing all of @var{files}.
    Each item in @var{files} must be a two-element list where the first element is the
    file name to use in the new directory, and the second element is a gexp
    denoting the target file.  Here's an example:
    
    @example
    (file-union "etc"
                `(("hosts" ,(plain-file "hosts"
                                        "127.0.0.1 localhost"))
                  ("bashrc" ,(plain-file "bashrc"
    
                                         "alias ls='ls --color=auto'"))))
    
    @end example
    
    This yields an @code{etc} directory containing these two files.
    @end deffn
    
    
    @deffn {Scheme Procedure} directory-union @var{name} @var{things}
    Return a directory that is the union of @var{things}, where @var{things} is a list of
    file-like objects denoting directories.  For example:
    
    @example
    (directory-union "guile+emacs" (list guile emacs))
    @end example
    
    yields a directory that is the union of the @code{guile} and @code{emacs} packages.
    @end deffn
    
    
    @deffn {Scheme Procedure} file-append @var{obj} @var{suffix} @dots{}
    Return a file-like object that expands to the concatenation of @var{obj}
    and @var{suffix}, where @var{obj} is a lowerable object and each
    @var{suffix} is a string.
    
    As an example, consider this gexp:
    
    @example
    (gexp->script "run-uname"
                  #~(system* #$(file-append coreutils
                                            "/bin/uname")))
    @end example
    
    The same effect could be achieved with:
    
    @example
    (gexp->script "run-uname"
                  #~(system* (string-append #$coreutils
                                            "/bin/uname")))
    @end example
    
    There is one difference though: in the @code{file-append} case, the
    resulting script contains the absolute file name as a string, whereas in
    the second case, the resulting script contains a @code{(string-append
    @dots{})} expression to construct the file name @emph{at run time}.
    @end deffn
    
    
    
    Ludovic Courtès's avatar
    Ludovic Courtès committed
    Of course, in addition to gexps embedded in ``host'' code, there are
    also modules containing build tools.  To make it clear that they are
    meant to be used in the build stratum, these modules are kept in the
    @code{(guix build @dots{})} name space.
    
    
    @cindex lowering, of high-level objects in gexps
    Internally, high-level objects are @dfn{lowered}, using their compiler,
    to either derivations or store items.  For instance, lowering a package
    yields a derivation, and lowering a @code{plain-file} yields a store
    item.  This is achieved using the @code{lower-object} monadic procedure.
    
    @deffn {Monadic Procedure} lower-object @var{obj} [@var{system}] @
               [#:target #f]
    Return as a value in @var{%store-monad} the derivation or store item
    corresponding to @var{obj} for @var{system}, cross-compiling for
    @var{target} if @var{target} is true.  @var{obj} must be an object that
    has an associated gexp compiler, such as a @code{<package>}.
    @end deffn
    
    
    @node Invoking guix repl
    @section Invoking @command{guix repl}
    
    @cindex REPL, read-eval-print loop
    The @command{guix repl} command spawns a Guile @dfn{read-eval-print loop}
    (REPL) for interactive programming (@pxref{Using Guile Interactively,,, guile,
    GNU Guile Reference Manual}).  Compared to just launching the @command{guile}
    command, @command{guix repl} guarantees that all the Guix modules and all its
    dependencies are available in the search path.  You can use it this way:
    
    @example
    $ guix repl
    scheme@@(guile-user)> ,use (gnu packages base)
    scheme@@(guile-user)> coreutils
    $1 = #<package coreutils@@8.29 gnu/packages/base.scm:327 3e28300>
    @end example
    
    @cindex inferiors
    In addition, @command{guix repl} implements a simple machine-readable REPL
    protocol for use by @code{(guix inferior)}, a facility to interact with
    @dfn{inferiors}, separate processes running a potentially different revision
    of Guix.
    
    The available options are as follows:
    
    @table @code
    @item --type=@var{type}
    @itemx -t @var{type}
    Start a REPL of the given @var{TYPE}, which can be one of the following:
    
    @table @code
    @item guile
    This is default, and it spawns a standard full-featured Guile REPL.
    @item machine
    Spawn a REPL that uses the machine-readable protocol.  This is the protocol
    that the @code{(guix inferior)} module speaks.
    @end table
    
    @item --listen=@var{endpoint}
    By default, @command{guix repl} reads from standard input and writes to
    standard output.  When this option is passed, it will instead listen for
    connections on @var{endpoint}.  Here are examples of valid options:
    
    @table @code
    @item --listen=tcp:37146
    Accept connections on localhost on port 37146.
    
    @item --listen=unix:/tmp/socket
    Accept connections on the Unix-domain socket @file{/tmp/socket}.
    @end table
    @end table
    
    @c *********************************************************************
    @node Utilities
    @chapter Utilities
    
    
    This section describes Guix command-line utilities.  Some of them are
    primarily targeted at developers and users who write new package
    definitions, while others are more generally useful.  They complement
    the Scheme programming interface of Guix in a convenient way.
    
    * Invoking guix build::         Building packages from the command line.
    
    Ludovic Courtès's avatar
    Ludovic Courtès committed
    * Invoking guix edit::          Editing package definitions.
    
    * Invoking guix download::      Downloading a file and printing its hash.
    
    * Invoking guix hash::          Computing the cryptographic hash of a file.
    
    * Invoking guix import::        Importing package definitions.
    
    * Invoking guix refresh::       Updating package definitions.
    
    Cyril Roelandt's avatar
    Cyril Roelandt committed
    * Invoking guix lint::          Finding errors in package definitions.
    
    Ludovic Courtès's avatar
    Ludovic Courtès committed
    * Invoking guix size::          Profiling disk usage.
    
    Ludovic Courtès's avatar
    Ludovic Courtès committed
    * Invoking guix graph::         Visualizing the graph of packages.
    
    * Invoking guix environment::   Setting up development environments.
    
    * Invoking guix publish::       Sharing substitutes.
    
    Ludovic Courtès's avatar
    Ludovic Courtès committed
    * Invoking guix challenge::     Challenging substitute servers.
    
    Ludovic Courtès's avatar
    Ludovic Courtès committed
    * Invoking guix copy::          Copying to and from a remote store.
    
    * Invoking guix container::     Process isolation.
    
    Ludovic Courtès's avatar
    Ludovic Courtès committed
    * Invoking guix weather::       Assessing substitute availability.
    
    @node Invoking guix build
    @section Invoking @command{guix build}
    
    @cindex package building
    @cindex @command{guix build}
    
    The @command{guix build} command builds packages or derivations and
    
    their dependencies, and prints the resulting store paths.  Note that it
    does not modify the user's profile---this is the job of the
    
    @command{guix package} command (@pxref{Invoking guix package}).  Thus,
    
    it is mainly useful for distribution developers.
    
    The general syntax is:
    
    guix build @var{options} @var{package-or-derivation}@dots{}
    
    @end example
    
    
    Andreas Enge's avatar
    Andreas Enge committed
    As an example, the following command builds the latest versions of Emacs
    
    and of Guile, displays their build logs, and finally displays the
    resulting directories:
    
    @example
    guix build emacs guile
    @end example
    
    Similarly, the following command builds all the available packages:
    
    @example
    
    guix build --quiet --keep-going \
    
      `guix package -A | cut -f1,2 --output-delimiter=@@`
    @end example
    
    
    @var{package-or-derivation} may be either the name of a package found in
    
    the software distribution such as @code{coreutils} or
    
    Alex Kost's avatar
    Alex Kost committed
    @code{coreutils@@8.20}, or a derivation such as
    
    @file{/gnu/store/@dots{}-coreutils-8.19.drv}.  In the former case, a
    
    package with the corresponding name (and optionally version) is searched
    for among the GNU distribution modules (@pxref{Package Modules}).
    
    Alternatively, the @code{--expression} option may be used to specify a
    Scheme expression that evaluates to a package; this is useful when
    
    Ludovic Courtès's avatar
    Ludovic Courtès committed
    disambiguating among several same-named packages or package variants is
    
    There may be zero or more @var{options}.  The available options are
    described in the subsections below.
    
    @menu
    * Common Build Options::        Build options for most commands.
    
    Ludovic Courtès's avatar
    Ludovic Courtès committed
    * Package Transformation Options::  Creating variants of packages.
    
    * Additional Build Options::    Options specific to 'guix build'.
    
    * Debugging Build Failures::    Real life packaging experience.
    
    @end menu
    
    @node Common Build Options
    @subsection Common Build Options
    
    A number of options that control the build process are common to
    @command{guix build} and other commands that can spawn builds, such as
    @command{guix package} or @command{guix archive}.  These are the
    following:
    
    @table @code
    
    @item --load-path=@var{directory}
    @itemx -L @var{directory}
    Add @var{directory} to the front of the package module search path
    (@pxref{Package Modules}).
    
    This allows users to define their own packages and make them visible to
    the command-line tools.
    
    @item --keep-failed
    @itemx -K
    
    Ludovic Courtès's avatar
    Ludovic Courtès committed
    Keep the build tree of failed builds.  Thus, if a build fails, its build
    
    tree is kept under @file{/tmp}, in a directory whose name is shown at
    the end of the build log.  This is useful when debugging build issues.
    
    @xref{Debugging Build Failures}, for tips and tricks on how to debug
    build issues.
    
    
    @item --keep-going
    @itemx -k
    Keep going when some of the derivations fail to build; return only once
    all the builds have either completed or failed.
    
    The default behavior is to stop as soon as one of the specified
    derivations has failed.
    
    @item --dry-run
    @itemx -n
    Do not build the derivations.
    
    
    @item --fallback
    When substituting a pre-built binary fails, fall back to building
    
    packages locally (@pxref{Substitution Failure}).
    
    
    @item --substitute-urls=@var{urls}
    @anchor{client-substitute-urls}
    Consider @var{urls} the whitespace-separated list of substitute source
    URLs, overriding the default list of URLs of @command{guix-daemon}
    (@pxref{daemon-substitute-urls,, @command{guix-daemon} URLs}).
    
    This means that substitutes may be downloaded from @var{urls}, provided
    they are signed by a key authorized by the system administrator
    (@pxref{Substitutes}).
    
    
    When @var{urls} is the empty string, substitutes are effectively
    disabled.
    
    
    @item --no-substitutes
    Do not use substitutes for build products.  That is, always build things
    locally instead of allowing downloads of pre-built binaries
    (@pxref{Substitutes}).
    
    
    @item --no-grafts
    Do not ``graft'' packages.  In practice, this means that package updates
    available as grafts are not applied.  @xref{Security Updates}, for more
    information on grafts.
    
    
    @item --rounds=@var{n}
    Build each derivation @var{n} times in a row, and raise an error if
    consecutive build results are not bit-for-bit identical.
    
    This is a useful way to detect non-deterministic builds processes.
    Non-deterministic build processes are a problem because they make it
    practically impossible for users to @emph{verify} whether third-party
    binaries are genuine.  @xref{Invoking guix challenge}, for more.
    
    Note that, currently, the differing build results are not kept around,
    so you will have to manually investigate in case of an error---e.g., by
    
    stashing one of the build results with @code{guix archive --export}
    (@pxref{Invoking guix archive}), then rebuilding, and finally comparing
    the two results.
    
    Andreas Enge's avatar
    Andreas Enge committed
    Do not attempt to offload builds @i{via} the ``build hook'' of the daemon
    
    (@pxref{Daemon Offload Setup}).  That is, always build things locally
    instead of offloading builds to remote machines.
    
    @item --max-silent-time=@var{seconds}
    When the build or substitution process remains silent for more than
    @var{seconds}, terminate it and report a build failure.
    
    
    By default, the daemon's setting is honored (@pxref{Invoking
    guix-daemon, @code{--max-silent-time}}).
    
    
    @item --timeout=@var{seconds}
    Likewise, when the build or substitution process lasts for more than
    @var{seconds}, terminate it and report a build failure.
    
    
    By default, the daemon's setting is honored (@pxref{Invoking
    guix-daemon, @code{--timeout}}).
    
    
    @item --verbosity=@var{level}
    Use the given verbosity level.  @var{level} must be an integer between 0
    and 5; higher means more verbose output.  Setting a level of 4 or more
    may be helpful when debugging setup issues with the build daemon.
    
    @item --cores=@var{n}
    @itemx -c @var{n}
    Allow the use of up to @var{n} CPU cores for the build.  The special
    value @code{0} means to use as many CPU cores as available.
    
    @item --max-jobs=@var{n}
    @itemx -M @var{n}
    Allow at most @var{n} build jobs in parallel.  @xref{Invoking
    guix-daemon, @code{--max-jobs}}, for details about this option and the
    equivalent @command{guix-daemon} option.
    
    @end table
    
    Behind the scenes, @command{guix build} is essentially an interface to
    the @code{package-derivation} procedure of the @code{(guix packages)}
    module, and to the @code{build-derivations} procedure of the @code{(guix
    derivations)} module.
    
    In addition to options explicitly passed on the command line,
    @command{guix build} and other @command{guix} commands that support
    building honor the @code{GUIX_BUILD_OPTIONS} environment variable.
    
    @defvr {Environment Variable} GUIX_BUILD_OPTIONS
    Users can define this variable to a list of command line options that
    will automatically be used by @command{guix build} and other
    @command{guix} commands that can perform builds, as in the example
    below:
    
    @example
    $ export GUIX_BUILD_OPTIONS="--no-substitutes -c 2 -L /foo/bar"
    @end example
    
    These options are parsed independently, and the result is appended to
    the parsed command-line options.
    @end defvr
    
    
    
    @node Package Transformation Options
    @subsection Package Transformation Options
    
    @cindex package variants
    Another set of command-line options supported by @command{guix build}
    
    and also @command{guix package} are @dfn{package transformation
    
    Andreas Enge's avatar
    Andreas Enge committed
    options}.  These are options that make it possible to define @dfn{package
    
    variants}---for instance, packages built from different source code.
    This is a convenient way to create customized packages on the fly
    without having to type in the definitions of package variants
    (@pxref{Defining Packages}).
    
    
    @table @code
    
    @item --with-source=@var{source}
    
    @itemx --with-source=@var{package}=@var{source}
    @itemx --with-source=@var{package}@@@var{version}=@var{source}
    Use @var{source} as the source of @var{package}, and @var{version} as
    its version number.
    
    @var{source} must be a file name or a URL, as for @command{guix
    download} (@pxref{Invoking guix download}).
    
    
    When @var{package} is omitted,
    it is taken to be the package name specified on the
    command line that matches the base of @var{source}---e.g.,
    
    Andreas Enge's avatar
    Andreas Enge committed
    if @var{source} is @code{/src/guile-2.0.10.tar.gz}, the corresponding
    
    package is @code{guile}.
    
    Likewise, when @var{version} is omitted, the version string is inferred from
    
    Andreas Enge's avatar
    Andreas Enge committed
    @var{source}; in the previous example, it is @code{2.0.10}.
    
    
    This option allows users to try out versions of packages other than the
    one provided by the distribution.  The example below downloads
    @file{ed-1.7.tar.gz} from a GNU mirror and uses that as the source for
    the @code{ed} package:
    
    @example
    guix build ed --with-source=mirror://gnu/ed/ed-1.7.tar.gz
    @end example
    
    As a developer, @code{--with-source} makes it easy to test release
    candidates:
    
    @example
    guix build guile --with-source=../guile-2.0.9.219-e1bb7.tar.xz
    @end example
    
    @dots{} or to build from a checkout in a pristine environment:
    
    @example
    $ git clone git://git.sv.gnu.org/guix.git
    
    $ guix build guix --with-source=guix@@1.0=./guix
    
    @item --with-input=@var{package}=@var{replacement}
    Replace dependency on @var{package} by a dependency on
    @var{replacement}.  @var{package} must be a package name, and
    @var{replacement} must be a package specification such as @code{guile}
    or @code{guile@@1.8}.
    
    
    Andreas Enge's avatar
    Andreas Enge committed
    For instance, the following command builds Guix, but replaces its
    
    dependency on the current stable version of Guile with a dependency on
    
    the legacy version of Guile, @code{guile@@2.0}:
    
    guix build --with-input=guile=guile@@2.0 guix
    
    @end example
    
    This is a recursive, deep replacement.  So in this example, both
    @code{guix} and its dependency @code{guile-json} (which also depends on
    
    @code{guile}) get rebuilt against @code{guile@@2.0}.
    
    This is implemented using the @code{package-input-rewriting} Scheme
    procedure (@pxref{Defining Packages, @code{package-input-rewriting}}).
    
    
    @item --with-graft=@var{package}=@var{replacement}
    This is similar to @code{--with-input} but with an important difference:
    
    instead of rebuilding the whole dependency chain, @var{replacement} is
    
    built and then @dfn{grafted} onto the binaries that were initially
    referring to @var{package}.  @xref{Security Updates}, for more
    information on grafts.
    
    For example, the command below grafts version 3.5.4 of GnuTLS onto Wget
    and all its dependencies, replacing references to the version of GnuTLS
    they currently refer to:
    
    @example
    guix build --with-graft=gnutls=gnutls@@3.5.4 wget
    @end example
    
    This has the advantage of being much faster than rebuilding everything.
    But there is a caveat: it works if and only if @var{package} and
    @var{replacement} are strictly compatible---for example, if they provide
    a library, the application binary interface (ABI) of those libraries
    must be compatible.  If @var{replacement} is somehow incompatible with
    @var{package}, then the resulting package may be unusable.  Use with
    care!
    
    
    @node Additional Build Options
    @subsection Additional Build Options
    
    The command-line options presented below are specific to @command{guix
    build}.
    
    @item --quiet
    @itemx -q
    Build quietly, without displaying the build log.  Upon completion, the
    build log is kept in @file{/var} (or similar) and can always be
    retrieved using the @option{--log-file} option.
    
    
    @item --file=@var{file}
    @itemx -f @var{file}
    
    Build the package or derivation that the code within @var{file}
    evaluates to.
    
    As an example, @var{file} might contain a package definition like this
    (@pxref{Defining Packages}):
    
    @example
    @verbatiminclude package-hello.scm
    @end example
    
    
    @item --expression=@var{expr}
    @itemx -e @var{expr}
    
    Build the package or derivation @var{expr} evaluates to.
    
    For example, @var{expr} may be @code{(@@ (gnu packages guile)
    
    guile-1.8)}, which unambiguously designates this specific variant of
    version 1.8 of Guile.
    
    
    Andreas Enge's avatar
    Andreas Enge committed
    Alternatively, @var{expr} may be a G-expression, in which case it is used
    
    as a build program passed to @code{gexp->derivation}
    (@pxref{G-Expressions}).
    
    Lastly, @var{expr} may refer to a zero-argument monadic procedure
    
    (@pxref{The Store Monad}).  The procedure must return a derivation as a
    monadic value, which is then passed through @code{run-with-store}.
    
    
    @item --source
    @itemx -S
    
    Andreas Enge's avatar
    Andreas Enge committed
    Build the source derivations of the packages, rather than the packages
    
    For instance, @code{guix build -S gcc} returns something like
    
    Andreas Enge's avatar
    Andreas Enge committed
    @file{/gnu/store/@dots{}-gcc-4.7.2.tar.bz2}, which is the GCC
    source tarball.
    
    The returned source tarball is the result of applying any patches and
    
    Andreas Enge's avatar
    Andreas Enge committed
    code snippets specified in the package @code{origin} (@pxref{Defining