all messages for Guix-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* Let's share examples of simplistic and useless package definitions
@ 2023-05-26 18:59 Rodrigo Morales
  2023-05-28 19:40 ` Skyler Ferris
  0 siblings, 1 reply; 2+ messages in thread
From: Rodrigo Morales @ 2023-05-26 18:59 UTC (permalink / raw)
  To: help-guix

Table of Contents
_________________

1. The context
.. 1. Package no. 1: Create `%output' directory
.. 2. Package no. 2: Copy a local file
2. The enquiries

1 The context
=============

  As part of my learning journey with Guix, I'm trying to write
  simplistic and useless package definitions in order to experiment with
  them and get more familiar with the internals with Guix. I've been
  able to come up with the following packages.


1.1 Package no. 1: Create `%output' directory
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

  The following package creates the output directory and does nothing
  else.

  ,----
  | (define-module (my test)
  |   #:use-module (guix packages)
  |   #:use-module (guix build-system trivial))
  |
  | (define-public my-package-1
  |   (package
  |    (name "my-package-1")
  |    (version "50.0")
  |    (home-page #f)
  |    (synopsis #f)
  |    (description #f)
  |    (license #f)
  |    (source #f)
  |    (build-system trivial-build-system)
  |    (arguments
  |     `(#:modules ((guix build utils))
  |       #:builder
  |       (begin
  |         (use-modules (guix build utils))
  |         (format #t "~a~%" "foo 1")
  |         (format #t "~a~%" %output)
  |         (format #t "~a~%" "foo 2")
  |         (mkdir %output))))))
  `----

  ,----
  | guix package -v3 --no-substitutes -L ~/my/git-repos/guix-packages
-i my-package-1
  | echo Exit code: $?
  `----

  ,----
  | The following package will be installed:
  |    my-package-1 50.0
  |
  | The following derivations will be built:
  |   /gnu/store/d5882590j5w74gwyzl784609wp08hjnl-profile.drv
  |   /gnu/store/fh51w90hsp5pn67wz2k6a14y3ix96kdq-my-package-1-50.0.drv
  | The following profile hooks will be built:
  |    /gnu/store/1mrv6rb283vhjd0kxvbzngy9bdh4nyr5-glib-schemas.drv
  |    /gnu/store/gaz7c0ivgz4ygyg6m0805s4bhv4n09p9-gdk-pixbuf-loaders-cache-file.drv
  |    /gnu/store/hhqw7ykgx3hk2r2p1b4mb5bwbi9syz5k-xdg-mime-database.drv
  |    /gnu/store/hnslzij7j5772zdypb5rxcwzjlm2snj4-ca-certificate-bundle.drv
  |    /gnu/store/j797wxpm81ajszfx08j7yq7643dmx3cv-gtk-im-modules.drv
  |    /gnu/store/jk0qiy0bmy3jp99jhj6bvinly6an4rg4-gtk-icon-themes.drv
  |    /gnu/store/phf48hr7xa7smgc7q4zcksc4nwxi622v-xdg-desktop-database.drv
  |    /gnu/store/qissga15w17swkcdmjfhxnf086nl0gad-info-dir.drv
  |    /gnu/store/x5zxihym559mqhmkbnz0jpxr9ywfyk84-fonts-dir.drv
  |    /gnu/store/xc6nwqvn0ap15ajp5lw9k1k1kf2fc1va-emacs-subdirs.drv
  | building /gnu/store/fh51w90hsp5pn67wz2k6a14y3ix96kdq-my-package-1-50.0.drv...
  | foo 1
  | /gnu/store/i48wxgjpvskkmvpkgscik3vbmsqn8z27-my-package-1-50.0
  | foo 2
  | successfully built
/gnu/store/fh51w90hsp5pn67wz2k6a14y3ix96kdq-my-package-1-50.0.drv
  | building CA certificate bundle...
  | successfully built
/gnu/store/hnslzij7j5772zdypb5rxcwzjlm2snj4-ca-certificate-bundle.drv
  | listing Emacs sub-directories...
  | successfully built
/gnu/store/xc6nwqvn0ap15ajp5lw9k1k1kf2fc1va-emacs-subdirs.drv
  | building fonts directory...
  | successfully built /gnu/store/x5zxihym559mqhmkbnz0jpxr9ywfyk84-fonts-dir.drv
  | generating GdkPixbuf loaders cache...
  | successfully built
/gnu/store/gaz7c0ivgz4ygyg6m0805s4bhv4n09p9-gdk-pixbuf-loaders-cache-file.drv
  | generating GLib schema cache...
  | successfully built
/gnu/store/1mrv6rb283vhjd0kxvbzngy9bdh4nyr5-glib-schemas.drv
  | creating GTK+ icon theme cache...
  | successfully built
/gnu/store/jk0qiy0bmy3jp99jhj6bvinly6an4rg4-gtk-icon-themes.drv
  | building cache files for GTK+ input methods...
  | successfully built
/gnu/store/j797wxpm81ajszfx08j7yq7643dmx3cv-gtk-im-modules.drv
  | building directory of Info manuals...
  | successfully built /gnu/store/qissga15w17swkcdmjfhxnf086nl0gad-info-dir.drv
  | building XDG desktop file cache...
  | successfully built
/gnu/store/phf48hr7xa7smgc7q4zcksc4nwxi622v-xdg-desktop-database.drv
  | building XDG MIME database...
  | successfully built
/gnu/store/hhqw7ykgx3hk2r2p1b4mb5bwbi9syz5k-xdg-mime-database.drv
  | building profile with 19 packages...
  | successfully built /gnu/store/d5882590j5w74gwyzl784609wp08hjnl-profile.drv
  | hint: Consider setting the necessary environment variables by running:
  |
  |      GUIX_PROFILE="/home/rdrg/.guix-profile"
  |      . "$GUIX_PROFILE/etc/profile"
  |
  | Alternately, see `guix package --search-paths -p
  | "/home/rdrg/.guix-profile"'.
  |
  | Exit code: 0
  `----


1.2 Package no. 2: Copy a local file
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

  The following package copies a local file to `~/.guix-profile/share'.

  ,----
  | echo a > ~/e/foo.txt
  `----

  ,----
  | (define-module (my test)
  |   #:use-module (guix gexp)
  |   #:use-module (guix packages)
  |   #:use-module (guix build-system trivial))
  |
  | (define-public my-package-1
  |   (package
  |    (name "my-package-1")
  |    (version "53.0")
  |    (home-page #f)
  |    (synopsis #f)
  |    (description #f)
  |    (license #f)
  |    (source (local-file (string-append "/home/rdrg/e/foo.txt")))
  |    (build-system trivial-build-system)
  |    (arguments
  |     `(#:modules ((guix build utils))
  |       #:builder
  |       (begin
  |         (use-modules (guix build utils))
  |         (let* ((dir (string-append %output "/share"))
  |                (file (string-append dir "/foo.txt")))
  |           (mkdir-p dir)
  |           (copy-file (assoc-ref %build-inputs "source") file)
  |           (format #t "~a~%" "foo 1")
  |           (format #t "%build-inputs: ~a~%" %build-inputs)
  |           (format #t "%output: ~a~%" %output)
  |           (format #t "dir: ~a~%" dir)
  |           (format #t "file: ~a~%" file)
  |           (format #t "~a~%" "foo 2")))))))
  `----

  ,----
  | guix package -v3 --no-substitutes -L ~/my/git-repos/guix-packages
-i my-package-1
  | echo Exit code: $?
  `----

  ,----
  | The following package will be installed:
  |    my-package-1 53.0
  |
  | The following derivations will be built:
  |   /gnu/store/n528dak6y0f9r9zdbd86drysphgyl21h-profile.drv
  |   /gnu/store/pck6w1v3shagpmhw9xzhgjrbz3s09saw-my-package-1-53.0.drv
  | The following profile hooks will be built:
  |    /gnu/store/0w41r9qzf1y0yymx4ym7zmx3lv4r46ds-ca-certificate-bundle.drv
  |    /gnu/store/2qd0kypbs5nb7ma4sdgd3iffj23qfhmb-glib-schemas.drv
  |    /gnu/store/72wpyxjxzdiy5gjw9ji0bwlprcf76407-gtk-icon-themes.drv
  |    /gnu/store/a18j6s911nn46a5d7r2902q32kbvf6wy-xdg-desktop-database.drv
  |    /gnu/store/hl2zcs4d79d9xb2x2wwih27hhbwwmpqn-info-dir.drv
  |    /gnu/store/pgqb8kb71jrplwa7dmaz0rrh6h1i20bl-xdg-mime-database.drv
  |    /gnu/store/w3mcyvm92kn15mr7hcw1mgb2687jhpkv-fonts-dir.drv
  |    /gnu/store/x3ams39s13ykak6ghg3knbyn4rnpw07b-gtk-im-modules.drv
  |    /gnu/store/xmfnb64dxknbnq16rg8sf3wxmri99w9g-gdk-pixbuf-loaders-cache-file.drv
  |    /gnu/store/yh5c44v3pdf1l1j6a9hjqa7l0wacighg-emacs-subdirs.drv
  | building /gnu/store/pck6w1v3shagpmhw9xzhgjrbz3s09saw-my-package-1-53.0.drv...
  | foo 1
  | %build-inputs: ((source .
/gnu/store/bzbxn3r9gq7rhqm565rw9fcy88b8gryy-foo.txt))
  | %output: /gnu/store/hf37gi60wq46qlgzks9hfxln3hx77xms-my-package-1-53.0
  | dir: /gnu/store/hf37gi60wq46qlgzks9hfxln3hx77xms-my-package-1-53.0/share
  | file: /gnu/store/hf37gi60wq46qlgzks9hfxln3hx77xms-my-package-1-53.0/share/foo.txt
  | foo 2
  | successfully built
/gnu/store/pck6w1v3shagpmhw9xzhgjrbz3s09saw-my-package-1-53.0.drv
  | building CA certificate bundle...
  | successfully built
/gnu/store/0w41r9qzf1y0yymx4ym7zmx3lv4r46ds-ca-certificate-bundle.drv
  | listing Emacs sub-directories...
  | successfully built
/gnu/store/yh5c44v3pdf1l1j6a9hjqa7l0wacighg-emacs-subdirs.drv
  | building fonts directory...
  | successfully built /gnu/store/w3mcyvm92kn15mr7hcw1mgb2687jhpkv-fonts-dir.drv
  | generating GdkPixbuf loaders cache...
  | successfully built
/gnu/store/xmfnb64dxknbnq16rg8sf3wxmri99w9g-gdk-pixbuf-loaders-cache-file.drv
  | generating GLib schema cache...
  | successfully built
/gnu/store/2qd0kypbs5nb7ma4sdgd3iffj23qfhmb-glib-schemas.drv
  | creating GTK+ icon theme cache...
  | successfully built
/gnu/store/72wpyxjxzdiy5gjw9ji0bwlprcf76407-gtk-icon-themes.drv
  | building cache files for GTK+ input methods...
  | successfully built
/gnu/store/x3ams39s13ykak6ghg3knbyn4rnpw07b-gtk-im-modules.drv
  | building directory of Info manuals...
  | successfully built /gnu/store/hl2zcs4d79d9xb2x2wwih27hhbwwmpqn-info-dir.drv
  | building XDG desktop file cache...
  | successfully built
/gnu/store/a18j6s911nn46a5d7r2902q32kbvf6wy-xdg-desktop-database.drv
  | building XDG MIME database...
  | successfully built
/gnu/store/pgqb8kb71jrplwa7dmaz0rrh6h1i20bl-xdg-mime-database.drv
  | building profile with 19 packages...
  | successfully built /gnu/store/n528dak6y0f9r9zdbd86drysphgyl21h-profile.drv
  | hint: Consider setting the necessary environment variables by running:
  |
  |      GUIX_PROFILE="/home/rdrg/.guix-profile"
  |      . "$GUIX_PROFILE/etc/profile"
  |
  | Alternately, see `guix package --search-paths -p
  | "/home/rdrg/.guix-profile"'.
  |
  | Exit code: 0
  `----


2 The enquiries
===============

  1. I'm now trying to write a package that creates a temporary file
     containing the letter "a" with "echo > /a/temporary/location.txt"
     and makes it available at =~/.guix-profile/share=. What would this
     package look like?
  2. Could you share other package definitions that have the same level
     of simplicity?

  I opened [a thread] on this topic 3 months ago and someone replied,
  but I didn't have the enough knowledge to understand his
  response. Today, the same question popped up in my mind and I finally
  understood it so I wrote the packages shown above.


[a thread]
<https://lists.gnu.org/archive/html/help-guix/2023-02/msg00146.html>


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

* Re: Let's share examples of simplistic and useless package definitions
  2023-05-26 18:59 Let's share examples of simplistic and useless package definitions Rodrigo Morales
@ 2023-05-28 19:40 ` Skyler Ferris
  0 siblings, 0 replies; 2+ messages in thread
From: Skyler Ferris @ 2023-05-28 19:40 UTC (permalink / raw)
  To: Rodrigo Morales, help-guix

Hi Rodrigo! In a topic adjacent to your second question, I wanted to share
something that I recently learned about using local-file in package 
definitions.
I ran through a similar exercise when building a package that largely 
consisted
of local file, and came upon a similar solution of adding local-file 
forms to the
source field. However, I ran into an issue when including multiple local 
files,
because the name that the build code looks up is just an underscore by 
default
(I do not remember for sure how it handles multiple local files, I think 
it was just
adding an extra underscore for each new file). This is fine with just 
one file as in
your example, but is not particularly readable when you're working with 
multiple
files. The names can be set explicitly, but this syntax is deprecated (see
https://guix.gnu.org/blog/2021/the-big-change/ for a detailed discussion 
of this,
if you aren't already familiar). And unfortunately, you can't mix the 
two syntaxes;
that is, if you are writing a more complex package with both local files and
normal package inputs, you would have to write the normal package inputs
using the deprecated syntax as well, or accept the underscore names.

However, after digging through some upstream package definitions, I learned
that there is an idiom to solve this: there is actually no need to 
include the local
files in the source field, you can include them directly in the build 
definition using
g-expressions!  Written with this idiom, a simple package that just 
copies a file
would look like this:

,---
| (use-modules
|     (guix build-system trivial)
|     (guix gexp)
|     (guix packages)
|     )
|
| (package
|     (name        "copy-file-using-gexp")
|     (version     "0.1")
|     (home-page   #f)
|     (synopsis    #f)
|     (description #f)
|     (license     #f)
|     (source      #f)
|
|     (build-system trivial-build-system)
|     ; NOTE: arguments is now a plain list instead of a quoted list
|     (arguments (list
|         #:modules '((guix build utils))
|         ; NOTE: builder is now defined inside of a gexp, indicated by 
the #~
|         #:builder #~(begin
|             (use-modules (guix build utils))
|
|             (let* ((dir  (string-append %output "/share"))
|                    (file (string-append dir "/foo.txt")))
|                 (mkdir-p dir)
|                 ; NOTE: the local file is now included inline as an 
ungexp'd
|                 ;       form, indicated by the #$
|                 (copy-file #$(local-file "/tmp/foo.txt") file))))))
`---

I hope this was helpful and/or interesting!
- Skyler



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

end of thread, other threads:[~2023-05-28 19:41 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-05-26 18:59 Let's share examples of simplistic and useless package definitions Rodrigo Morales
2023-05-28 19:40 ` Skyler Ferris

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.