2018-07-10 08:18:36 -04:00
|
|
|
|
;;; GNU Guix --- Functional package management for GNU
|
2020-01-19 15:54:46 -05:00
|
|
|
|
;;; Copyright © 2018, 2019, 2020 Ludovic Courtès <ludo@gnu.org>
|
2021-03-29 06:18:04 -04:00
|
|
|
|
;;; Copyright © 2020, 2021 Simon Tournier <zimon.toutoune@gmail.com>
|
2020-06-14 03:00:12 -04:00
|
|
|
|
;;; Copyright © 2020 Konrad Hinsen <konrad.hinsen@fastmail.net>
|
2018-07-10 08:18:36 -04:00
|
|
|
|
;;;
|
|
|
|
|
;;; This file is part of GNU Guix.
|
|
|
|
|
;;;
|
|
|
|
|
;;; GNU Guix is free software; you can redistribute it and/or modify it
|
|
|
|
|
;;; under the terms of the GNU General Public License as published by
|
|
|
|
|
;;; the Free Software Foundation; either version 3 of the License, or (at
|
|
|
|
|
;;; your option) any later version.
|
|
|
|
|
;;;
|
|
|
|
|
;;; GNU Guix is distributed in the hope that it will be useful, but
|
|
|
|
|
;;; WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
;;; GNU General Public License for more details.
|
|
|
|
|
;;;
|
|
|
|
|
;;; You should have received a copy of the GNU General Public License
|
|
|
|
|
;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
|
|
(define-module (guix scripts repl)
|
|
|
|
|
#:use-module (guix ui)
|
|
|
|
|
#:use-module (guix scripts)
|
2019-06-10 11:09:35 -04:00
|
|
|
|
#:use-module (guix repl)
|
2018-07-10 08:18:36 -04:00
|
|
|
|
#:use-module (srfi srfi-1)
|
2020-06-14 03:00:12 -04:00
|
|
|
|
#:use-module (srfi srfi-26)
|
2018-07-10 08:18:36 -04:00
|
|
|
|
#:use-module (srfi srfi-37)
|
|
|
|
|
#:use-module (ice-9 match)
|
|
|
|
|
#:use-module (rnrs bytevectors)
|
2020-09-19 10:26:44 -04:00
|
|
|
|
#:autoload (guix describe) (current-profile)
|
2018-07-10 08:18:36 -04:00
|
|
|
|
#:autoload (system repl repl) (start-repl)
|
|
|
|
|
#:autoload (system repl server)
|
|
|
|
|
(make-tcp-server-socket make-unix-domain-server-socket)
|
2019-06-10 11:09:35 -04:00
|
|
|
|
#:export (guix-repl))
|
2018-07-10 08:18:36 -04:00
|
|
|
|
|
|
|
|
|
;;; Commentary:
|
|
|
|
|
;;;
|
2020-06-14 03:00:12 -04:00
|
|
|
|
;;; This command provides a Guile script runner and REPL in an environment
|
|
|
|
|
;;; that contains all the modules comprising Guix.
|
2018-07-10 08:18:36 -04:00
|
|
|
|
|
|
|
|
|
(define %default-options
|
|
|
|
|
`((type . guile)))
|
|
|
|
|
|
|
|
|
|
(define %options
|
|
|
|
|
(list (option '(#\h "help") #f #f
|
|
|
|
|
(lambda args
|
|
|
|
|
(show-help)
|
|
|
|
|
(exit 0)))
|
|
|
|
|
(option '(#\V "version") #f #f
|
|
|
|
|
(lambda args
|
|
|
|
|
(show-version-and-exit "guix repl")))
|
|
|
|
|
(option '(#\t "type") #t #f
|
|
|
|
|
(lambda (opt name arg result)
|
|
|
|
|
(alist-cons 'type (string->symbol arg) result)))
|
2022-12-02 20:09:36 -05:00
|
|
|
|
(option '("list-types") #f #f
|
|
|
|
|
(lambda (opt name arg result)
|
|
|
|
|
(display (string-join '("guile" "machine") "\n" 'suffix))
|
|
|
|
|
(exit 0)))
|
2018-07-10 08:18:36 -04:00
|
|
|
|
(option '("listen") #t #f
|
|
|
|
|
(lambda (opt name arg result)
|
2020-01-15 12:00:05 -05:00
|
|
|
|
(alist-cons 'listen arg result)))
|
2020-01-19 15:54:46 -05:00
|
|
|
|
(option '(#\q) #f #f
|
|
|
|
|
(lambda (opt name arg result)
|
|
|
|
|
(alist-cons 'ignore-dot-guile? #t result)))
|
2022-12-02 20:09:37 -05:00
|
|
|
|
(option '(#\i "interactive") #f #f
|
|
|
|
|
(lambda (opt name arg result)
|
|
|
|
|
(alist-cons 'interactive? #t result)))
|
2020-01-19 16:01:33 -05:00
|
|
|
|
(option '(#\L "load-path") #t #f
|
|
|
|
|
(lambda (opt name arg result)
|
|
|
|
|
;; XXX: Imperatively modify the search paths.
|
|
|
|
|
(set! %load-path (cons arg %load-path))
|
|
|
|
|
(set! %load-compiled-path (cons arg %load-compiled-path))
|
|
|
|
|
result))))
|
2018-07-10 08:18:36 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(define (show-help)
|
2020-06-14 03:00:12 -04:00
|
|
|
|
(display (G_ "Usage: guix repl [OPTIONS...] [-- FILE ARGS...]
|
|
|
|
|
In the Guix execution environment, run FILE as a Guile script with
|
|
|
|
|
command-line arguments ARGS. If no FILE is given, start a Guile REPL.\n"))
|
2022-12-02 20:09:36 -05:00
|
|
|
|
(display (G_ "
|
|
|
|
|
--list-types display REPL types and exit"))
|
2018-07-10 08:18:36 -04:00
|
|
|
|
(display (G_ "
|
|
|
|
|
-t, --type=TYPE start a REPL of the given TYPE"))
|
2020-01-15 12:00:06 -05:00
|
|
|
|
(display (G_ "
|
2020-01-19 15:56:04 -05:00
|
|
|
|
--listen=ENDPOINT listen to ENDPOINT instead of standard input"))
|
2020-01-19 15:54:46 -05:00
|
|
|
|
(display (G_ "
|
|
|
|
|
-q inhibit loading of ~/.guile"))
|
2020-01-15 12:00:06 -05:00
|
|
|
|
(newline)
|
2020-01-15 12:00:05 -05:00
|
|
|
|
(display (G_ "
|
2022-12-02 20:09:37 -05:00
|
|
|
|
-i, --interactive launch REPL after evaluating FILE"))
|
|
|
|
|
(newline)
|
|
|
|
|
(display (G_ "
|
2020-01-15 12:00:05 -05:00
|
|
|
|
-L, --load-path=DIR prepend DIR to the package module search path"))
|
2018-07-10 08:18:36 -04:00
|
|
|
|
(newline)
|
|
|
|
|
(display (G_ "
|
|
|
|
|
-h, --help display this help and exit"))
|
|
|
|
|
(display (G_ "
|
|
|
|
|
-V, --version display version information and exit"))
|
|
|
|
|
(newline)
|
|
|
|
|
(show-bug-report-information))
|
|
|
|
|
|
|
|
|
|
(define user-module
|
|
|
|
|
;; Module where we execute user code.
|
|
|
|
|
(let ((module (resolve-module '(guix-user) #f #f #:ensure #t)))
|
|
|
|
|
(beautify-user-module! module)
|
|
|
|
|
module))
|
|
|
|
|
|
|
|
|
|
(define (call-with-connection spec thunk)
|
|
|
|
|
"Dynamically-bind the current input and output ports according to SPEC and
|
|
|
|
|
call THUNK."
|
|
|
|
|
(if (not spec)
|
|
|
|
|
(thunk)
|
|
|
|
|
|
|
|
|
|
;; Note: the "PROTO:" prefix in SPEC is here so that we can eventually
|
|
|
|
|
;; parse things like "fd:123" in a non-ambiguous way.
|
|
|
|
|
(match (string-index spec #\:)
|
|
|
|
|
(#f
|
|
|
|
|
(leave (G_ "~A: invalid listen specification~%") spec))
|
|
|
|
|
(index
|
|
|
|
|
(let ((protocol (string-take spec index))
|
|
|
|
|
(address (string-drop spec (+ index 1))))
|
|
|
|
|
(define socket
|
|
|
|
|
(match protocol
|
|
|
|
|
("tcp"
|
|
|
|
|
(make-tcp-server-socket #:port (string->number address)))
|
|
|
|
|
("unix"
|
|
|
|
|
(make-unix-domain-server-socket #:path address))
|
|
|
|
|
(_
|
|
|
|
|
(leave (G_ "~A: unsupported protocol family~%")
|
|
|
|
|
protocol))))
|
|
|
|
|
|
|
|
|
|
(listen socket 10)
|
|
|
|
|
(let loop ()
|
|
|
|
|
(match (accept socket)
|
|
|
|
|
((connection . address)
|
|
|
|
|
(if (= AF_UNIX (sockaddr:fam address))
|
|
|
|
|
(info (G_ "accepted connection~%"))
|
|
|
|
|
(info (G_ "accepted connection from ~a~%")
|
|
|
|
|
(inet-ntop (sockaddr:fam address)
|
|
|
|
|
(sockaddr:addr address))))
|
|
|
|
|
(dynamic-wind
|
|
|
|
|
(const #t)
|
|
|
|
|
(lambda ()
|
|
|
|
|
(parameterize ((current-input-port connection)
|
|
|
|
|
(current-output-port connection))
|
|
|
|
|
(thunk)))
|
|
|
|
|
(lambda ()
|
|
|
|
|
(false-if-exception (close-port connection))
|
|
|
|
|
(info (G_ "connection closed~%"))))))
|
|
|
|
|
(loop)))))))
|
|
|
|
|
|
|
|
|
|
|
2020-09-01 16:13:11 -04:00
|
|
|
|
(define-command (guix-repl . args)
|
|
|
|
|
(category plumbing)
|
|
|
|
|
(synopsis "read-eval-print loop (REPL) for interactive programming")
|
|
|
|
|
|
2018-07-10 08:18:36 -04:00
|
|
|
|
(define opts
|
2021-03-29 06:18:04 -04:00
|
|
|
|
(parse-command-line args %options (list %default-options)
|
|
|
|
|
#:build-options? #f
|
|
|
|
|
#:argument-handler
|
2018-07-10 08:18:36 -04:00
|
|
|
|
(lambda (arg result)
|
2020-06-14 03:00:12 -04:00
|
|
|
|
(append `((script . ,arg)
|
2020-06-17 16:22:12 -04:00
|
|
|
|
(ignore-dot-guile? . #t))
|
2021-03-29 06:18:04 -04:00
|
|
|
|
result))))
|
2018-07-10 08:18:36 -04:00
|
|
|
|
|
2020-01-19 15:54:46 -05:00
|
|
|
|
(define user-config
|
|
|
|
|
(and=> (getenv "HOME")
|
|
|
|
|
(lambda (home)
|
|
|
|
|
(string-append home "/.guile"))))
|
|
|
|
|
|
2020-06-14 03:00:12 -04:00
|
|
|
|
(define (set-user-module)
|
|
|
|
|
(set-current-module user-module)
|
|
|
|
|
(when (and (not (assoc-ref opts 'ignore-dot-guile?))
|
|
|
|
|
user-config
|
|
|
|
|
(file-exists? user-config))
|
|
|
|
|
(load user-config)))
|
|
|
|
|
|
|
|
|
|
(define script
|
|
|
|
|
(reverse
|
|
|
|
|
(filter-map (match-lambda
|
|
|
|
|
(('script . script) script)
|
|
|
|
|
(_ #f))
|
|
|
|
|
opts)))
|
|
|
|
|
|
2018-07-10 08:18:36 -04:00
|
|
|
|
(with-error-handling
|
2020-06-14 03:00:12 -04:00
|
|
|
|
|
|
|
|
|
(unless (null? script)
|
|
|
|
|
;; Run script
|
|
|
|
|
(save-module-excursion
|
|
|
|
|
(lambda ()
|
2020-09-19 10:26:44 -04:00
|
|
|
|
;; Invoke 'current-profile' so that it memoizes the correct value
|
|
|
|
|
;; based on (program-arguments), before we call
|
|
|
|
|
;; 'set-program-arguments'. This in turn ensures that
|
|
|
|
|
;; (%package-module-path) will contain entries for the channels
|
|
|
|
|
;; available in the current profile.
|
|
|
|
|
(current-profile)
|
|
|
|
|
|
2020-06-14 03:00:12 -04:00
|
|
|
|
(set-program-arguments script)
|
|
|
|
|
(set-user-module)
|
2020-09-11 07:13:26 -04:00
|
|
|
|
|
|
|
|
|
;; When passed a relative file name, 'load-in-vicinity' searches the
|
|
|
|
|
;; file in %LOAD-PATH. Thus, pass (getcwd) instead of ".".
|
|
|
|
|
(load-in-vicinity (getcwd) (car script)))))
|
2020-06-14 03:00:12 -04:00
|
|
|
|
|
2022-12-02 20:09:37 -05:00
|
|
|
|
(when (or (null? script) (assoc-ref opts 'interactive?))
|
2020-06-14 03:00:12 -04:00
|
|
|
|
;; Start REPL
|
|
|
|
|
(let ((type (assoc-ref opts 'type)))
|
|
|
|
|
(call-with-connection (assoc-ref opts 'listen)
|
|
|
|
|
(lambda ()
|
|
|
|
|
(case type
|
|
|
|
|
((guile)
|
|
|
|
|
(save-module-excursion
|
|
|
|
|
(lambda ()
|
2023-02-17 09:56:27 -05:00
|
|
|
|
(current-profile) ;populate (%package-module-path); see above
|
2020-06-14 03:00:12 -04:00
|
|
|
|
(set-user-module)
|
|
|
|
|
;; Do not exit repl on SIGINT.
|
|
|
|
|
((@@ (ice-9 top-repl) call-with-sigint)
|
|
|
|
|
(lambda ()
|
|
|
|
|
(start-repl))))))
|
|
|
|
|
((machine)
|
|
|
|
|
(machine-repl))
|
|
|
|
|
(else
|
|
|
|
|
(leave (G_ "~a: unknown type of REPL~%") type)))))))))
|
2018-07-10 08:18:36 -04:00
|
|
|
|
|
|
|
|
|
;; Local Variables:
|
|
|
|
|
;; eval: (put 'call-with-connection 'scheme-indent-function 1)
|
|
|
|
|
;; End:
|