all messages for Guix-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* how to use a variable in a plain-file object?
@ 2024-05-16 15:57 Giovanni Biscuolo
  2024-05-16 16:15 ` raingloom
  2024-05-16 18:39 ` Tomas Volf
  0 siblings, 2 replies; 5+ messages in thread
From: Giovanni Biscuolo @ 2024-05-16 15:57 UTC (permalink / raw)
  To: help-guix

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

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?

Thanks, Gio'

-- 
Giovanni Biscuolo

Xelera IT Infrastructures

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 849 bytes --]

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: how to use a variable in a plain-file object?
  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
  1 sibling, 1 reply; 5+ messages in thread
From: raingloom @ 2024-05-16 16:15 UTC (permalink / raw)
  To: Giovanni Biscuolo; +Cc: help-guix

On 2024-05-16 17:57, 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?
> 
> Thanks, Gio'

You probably want a G-expression.

Short untested example:

(define name "Giovanni")
(define greeting-gexp
  #~(begin (with-output-to-file #$output (lambda _ (write (string-append
"Hello, " #$name "!"))))))

You should be able to use greeting-gexp in place of %my-file-object.

I highly recommend reading the relevant docs in Guix's info pages.
Online version:
https://guix.gnu.org/manual/en/html_node/G_002dExpressions.html
Related blog post:
https://guix.gnu.org/en/blog/2023/dissecting-guix-part-3-g-expressions/


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: how to use a variable in a plain-file object?
  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-16 18:39 ` Tomas Volf
  2024-05-17  8:24   ` Giovanni Biscuolo
  1 sibling, 1 reply; 5+ messages in thread
From: Tomas Volf @ 2024-05-16 18:39 UTC (permalink / raw)
  To: Giovanni Biscuolo; +Cc: help-guix

[-- 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 --]

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: how to use a variable in a plain-file object?
  2024-05-16 18:39 ` Tomas Volf
@ 2024-05-17  8:24   ` Giovanni Biscuolo
  0 siblings, 0 replies; 5+ messages in thread
From: Giovanni Biscuolo @ 2024-05-17  8:24 UTC (permalink / raw)
  To: Tomas Volf; +Cc: help-guix

Hello Tomas,

Tomas Volf <~@wolfsden.cz> writes:

[...]

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

Oh yes!  I missed (to understand) mixed-text-file and text-file* in the
G-exp manual section [1], in particular how it can be easily used in
place of local-file and plain-file, because I missed (to understand) the
given example.  Now that I see, I think I'll use that in place of
plain-file... everywhere :-O

[...]

>     (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

Cristal clear, also thank you for having pointed out a quick way to test
the result by building it and cat(ing) it in a shell session

This could be a nice section in the Cookbook :-)

Thank you and happy hacking!

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

P.S.: I'll steal the above sentence, _verbatim_ :-)


[1] IMHO that manual section "talks" to experienced Guile programmers
and not to Guix _users_... and actually is a subsection of "Programming
interface".

-- 
Giovanni Biscuolo

Xelera IT Infrastructures


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: how to use a variable in a plain-file object?
  2024-05-16 16:15 ` raingloom
@ 2024-05-17  8:45   ` Giovanni Biscuolo
  0 siblings, 0 replies; 5+ messages in thread
From: Giovanni Biscuolo @ 2024-05-17  8:45 UTC (permalink / raw)
  To: raingloom; +Cc: help-guix

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

Hello raingloom

thank you for you quick reply!

raingloom@riseup.net writes:

[...]

> Short untested example:
>
> (define name "Giovanni")
> (define greeting-gexp
>   #~(begin (with-output-to-file #$output (lambda _ (write (string-append
> "Hello, " #$name "!"))))))
>
> You should be able to use greeting-gexp in place of %my-file-object.

I've tested it with this code:

--8<---------------cut here---------------start------------->8---

(use-modules (guix gexp))

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

(define %another-file-object
  #~(begin (with-output-to-file #$output (lambda _ (write (string-append "\
# This is an example configuration file
# stored in %another-file-object
attribute1 = " #$var1 "
attribute2 = " #$var2 "
"))))))

%another-file-object

--8<---------------cut here---------------end--------------->8---

And this is the result:

--8<---------------cut here---------------start------------->8---

[~/tmp]
giovanni@roquette [genv]\: cat $(guix build --no-offload -e '(load "guix-output-to-file-example.scm")')
"# This is an example configuration file\n# stored in %another-file-object\nattribute1 = var1-value\nattribute2 = var2-value\n"

--8<---------------cut here---------------end--------------->8---

The output cannot be used in place of %my-file-object, right?

AFAIU the solution is to use mixed-text-file as suggested by Tomas Volf
yesterday in this thread.

Happy hacking! Gio'

[...]

-- 
Giovanni Biscuolo

Xelera IT Infrastructures

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 849 bytes --]

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2024-05-17  8:46 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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
2024-05-17  8:24   ` Giovanni Biscuolo

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.