Guix Scripting

To define a module and export its defined methods:

1
2
3
4
5
(define-module (conman utilities)
#:export (find-occurences-in-string
any-not-false?
to-regular-char
))

Note that when you use (use-module (conman utilities)) that the module designation is essentially a path to the .scm. So in this example the system expects to find somewhere on the %load-path the directory conman with the utilities.scm file in it. To append the working directory to the load path, one of the very first commands should be (add-to-load-path "/home/mbc/projects"). If the file instead was located at /home/mbc/projects/conman/web/utilities.scm, then I would have to use (use-module (conman web utilities))

To run as a script in a protected Guix environment:

1
guix environment --pure --network --expose=/etc/ssl/certs/  --manifest=manifest.scm -- guile -e main -s conman.scm 7 2

In this example the args passed into main are ‘(‘conman.scm’ ‘7’ ‘2’)

manifest.scm
1
2
3
(specifications->manifest
'("guile@3.0.4" "guile-lib" "coreutils" "gawk" "sed" "findutils" "glibc" "grep" "bash-minimal" "openssl" "gnutls" "curl" "emacs" "libcanberra" "guile-dbi" "glibc-locales" "glibc-utf8-locales"))

The script comman can be shortened by placing some of the commands in the script file. This may make the script computer specific - i.e. the hash for the Guile executable might be unique for each computer. Determine what the Guile executable is by looking in /gnu/store:

1
2
mbc@xps:~$ find /gnu/store -wholename *guile-3.0.4/bin/guile
/gnu/store/0w76khfspfy8qmcpjya41chj3bgfcy0k-guile-3.0.4/bin/guile

Include the executable at the top of the script. See https://www.gnu.org/software/guile/manual/html_node/The-Meta-Switch.html

conman.scm
1
2
3
4
#!/gnu/store/0w76khfspfy8qmcpjya41chj3bgfcy0k-guile-3.0.4/bin/guile \
-e main -s
!#

Now an abridged command can be used in the terminal:

1
2
mbc@xps:~/projects/conman$ guix environment --pure --network --expose=/etc/ssl/certs/  --manifest=manifest.scm -- ./conman.scm 7 2

1

Share