unofficial mirror of help-guix@gnu.org 
 help / color / mirror / Atom feed
From: Timothy Sample <samplet@ngyro.com>
To: "Pierre-Henry F." <contact@phfrohring.com>
Cc: "help-guix@gnu.org" <help-guix@gnu.org>
Subject: Re: Help defining a trivial package.
Date: Thu, 29 Aug 2019 11:26:25 -0400	[thread overview]
Message-ID: <87v9ug2dsu.fsf@ngyro.com> (raw)
In-Reply-To: <RyEPWUBJtW5MOb40O1mOc3d_xJVtWWo7EJEXla_lrO44pepYMM0ezk11SGsSwAdpp6muV98vKnN3eoytfWnoK6ElfJf31Tjl8f9R7ySrBkg=@phfrohring.com> (Pierre-Henry F.'s message of "Thu, 29 Aug 2019 11:23:26 +0000")

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

Hi Pierre,

"Pierre-Henry F." <contact@phfrohring.com> writes:

> So, we have, in ~bash~:
>
>   $ lzip --decompress release_3.tar.lz
>   $ tar xf release_3.tar
>   $ cd blog/ # coming from the preceding line.
>   $ tree
>   .
>   ├── bin
>   │   └── program
>   └── src
>       └── hello_world.py
>
>   $ cat src/hello_world.py
>   print("Hello world!")
>
>   $ cat bin/program
>   #! /usr/bin/env bash
>
>   script_path="${BASH_SOURCE[0]}"
>   script_dir="$(dirname $script_path)"
>   python3 "$script_dir/../src/hello_world.py"
>
> That's it.
>
> The idea is to build a Guix package that does exactly that.
> Expected outcome:
>
>   $ guix package -s blog
>   # fetch release_3.tar.lz
>   # uncompress ...
>   # ...
>
>   $ program
>   Hello World!
>
>
> My question: how to build that Guix package?  I try to get something working using
> the docs.  This is how the package looks to far:
>
>   (use-modules
>     (guix packages)
>     (guix download)
>     (guix build-system trivial)
>     (guix licenses)
>     (guix gexp)
>     (gnu packages base)
>     (gnu packages python))
>
>
>   ;; The "derivation" i.e. low level sequence of instructions that the build deamon is
>   ;; supposed to execute on the behalf of the user.
>   (define build-drv
>     (gexp->derivation
>       "the-thing"
>       #~(begin
>           (mkdir #$output)
>           (chdir #$output)
>           (symlink (string-append #$coreutils "/bin/ls")
>             "list-files"))))
>
>
>   (package
>     (name "blog")
>     (version "3")
>     (source
>       (origin
>         (method url-fetch)
>         (uri (string-append "/home/phf/programs/blog/releases/release_" version ".tar.lz"))
>         (sha256
>           (base32
>             "1y819b53ksyas6asldysr0r8p73n5i8ipbpmbgjrfx8qz8cy2zsx"))))
>     (build-system trivial-build-system)
>     (arguments `(#:builder ,build-drv))
>
>     (inputs `(("python" ,python)))
>     (synopsis "Guix 'hello world' to learn about Guix")
>     (description "Guess what GNU Hello prints!")
>     (home-page "http://www.gnu.org/software/hello/")
>     (license gpl3+))
>
> Assuming everything else is correct, would you please help refine ~build-drv~ until
> it matches the ~bash~ above ?
>
> This is what the execution trace looks like:
>
>   phf@f02c:~/tools/guix/packages$ guix build --keep-failed --verbosity=2 --file=./blog.scm
>   substitute: updating substitutes from 'https://ci.guix.gnu.org'... 100.0%
>   building /gnu/store/8b0gyazhgmc9rkrxir7vxpav0x28xk3d-blog-3.drv...
>   ERROR: In procedure primitive-load:
>   In procedure scm_lreadr: /gnu/store/zlbf2x6n4084v0cpw2rh9dydqmi5b2rn-blog-3-guile-builder:1:10: Unknown # object: #\<

This error is because you can’t pass a derivation object as the
“#:builder” argument.  It has to be some quoted Scheme code that can run
in the build environment.

I took a little time and wrote a package definition that matches your
example.  There are some things you could do to change your source code
to make it more in-line with what Guix expects, but I didn’t do that.  I
hope that an example dealing with what you have will be more helpful.

I’ve attached the package definition I came up with (note that I changed
the URL of the tarball).


[-- Attachment #2: phf.scm --]
[-- Type: text/plain, Size: 1668 bytes --]

(use-modules ((gnu packages base) #:select (tar))
             ((gnu packages bash) #:select (bash-minimal))
             ((gnu packages compression) #:select (lzip))
             ((gnu packages python) #:select (python))
             (guix)
             (guix build-system trivial))

(package
  (name "blog")
  (version "3")
  (source
   (origin
     (method url-fetch)
     (uri (string-append "file:///tmp/phf/release_" version ".tar.lz"))
     (sha256
      (base32
       "1y819b53ksyas6asldysr0r8p73n5i8ipbpmbgjrfx8qz8cy2zsx"))))
  (build-system trivial-build-system)
  (arguments
   `(#:modules ((guix build utils))
     #:builder
     (begin
       (use-modules (guix build utils))
       ;; Unpack
       (let ((source (assoc-ref %build-inputs "source"))
             (tar (assoc-ref %build-inputs "tar"))
             (lzip (assoc-ref %build-inputs "lzip")))
         (setenv "PATH" (string-append tar "/bin"
                                       ":" lzip "/bin"))
         (invoke "tar" "--lzip" "-xvf" source))
       ;; Configure
       (let ((bash (assoc-ref %build-inputs "bash"))
             (python (assoc-ref %build-inputs "python")))
         (substitute* "blog/bin/program"
           (("/usr/bin/env bash") (string-append bash "/bin/bash"))
           (("python3") (string-append python "/bin/python3"))))
       ;; Install
       (let ((out (assoc-ref %outputs "out")))
         (chmod "blog/bin/program" #o775)
         (copy-recursively "blog" out)))))
  (inputs
   `(("bash" ,bash-minimal)
     ("python" ,python)))
  (native-inputs
   `(("lzip" ,lzip)
     ("tar" ,tar)))
  (synopsis #f)
  (description #f)
  (home-page #f)
  (license #f))

[-- Attachment #3: Type: text/plain, Size: 684 bytes --]


The builder code imports the “(guix build utils)” module, which has some
really handy shell-like functions like “invoke” for running programs,
“substitute*” which is like “sed -i”, and “copy-recursively”, which is
like “cp -r”.

The “Unpack” section sets up the “PATH” variable for Tar, and then
invokes “tar” to unpack the tarball.

The “Configure” section replaces the references to Bash and Python with
their absolute paths in the Guix store.

The “Install” section makes the “program” file executable then copies
the code into the Guix-provided output directory.

I hope that makes sense and good luck!


-- Tim

  reply	other threads:[~2019-08-29 15:26 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-08-23 19:42 Help defining a trivial package Pierre-Henry F.
2019-08-25 12:17 ` Timothy Sample
2019-08-29 11:23   ` Pierre-Henry F.
2019-08-29 15:26     ` Timothy Sample [this message]
     [not found]       ` <Al-P-mUDUQnoUvF4kgNS_NeITvHjEDEvxG8Gmk9aMiXX2FTUfL0lbZAq5h4m54GIGNXPwM271CzLWwt5sgOt1JI6Z1wxqxSwWmpnPHu7I5A=@phfrohring.com>
2019-09-03  1:58         ` Timothy Sample
2019-09-04 11:21           ` Pierre-Henry F.
2019-09-04 17:34             ` Timothy Sample

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

  List information: https://guix.gnu.org/

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

  git send-email \
    --in-reply-to=87v9ug2dsu.fsf@ngyro.com \
    --to=samplet@ngyro.com \
    --cc=contact@phfrohring.com \
    --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.
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).