unofficial mirror of help-guix@gnu.org 
 help / color / mirror / Atom feed
* Guix Home, .local/bin executable scripts
@ 2022-09-24 20:06 Fabio Natali
  2022-09-24 22:49 ` Trev
  0 siblings, 1 reply; 3+ messages in thread
From: Fabio Natali @ 2022-09-24 20:06 UTC (permalink / raw)
  To: help-guix

Dear All,

I've recently started exploring Guix Home. I was able to create a few
simple services for some of my apps, e.g. Emacs.

I've now come to a point where I'd like to add some executables to my
configuration, e.g. some of my `.local/bin' scripts.

Consider this `hello.sh' script, for example:

,----
| #!/bin/bash
| 
| echo "Hello World" | sed "s/World/Guix/"
`----

The following service copies `hello.sh' to the expected destination and
does so while preserving the correct permissions (`#:recursive #t'),
i.e. the file is executable.

,----
| (define my/home-hello-service
|   (service
|    (service-type
|     (name 'home-hello)
|     (extensions
|      (list
|       (service-extension
|        home-files-service-type
|        (lambda (config)
|          `((".local/bin/hello.sh"
|             ,(local-file "scripts/hello.sh" #:recursive? #t)))))))
|     (default-value #f)
|     (description "My valuable Hello World script."))))
`----

The script shows up in my Guix Home but it won't work - I suppose that
`/bin/bash' should be replaced with the correct Bash store path?

,----
| ~$ hello.sh
| -bash: ~/.local/bin/hello.sh: /bin/bash: bad interpreter: No such file or directory
`----

I've been looking at the docs and found something that feels related to
my problem (e.g. `computed-file', `substitute*', ...?) but I'd
appreciate if someone could point me in the right direction.

Thanks, best, Fabio.


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

* Re: Guix Home, .local/bin executable scripts
  2022-09-24 20:06 Guix Home, .local/bin executable scripts Fabio Natali
@ 2022-09-24 22:49 ` Trev
  2022-09-25 13:27   ` Fabio Natali
  0 siblings, 1 reply; 3+ messages in thread
From: Trev @ 2022-09-24 22:49 UTC (permalink / raw)
  To: Fabio Natali, help-guix

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

> Dear All,
>
> I've recently started exploring Guix Home. I was able to create a few
> simple services for some of my apps, e.g. Emacs.
>
> I've now come to a point where I'd like to add some executables to my
> configuration, e.g. some of my `.local/bin' scripts.
>
> Consider this `hello.sh' script, for example:
>
> ,----
> | #!/bin/bash
> | 
> | echo "Hello World" | sed "s/World/Guix/"
> `----
>
> The following service copies `hello.sh' to the expected destination and
> does so while preserving the correct permissions (`#:recursive #t'),
> i.e. the file is executable.
>

If the thing is being moved to your store for any reason you may want to
explicitly update the permissions.  I had to do this when I packaged PHP
composer for personal use.  Things tend to go into the store as read-only.

> ,----
> | (define my/home-hello-service
> |   (service
> |    (service-type
> |     (name 'home-hello)
> |     (extensions
> |      (list
> |       (service-extension
> |        home-files-service-type
> |        (lambda (config)
> |          `((".local/bin/hello.sh"
> |             ,(local-file "scripts/hello.sh" #:recursive? #t)))))))
> |     (default-value #f)
> |     (description "My valuable Hello World script."))))
> `----
>
> The script shows up in my Guix Home but it won't work - I suppose that
> `/bin/bash' should be replaced with the correct Bash store path?
>

I would try to us a portable shebang such as #!/usr/bin/env bash.  You
may also use a gexp to dynamically insert the true location of your
profile's bash path.  This is the guix way, so far as I can tell.  I am
not sure what the use of a #:recursive? keyword might be in the context
of a single script.

Check out the manual on Gexps.  I find it to be pretty helpful.

> ,----
> | ~$ hello.sh
> | -bash: ~/.local/bin/hello.sh: /bin/bash: bad interpreter: No such file or directory
> `----
>
> I've been looking at the docs and found something that feels related to
> my problem (e.g. `computed-file', `substitute*', ...?) but I'd
> appreciate if someone could point me in the right direction.
>

You can patch a shebang or anything else with substitute by doing the
following from within a gexp where #$bash happens to be the bash package
reference so long as it is in the right lexical scope. See
(with-imported-modules) in the manual.

(substitute* "scripts/hello.sh"
  ("#!/bin/bash" (string-append #$bash "/bin/bash")))

Link to manual: https://guix.gnu.org/manual/en/html_node/G_002dExpressions.html

> Thanks, best, Fabio.
>

Good luck.

Full disclosure: I'm only just starting to get the hang of this
stuff.  Anyone else is welcome with better advice.

-- 

Trev : 0FB7 D06B 4A2A F07E AD5B  1169 183B 6306 8AA1 D206

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

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

* Re: Guix Home, .local/bin executable scripts
  2022-09-24 22:49 ` Trev
@ 2022-09-25 13:27   ` Fabio Natali
  0 siblings, 0 replies; 3+ messages in thread
From: Fabio Natali @ 2022-09-25 13:27 UTC (permalink / raw)
  To: Trev, help-guix

Hey Trev,

Thanks for your help!

On 2022-09-24, 15:49 -0700, Trev <trev@trevdev.ca> wrote:
> If the thing is being moved to your store for any reason you may want
> to explicitly update the permissions.  I had to do this when I
> packaged PHP composer for personal use.  Things tend to go into the
> store as read-only.

I see, yes, I followed your advice and ended up adding an explicit
`chmod' command.

> You can patch a shebang or anything else with substitute by doing the
> following from within a gexp where #$bash happens to be the bash
> package reference so long as it is in the right lexical scope. See
> (with-imported-modules) in the manual.
>
> (substitute* "scripts/hello.sh"
>   ("#!/bin/bash" (string-append #$bash "/bin/bash")))

This was a great starting point. Thanks to this and some fantastic
support on IRC, I was able to get to this working version:

,----
| (define my/home-hello-service
|   (service
|    home-files-service-type
|    `((".local/bin/hello.sh"
|       ,(computed-file
|         "hello.sh"
|         (with-imported-modules
|          '((guix build utils))
|          #~(begin
|              (use-modules (guix build utils))
|              (copy-file #$(local-file "scripts/hello.sh") #$output)
|              (substitute*
|               #$output (("/bin/bash") #$(file-append bash-minimal "/bin/bash")))
|              (chmod #$output #o555))))))))
`----

It was quite a learning curve, but I'm happy I got that working.

Thanks, best, Fabio.


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

end of thread, other threads:[~2022-09-25 13:27 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-09-24 20:06 Guix Home, .local/bin executable scripts Fabio Natali
2022-09-24 22:49 ` Trev
2022-09-25 13:27   ` Fabio Natali

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).