1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2018, 2019, 2020 Ludovic Courtès <ludo@gnu.org>
3 ;;; Copyright © 2020 Simon Tournier <zimon.toutoune@gmail.com>
4 ;;; Copyright © 2020 Konrad Hinsen <konrad.hinsen@fastmail.net>
6 ;;; This file is part of GNU Guix.
8 ;;; GNU Guix is free software; you can redistribute it and/or modify it
9 ;;; under the terms of the GNU General Public License as published by
10 ;;; the Free Software Foundation; either version 3 of the License, or (at
11 ;;; your option) any later version.
13 ;;; GNU Guix is distributed in the hope that it will be useful, but
14 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ;;; GNU General Public License for more details.
18 ;;; You should have received a copy of the GNU General Public License
19 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
21 (define-module (guix scripts repl)
22 #:use-module (guix ui)
23 #:use-module (guix scripts)
24 #:use-module (guix repl)
25 #:use-module (srfi srfi-1)
26 #:use-module (srfi srfi-26)
27 #:use-module (srfi srfi-37)
28 #:use-module (ice-9 match)
29 #:use-module (rnrs bytevectors)
30 #:autoload (system repl repl) (start-repl)
31 #:autoload (system repl server)
32 (make-tcp-server-socket make-unix-domain-server-socket)
37 ;;; This command provides a Guile script runner and REPL in an environment
38 ;;; that contains all the modules comprising Guix.
40 (define %default-options
44 (list (option '(#\h "help") #f #f
48 (option '(#\V "version") #f #f
50 (show-version-and-exit "guix repl")))
51 (option '(#\t "type") #t #f
52 (lambda (opt name arg result)
53 (alist-cons 'type (string->symbol arg) result)))
54 (option '("listen") #t #f
55 (lambda (opt name arg result)
56 (alist-cons 'listen arg result)))
58 (lambda (opt name arg result)
59 (alist-cons 'ignore-dot-guile? #t result)))
60 (option '(#\L "load-path") #t #f
61 (lambda (opt name arg result)
62 ;; XXX: Imperatively modify the search paths.
63 (set! %load-path (cons arg %load-path))
64 (set! %load-compiled-path (cons arg %load-compiled-path))
69 (display (G_ "Usage: guix repl [OPTIONS...] [-- FILE ARGS...]
70 In the Guix execution environment, run FILE as a Guile script with
71 command-line arguments ARGS. If no FILE is given, start a Guile REPL.\n"))
73 -t, --type=TYPE start a REPL of the given TYPE"))
75 --listen=ENDPOINT listen to ENDPOINT instead of standard input"))
77 -q inhibit loading of ~/.guile"))
80 -L, --load-path=DIR prepend DIR to the package module search path"))
83 -h, --help display this help and exit"))
85 -V, --version display version information and exit"))
87 (show-bug-report-information))
90 ;; Module where we execute user code.
91 (let ((module (resolve-module '(guix-user) #f #f #:ensure #t)))
92 (beautify-user-module! module)
95 (define (call-with-connection spec thunk)
96 "Dynamically-bind the current input and output ports according to SPEC and
101 ;; Note: the "PROTO:" prefix in SPEC is here so that we can eventually
102 ;; parse things like "fd:123" in a non-ambiguous way.
103 (match (string-index spec #\:)
105 (leave (G_ "~A: invalid listen specification~%") spec))
107 (let ((protocol (string-take spec index))
108 (address (string-drop spec (+ index 1))))
112 (make-tcp-server-socket #:port (string->number address)))
114 (make-unix-domain-server-socket #:path address))
116 (leave (G_ "~A: unsupported protocol family~%")
121 (match (accept socket)
122 ((connection . address)
123 (if (= AF_UNIX (sockaddr:fam address))
124 (info (G_ "accepted connection~%"))
125 (info (G_ "accepted connection from ~a~%")
126 (inet-ntop (sockaddr:fam address)
127 (sockaddr:addr address))))
131 (parameterize ((current-input-port connection)
132 (current-output-port connection))
135 (false-if-exception (close-port connection))
136 (info (G_ "connection closed~%"))))))
140 (define-command (guix-repl . args)
142 (synopsis "read-eval-print loop (REPL) for interactive programming")
145 (args-fold* args %options
146 (lambda (opt name arg result)
147 (leave (G_ "~A: unrecognized option~%") name))
149 (append `((script . ,arg)
150 (ignore-dot-guile? . #t))
155 (and=> (getenv "HOME")
157 (string-append home "/.guile"))))
159 (define (set-user-module)
160 (set-current-module user-module)
161 (when (and (not (assoc-ref opts 'ignore-dot-guile?))
163 (file-exists? user-config))
168 (filter-map (match-lambda
169 (('script . script) script)
175 (unless (null? script)
177 (save-module-excursion
179 (set-program-arguments script)
182 ;; When passed a relative file name, 'load-in-vicinity' searches the
183 ;; file in %LOAD-PATH. Thus, pass (getcwd) instead of ".".
184 (load-in-vicinity (getcwd) (car script)))))
188 (let ((type (assoc-ref opts 'type)))
189 (call-with-connection (assoc-ref opts 'listen)
193 (save-module-excursion
196 ;; Do not exit repl on SIGINT.
197 ((@@ (ice-9 top-repl) call-with-sigint)
203 (leave (G_ "~a: unknown type of REPL~%") type)))))))))
206 ;; eval: (put 'call-with-connection 'scheme-indent-function 1)