[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Let's share examples of simplistic and useless package definitions
From: |
Skyler Ferris |
Subject: |
Re: Let's share examples of simplistic and useless package definitions |
Date: |
Sun, 28 May 2023 19:40:40 +0000 |
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