aboutsummaryrefslogtreecommitdiff
path: root/doc
diff options
context:
space:
mode:
authorLudovic Courtès <ludo@gnu.org>2014-02-03 23:12:54 +0100
committerLudovic Courtès <ludo@gnu.org>2014-02-03 23:20:49 +0100
commit45adbd624f920d315259b102b923728d655a1efa (patch)
tree8f55c82395e63c58c32db0737017253b5645c288 /doc
parent67995f4beaeb97a10c455d265acc7a209fcc5312 (diff)
downloadpatches-45adbd624f920d315259b102b923728d655a1efa.tar
patches-45adbd624f920d315259b102b923728d655a1efa.tar.gz
monads: Add 'text-file*'.
* guix/monads.scm (text-file*): New procedure. * tests/monads.scm ("text-file*"): New test. * doc/guix.texi (The Store Monad): Change example since the previous one would erroneously fail to retain a reference to Coreutils. Document 'text-file*'.
Diffstat (limited to 'doc')
-rw-r--r--doc/guix.texi48
1 files changed, 36 insertions, 12 deletions
diff --git a/doc/guix.texi b/doc/guix.texi
index 91fa07f1a8..28b1cb8bd7 100644
--- a/doc/guix.texi
+++ b/doc/guix.texi
@@ -1590,23 +1590,22 @@ in a monad---values that carry this additional context---are called
Consider this ``normal'' procedure:
@example
-(define (profile.sh store)
- ;; Return the name of a shell script in the store that
- ;; initializes the 'PATH' environment variable.
- (let* ((drv (package-derivation store coreutils))
- (out (derivation->output-path drv)))
- (add-text-to-store store "profile.sh"
- (format #f "export PATH=~a/bin" out))))
+(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))))
@end example
Using @code{(guix monads)}, it may be rewritten as a monadic function:
@example
-(define (profile.sh)
+(define (sh-symlink)
;; Same, but return a monadic value.
- (mlet %store-monad ((bin (package-file coreutils "bin")))
- (text-file "profile.sh"
- (string-append "export PATH=" bin))))
+ (mlet %store-monad ((sh (package-file bash "bin")))
+ (derivation-expression "sh" `(symlink ,sh %output))))
@end example
There are two things to note in the second version: the @code{store}
@@ -1672,7 +1671,32 @@ open store connection.
@deffn {Monadic Procedure} text-file @var{name} @var{text}
Return as a monadic value the absolute file name in the store of the file
-containing @var{text}.
+containing @var{text}, a string.
+@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, packages, derivations, and store file names; 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{/nix/store/@dots{}-profile.sh} file
+will references @var{coreutils}, @var{grep}, and @var{sed}, thereby
+preventing them from being garbage-collected during its lifetime.
@end deffn
@deffn {Monadic Procedure} package-file @var{package} [@var{file}] @