Skip to content
Snippets Groups Projects
guix.texi 1000 KiB
Newer Older
  • Learn to ignore specific revisions
  • @code{"out"} is assumed.  @var{modules} is a list of names of Guile
    modules from the current search path to be copied in the store,
    compiled, and made available in the load path during the execution of
    @var{exp}---e.g., @code{((guix build utils) (guix build
    gnu-build-system))}.
    
    @var{exp} is evaluated in an environment where @code{%outputs} is bound
    to a list of output/path pairs, and where @code{%build-inputs} is bound
    to a list of string/output-path pairs made from @var{inputs}.
    Optionally, @var{env-vars} is a list of string pairs specifying the name
    and value of environment variables visible to the builder.  The builder
    terminates by passing the result of @var{exp} to @code{exit}; thus, when
    @var{exp} returns @code{#f}, the build is considered to have failed.
    
    @var{exp} is built using @var{guile-for-build} (a derivation).  When
    @var{guile-for-build} is omitted or is @code{#f}, the value of the
    @code{%guile-for-build} fluid is used instead.
    
    See the @code{derivation} procedure for the meaning of
    @var{references-graphs}, @var{allowed-references},
    @var{disallowed-references}, @var{local-build?}, and
    @var{substitutable?}.
    
    @end deffn
    
    @noindent
    Here's an example of a single-output derivation that creates a directory
    containing one file:
    
    @lisp
    (let ((builder '(let ((out (assoc-ref %outputs "out")))
                      (mkdir out)    ; create /gnu/store/@dots{}-goo
                      (call-with-output-file (string-append out "/test")
                        (lambda (p)
                          (display '(hello guix) p))))))
      (build-expression->derivation store "goo" builder))
    
    @result{} #<derivation /gnu/store/@dots{}-goo.drv => @dots{}>
    @end lisp
    
    @node The Store Monad
    @section The Store Monad
    
    The procedures that operate on the store described in the previous
    sections all take an open connection to the build daemon as their first
    argument.  Although the underlying model is functional, they either have
    side effects or depend on the current state of the store.
    
    The former is inconvenient: the connection to the build daemon has to be
    carried around in all those functions, making it impossible to compose
    functions that do not take that parameter with functions that do.  The
    latter can be problematic: since store operations have side effects
    and/or depend on external state, they have to be properly sequenced.
    
    @cindex monadic values
    @cindex monadic functions
    This is where the @code{(guix monads)} module comes in.  This module
    provides a framework for working with @dfn{monads}, and a particularly
    useful monad for our uses, the @dfn{store monad}.  Monads are a
    construct that allows two things: associating ``context'' with values
    (in our case, the context is the store), and building sequences of
    computations (here computations include accesses to the store).  Values
    in a monad---values that carry this additional context---are called
    @dfn{monadic values}; procedures that return such values are called
    @dfn{monadic procedures}.
    
    Consider this ``normal'' procedure:
    
    (define (sh-symlink store)
      ;; Return a derivation that symlinks the 'bash' executable.
      (let* ((drv (package-derivation store bash))
             (out (derivation->output-path drv))
             (sh  (string-append out "/bin/bash")))
        (build-expression->derivation store "sh"
                                      `(symlink ,sh %output))))
    
    Using @code{(guix monads)} and @code{(guix gexp)}, it may be rewritten
    as a monadic function:
    
    (define (sh-symlink)
      ;; Same, but return a monadic value.
      (mlet %store-monad ((drv (package->derivation bash)))
        (gexp->derivation "sh"
                          #~(symlink (string-append #$drv "/bin/bash")
                                     #$output))))
    
    There are several things to note in the second version: the @code{store}
    parameter is now implicit and is ``threaded'' in the calls to the
    @code{package->derivation} and @code{gexp->derivation} monadic
    procedures, and the monadic value returned by @code{package->derivation}
    is @dfn{bound} using @code{mlet} instead of plain @code{let}.
    
    As it turns out, the call to @code{package->derivation} can even be
    omitted since it will take place implicitly, as we will see later
    (@pxref{G-Expressions}):
    
    (define (sh-symlink)
      (gexp->derivation "sh"
                        #~(symlink (string-append #$bash "/bin/bash")
                                   #$output)))
    
    @c See
    @c <https://syntaxexclamation.wordpress.com/2014/06/26/escaping-continuations/>
    @c for the funny quote.
    Calling the monadic @code{sh-symlink} has no effect.  As someone once
    said, ``you exit a monad like you exit a building on fire: by running''.
    So, to exit the monad and get the desired effect, one must use
    @code{run-with-store}:
    
    (run-with-store (open-connection) (sh-symlink))
    @result{} /gnu/store/...-sh-symlink
    
    Note that the @code{(guix monad-repl)} module extends the Guile REPL with
    new ``meta-commands'' to make it easier to deal with monadic procedures:
    @code{run-in-store}, and @code{enter-store-monad}.  The former is used
    to ``run'' a single monadic value through the store:
    
    scheme@@(guile-user)> ,run-in-store (package->derivation hello)
    $1 = #<derivation /gnu/store/@dots{}-hello-2.9.drv => @dots{}>
    
    The latter enters a recursive REPL, where all the return values are
    automatically run through the store:
    
    @example
    scheme@@(guile-user)> ,enter-store-monad
    store-monad@@(guile-user) [1]> (package->derivation hello)
    $2 = #<derivation /gnu/store/@dots{}-hello-2.9.drv => @dots{}>
    store-monad@@(guile-user) [1]> (text-file "foo" "Hello!")
    $3 = "/gnu/store/@dots{}-foo"
    store-monad@@(guile-user) [1]> ,q
    scheme@@(guile-user)>
    @end example
    
    @noindent
    Note that non-monadic values cannot be returned in the
    @code{store-monad} REPL.
    
    The main syntactic forms to deal with monads in general are provided by
    the @code{(guix monads)} module and are described below.
    
    @deffn {Scheme Syntax} with-monad @var{monad} @var{body} ...
    Evaluate any @code{>>=} or @code{return} forms in @var{body} as being
    in @var{monad}.
    @end deffn
    
    @deffn {Scheme Syntax} return @var{val}
    Return a monadic value that encapsulates @var{val}.
    @end deffn
    
    @deffn {Scheme Syntax} >>= @var{mval} @var{mproc} ...
    @dfn{Bind} monadic value @var{mval}, passing its ``contents'' to monadic
    procedures @var{mproc}@dots{}@footnote{This operation is commonly
    referred to as ``bind'', but that name denotes an unrelated procedure in
    Guile.  Thus we use this somewhat cryptic symbol inherited from the
    Haskell language.}.  There can be one @var{mproc} or several of them, as
    in this example:
    
    (run-with-state
        (with-monad %state-monad
          (>>= (return 1)
               (lambda (x) (return (+ 1 x)))
               (lambda (x) (return (* 2 x)))))
      'some-state)
    
    @deffn {Scheme Syntax} mlet @var{monad} ((@var{var} @var{mval}) ...) @
           @var{body} ...
    @deffnx {Scheme Syntax} mlet* @var{monad} ((@var{var} @var{mval}) ...) @
           @var{body} ...
    Bind the variables @var{var} to the monadic values @var{mval} in
    @var{body}, which is a sequence of expressions.  As with the bind
    operator, this can be thought of as ``unpacking'' the raw, non-monadic
    value ``contained'' in @var{mval} and making @var{var} refer to that
    raw, non-monadic value within the scope of the @var{body}.  The form
    (@var{var} -> @var{val}) binds @var{var} to the ``normal'' value
    @var{val}, as per @code{let}.  The binding operations occur in sequence
    from left to right.  The last expression of @var{body} must be a monadic
    expression, and its result will become the result of the @code{mlet} or
    @code{mlet*} when run in the @var{monad}.
    
    @code{mlet*} is to @code{mlet} what @code{let*} is to @code{let}
    (@pxref{Local Bindings,,, guile, GNU Guile Reference Manual}).
    @end deffn
    
    @deffn {Scheme System} mbegin @var{monad} @var{mexp} ...
    Bind @var{mexp} and the following monadic expressions in sequence,
    returning the result of the last expression.  Every expression in the
    sequence must be a monadic expression.
    
    This is akin to @code{mlet}, except that the return values of the
    monadic expressions are ignored.  In that sense, it is analogous to
    @code{begin}, but applied to monadic expressions.
    @end deffn
    
    @deffn {Scheme System} mwhen @var{condition} @var{mexp0} @var{mexp*} ...
    When @var{condition} is true, evaluate the sequence of monadic
    expressions @var{mexp0}..@var{mexp*} as in an @code{mbegin}.  When
    @var{condition} is false, return @code{*unspecified*} in the current
    monad.  Every expression in the sequence must be a monadic expression.
    @end deffn
    
    @deffn {Scheme System} munless @var{condition} @var{mexp0} @var{mexp*} ...
    When @var{condition} is false, evaluate the sequence of monadic
    expressions @var{mexp0}..@var{mexp*} as in an @code{mbegin}.  When
    @var{condition} is true, return @code{*unspecified*} in the current
    monad.  Every expression in the sequence must be a monadic expression.
    @end deffn
    
    @cindex state monad
    The @code{(guix monads)} module provides the @dfn{state monad}, which
    allows an additional value---the state---to be @emph{threaded} through
    monadic procedure calls.
    
    @defvr {Scheme Variable} %state-monad
    The state monad.  Procedures in the state monad can access and change
    the state that is threaded.
    
    Consider the example below.  The @code{square} procedure returns a value
    in the state monad.  It returns the square of its argument, but also
    increments the current state value:
    
    (define (square x)
      (mlet %state-monad ((count (current-state)))
        (mbegin %state-monad
          (set-current-state (+ 1 count))
          (return (* x x)))))
    
    (run-with-state (sequence %state-monad (map square (iota 3))) 0)
    @result{} (0 1 4)
    @result{} 3
    
    When ``run'' through @var{%state-monad}, we obtain that additional state
    value, which is the number of @code{square} calls.
    @end defvr
    
    @deffn {Monadic Procedure} current-state
    Return the current state as a monadic value.
    @end deffn
    
    @deffn {Monadic Procedure} set-current-state @var{value}
    Set the current state to @var{value} and return the previous state as a
    monadic value.
    @end deffn
    
    @deffn {Monadic Procedure} state-push @var{value}
    Push @var{value} to the current state, which is assumed to be a list,
    and return the previous state as a monadic value.
    @end deffn
    
    @deffn {Monadic Procedure} state-pop
    Pop a value from the current state and return it as a monadic value.
    The state is assumed to be a list.
    @end deffn
    
    @deffn {Scheme Procedure} run-with-state @var{mval} [@var{state}]
    Run monadic value @var{mval} starting with @var{state} as the initial
    state.  Return two values: the resulting value, and the resulting state.
    @end deffn
    
    The main interface to the store monad, provided by the @code{(guix
    store)} module, is as follows.
    
    @defvr {Scheme Variable} %store-monad
    The store monad---an alias for @var{%state-monad}.
    
    Values in the store monad encapsulate accesses to the store.  When its
    effect is needed, a value of the store monad must be ``evaluated'' by
    passing it to the @code{run-with-store} procedure (see below.)
    @end defvr
    
    @deffn {Scheme Procedure} run-with-store @var{store} @var{mval} [#:guile-for-build] [#:system (%current-system)]
    Run @var{mval}, a monadic value in the store monad, in @var{store}, an
    open store connection.
    @end deffn
    
    @deffn {Monadic Procedure} text-file @var{name} @var{text} [@var{references}]
    Return as a monadic value the absolute file name in the store of the file
    containing @var{text}, a string.  @var{references} is a list of store items that the
    resulting text file refers to; it defaults to the empty list.
    @end deffn
    
    @deffn {Monadic Procedure} binary-file @var{name} @var{data} [@var{references}]
    Return as a monadic value the absolute file name in the store of the file
    containing @var{data}, a bytevector.  @var{references} is a list of store
    items that the resulting binary file refers to; it defaults to the empty list.
    @end deffn
    
    @deffn {Monadic Procedure} interned-file @var{file} [@var{name}] @
             [#:recursive? #t] [#:select? (const #t)]
    Return the name of @var{file} once interned in the store.  Use
    @var{name} as its store name, or the basename of @var{file} if
    @var{name} is omitted.
    
    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.
    
    The example below adds a file to the store, under two different names:
    
    (run-with-store (open-connection)
      (mlet %store-monad ((a (interned-file "README"))
                          (b (interned-file "README" "LEGU-MIN")))
        (return (list a b))))
    
    @result{} ("/gnu/store/rwm@dots{}-README" "/gnu/store/44i@dots{}-LEGU-MIN")
    
    The @code{(guix packages)} module exports the following package-related
    monadic procedures:
    
    @deffn {Monadic Procedure} package-file @var{package} [@var{file}] @
           [#:system (%current-system)] [#:target #f] @
           [#:output "out"]
    Return as a monadic
    value in the absolute file name of @var{file} within the @var{output}
    directory of @var{package}.  When @var{file} is omitted, return the name
    of the @var{output} directory of @var{package}.  When @var{target} is
    true, use it as a cross-compilation target triplet.
    @end deffn
    
    @deffn {Monadic Procedure} package->derivation @var{package} [@var{system}]
    @deffnx {Monadic Procedure} package->cross-derivation @var{package} @
              @var{target} [@var{system}]
    Monadic version of @code{package-derivation} and
    @code{package-cross-derivation} (@pxref{Defining Packages}).
    @end deffn
    
    @node G-Expressions
    @section G-Expressions
    
    @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}).
    These build actions are performed when asking the daemon to actually
    build the derivations; they are run by the daemon in a container
    (@pxref{Invoking guix-daemon}).
    
    @cindex strata of code
    It should come as no surprise that we like to write these build actions
    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.
    
    To describe a derivation and its build actions, one typically needs to
    embed build code inside host code.  It boils down to manipulating build
    code as data, and the homoiconicity of Scheme---code has a direct
    representation as data---comes in handy for that.  But we need more than
    the normal @code{quasiquote} mechanism in Scheme to construct build
    expressions.
    
    The @code{(guix gexp)} module implements @dfn{G-expressions}, a form of
    S-expressions adapted to build expressions.  G-expressions, or
    @dfn{gexps}, consist essentially of three syntactic forms: @code{gexp},
    @code{ungexp}, and @code{ungexp-splicing} (or simply: @code{#~},
    @code{#$}, and @code{#$@@}), which are comparable to
    @code{quasiquote}, @code{unquote}, and @code{unquote-splicing},
    respectively (@pxref{Expression Syntax, @code{quasiquote},, guile,
    GNU Guile Reference Manual}).  However, there are major differences:
    
    @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.
    
    @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
    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
    add files to the store and to refer to them in
    derivations and such (see @code{local-file} and @code{plain-file}
    below.)
    
    To illustrate the idea, here is an example of a gexp:
    
    (define build-exp
      #~(begin
          (mkdir #$output)
          (chdir #$output)
          (symlink (string-append #$coreutils "/bin/ls")
                   "list-files")))
    
    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}:
    
    (gexp->derivation "the-thing" build-exp)
    
    As one would expect, the @code{"/gnu/store/@dots{}-coreutils-8.22"} string is
    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
    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:
    
    (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")
    
    @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:
    
    (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)))
    
    @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.
    
    @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:
    
    (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{})))
    
    @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:
    
    (use-modules (gnu packages guile))  ;for 'guile-json'
    
    (with-extensions (list guile-json)
      (gexp->derivation "something-with-json"
                        #~(begin
                            (use-modules (json))
                            @dots{})))
    
    The syntactic form to construct gexps is summarized below.
    
    @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:
    
    @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
    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.
    
    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})
    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.
    
    @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}.
    
    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:
    
    `((guix build utils)
      (guix gcrypt)
      ((guix config) => ,(scheme-file "config.scm"
                                      #~(define-module @dots{}))))
    
    @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
    
    @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] @
           [#: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")] @
           [#:deprecation-warnings #f] @
           [#:local-build? #f] [#:substitutable? #t] @
           [#:properties '()] [#:guile-for-build #f]
    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
    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:
    
    (@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})
    
    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}).
    @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"))
    
    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 literal string
    denoting a relative file name, it is looked up relative to the source
    file where it appears; if @var{file} is not a literal string, it is
    looked up relative to the current working directory at run time.
    @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] @
      [#:system (%current-system)] [#:target #f]
    
    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}.
    
    The example below builds a script that simply invokes the @command{ls}
    command:
    
    (use-modules (guix gexp) (gnu packages base))
    
    (gexp->script "list-files"
                  #~(execl #$(file-append coreutils "/bin/ls")
                           "ls"))
    
    When ``running'' it through the store (@pxref{The Store Monad,
    @code{run-with-store}}), we obtain a derivation that produces an
    executable file @file{/gnu/store/@dots{}-list-files} along these lines:
    
    Nikita Karetnikov's avatar
    Nikita Karetnikov committed
    
    @example
    
    #!/gnu/store/@dots{}-guile-2.0.11/bin/guile -ds
    !#
    (execl "/gnu/store/@dots{}-coreutils-8.22"/bin/ls" "ls")
    
    Nikita Karetnikov's avatar
    Nikita Karetnikov committed
    @end example
    
    @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] @
                [#:splice? #f] @
                [#:guile (default-guile)]
    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}.
    
    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:
    
    (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"))
    
    In this example, the resulting @file{/gnu/store/@dots{}-profile.sh} file
    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:
    
    (mixed-text-file "profile"
                     "export PATH=" coreutils "/bin:" grep "/bin")
    
    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:
    
    (file-union "etc"
                `(("hosts" ,(plain-file "hosts"
                                        "127.0.0.1 localhost"))
                  ("bashrc" ,(plain-file "bashrc"
                                         "alias ls='ls --color=auto'"))))
    
    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:
    
    (directory-union "guile+emacs" (list guile emacs))
    
    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:
    
    (gexp->script "run-uname"
                  #~(system* #$(file-append coreutils
                                            "/bin/uname")))
    
    The same effect could be achieved with:
    
    (gexp->script "run-uname"
                  #~(system* (string-append #$coreutils
                                            "/bin/uname")))
    
    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
    
    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:
    
    $ guix repl
    scheme@@(guile-user)> ,use (gnu packages base)
    scheme@@(guile-user)> coreutils
    $1 = #<package coreutils@@8.29 gnu/packages/base.scm:327 3e28300>
    
    @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:
    
    @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
    
    @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.
    
    @menu
    * Invoking guix build::         Building packages from the command line.
    * 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.