all messages for Guix-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Tomas Volf <~@wolfsden.cz>
To: Giovanni Biscuolo <g@xelera.eu>
Cc: help-guix@gnu.org
Subject: Re: how to use a variable in a plain-file object?
Date: Thu, 16 May 2024 20:39:54 +0200	[thread overview]
Message-ID: <ZkZS-qKQNJsUt74A@ws> (raw)
In-Reply-To: <87le493g1o.fsf@xelera.eu>

[-- Attachment #1: Type: text/plain, Size: 2238 bytes --]

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 #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

  parent reply	other threads:[~2024-05-16 18:40 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-05-16 15:57 how to use a variable in a plain-file object? Giovanni Biscuolo
2024-05-16 16:15 ` raingloom
2024-05-17  8:45   ` Giovanni Biscuolo
2024-05-16 18:39 ` Tomas Volf [this message]
2024-05-17  8:24   ` Giovanni Biscuolo

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=ZkZS-qKQNJsUt74A@ws \
    --to=~@wolfsden.cz \
    --cc=g@xelera.eu \
    --cc=help-guix@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/guix.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.