Run Guile script with Guix

2026-04-08 published

A simple Guile script that can be executed using Guix. It also contains a dependency on another Guile module that is delivered using Guix package manager.

#!/usr/bin/env -S guix shell guile guile-json -- guile -e main -s

SPDX-License-Identifier: CC0-1.0

Print keys from the JSON file.

An example of executable Guile script that uses dependencies
(particularly guile-json) from Guix package manager.

!#

(define-public (main args)
  (if (not (= (length args) 2))
    (begin
      (displayln (string-append "Usage: " (car args) " file.json"))
      (exit 1))
    (begin
      (displayln (string-append (cadr args) "'s keys:"))
      (displayln
        (list->keys (with-input-from-file (cadr args) json->scm))))))

(use-modules (json))

(define (displayln what)
  (display what)
  (newline))

(define (list->keys li)
  (if (eq? li '())
      "---"
      (begin
        (displayln (caar li))
        (list->keys (cdr li)))))

When run on a file.json that contains:

{
    "a": 100,
    "b": 200
}

the result is:

$ ./print-keys.scm file.json
foo.json's keys:
b
a
---
go back | CC0 1.0