On 2024-05-16 17:57:55 +0200, Giovanni Biscuolo wrote:
> Hello,
>
> sorry for the very guile-absolute-beginner question, but I'd like to use
> a declared variable in a plain-file object, so I can write something
> like in this pseudo-code snippet:
>
> --8<---------------cut here---------------start------------->8---
>
> (define variable1 "var1-value")
> (define variable2 "var2-value")
>
> [...]
>
> (define %my-file-object
>   ,(plain-file "something.conf" "
> # This is an example configuration file
>
> attribute1 =" variable1 "
>
> attribute2 =" variable2 "
>
> "))
>
> --8<---------------cut here---------------end--------------->8---
>
> and obtain a "something.conf" file like this:
>
> --8<---------------cut here---------------start------------->8---
> # This is an example configuration file
>
> attribute1 = var1-value
>
> attribute2 = var2-value
>
> --8<---------------cut here---------------end--------------->8---
>
> how can I do, please?

I believe you can use mixed-text-file for this purpose:

  /tmp/xx.scm:

    (use-modules (guix gexp))

    (define var1 "var1-value")
    (define var2 "var2-value")

    (define %my-file-object
      (mixed-text-file "something.conf" "\
    # This is an example configuration file
    attribute1 = " var1 "
    attribute2 = " var2 "
    "))

    %my-file-object

When I build it and inspect the result:

    $ cat $(guix build -e '(load "/tmp/xx.scm")')
    # This is an example configuration file
    attribute1 = var1-value
    attribute2 = var2-value

The mixed-text-file accepts string *and* file-like objects as arguments.  That
makes it very suitable for constructing configuration files, since you can
expand paths, for example like this:

  /tmp/yy.scm:

    (use-modules (gnu packages base)
                 (guix gexp))

    (define %my-file-object
      (mixed-text-file "something.conf" "\
    SED_PATH = " sed "/bin/sed
    "))

    %my-file-object

And here is the result:

    $ cat $(guix build -e '(load "/tmp/yy.scm")')
    SED_PATH = /gnu/store/6kkygybkxkzqy3lf6k5kzimk5mjasrvw-sed-4.8/bin/sed


Hope this is useful and have a nice day,
Tomas Volf

--
There are only two hard things in Computer Science:
cache invalidation, naming things and off-by-one errors.

Attachment: signature.asc
Description: PGP signature

Reply via email to