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>
5 ;;; This file is part of GNU Guix.
7 ;;; GNU Guix is free software; you can redistribute it and/or modify it
8 ;;; under the terms of the GNU General Public License as published by
9 ;;; the Free Software Foundation; either version 3 of the License, or (at
10 ;;; your option) any later version.
12 ;;; GNU Guix is distributed in the hope that it will be useful, but
13 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 ;;; GNU General Public License for more details.
17 ;;; You should have received a copy of the GNU General Public License
18 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
20 (define-module (guix scripts repl)
21 #:use-module (guix ui)
22 #:use-module (guix scripts)
23 #:use-module (guix repl)
24 #:use-module (srfi srfi-1)
25 #:use-module (srfi srfi-37)
26 #:use-module (ice-9 match)
27 #:use-module (rnrs bytevectors)
28 #:autoload (system repl repl) (start-repl)
29 #:autoload (system repl server)
30 (make-tcp-server-socket make-unix-domain-server-socket)
35 ;;; This command provides a Guile REPL
37 (define %default-options
41 (list (option '(#\h "help") #f #f
45 (option '(#\V "version") #f #f
47 (show-version-and-exit "guix repl")))
48 (option '(#\t "type") #t #f
49 (lambda (opt name arg result)
50 (alist-cons 'type (string->symbol arg) result)))
51 (option '("listen") #t #f
52 (lambda (opt name arg result)
53 (alist-cons 'listen arg result)))
55 (lambda (opt name arg result)
56 (alist-cons 'ignore-dot-guile? #t result)))
57 (option '(#\L "load-path") #t #f
58 (lambda (opt name arg result)
59 ;; XXX: Imperatively modify the search paths.
60 (set! %load-path (cons arg %load-path))
61 (set! %load-compiled-path (cons arg %load-compiled-path))
66 (display (G_ "Usage: guix repl [OPTIONS...]
67 Start a Guile REPL in the Guix execution environment.\n"))
69 -t, --type=TYPE start a REPL of the given TYPE"))
71 --listen=ENDPOINT listen to ENDPOINT instead of standard input"))
73 -q inhibit loading of ~/.guile"))
76 -L, --load-path=DIR prepend DIR to the package module search path"))
79 -h, --help display this help and exit"))
81 -V, --version display version information and exit"))
83 (show-bug-report-information))
86 ;; Module where we execute user code.
87 (let ((module (resolve-module '(guix-user) #f #f #:ensure #t)))
88 (beautify-user-module! module)
91 (define (call-with-connection spec thunk)
92 "Dynamically-bind the current input and output ports according to SPEC and
97 ;; Note: the "PROTO:" prefix in SPEC is here so that we can eventually
98 ;; parse things like "fd:123" in a non-ambiguous way.
99 (match (string-index spec #\:)
101 (leave (G_ "~A: invalid listen specification~%") spec))
103 (let ((protocol (string-take spec index))
104 (address (string-drop spec (+ index 1))))
108 (make-tcp-server-socket #:port (string->number address)))
110 (make-unix-domain-server-socket #:path address))
112 (leave (G_ "~A: unsupported protocol family~%")
117 (match (accept socket)
118 ((connection . address)
119 (if (= AF_UNIX (sockaddr:fam address))
120 (info (G_ "accepted connection~%"))
121 (info (G_ "accepted connection from ~a~%")
122 (inet-ntop (sockaddr:fam address)
123 (sockaddr:addr address))))
127 (parameterize ((current-input-port connection)
128 (current-output-port connection))
131 (false-if-exception (close-port connection))
132 (info (G_ "connection closed~%"))))))
136 (define (guix-repl . args)
138 ;; Return the list of package names.
139 (args-fold* args %options
140 (lambda (opt name arg result)
141 (leave (G_ "~A: unrecognized option~%") name))
143 (leave (G_ "~A: extraneous argument~%") arg))
147 (and=> (getenv "HOME")
149 (string-append home "/.guile"))))
152 (let ((type (assoc-ref opts 'type)))
153 (call-with-connection (assoc-ref opts 'listen)
157 (save-module-excursion
159 (set-current-module user-module)
160 (when (and (not (assoc-ref opts 'ignore-dot-guile?))
162 (file-exists? user-config))
165 ;; Do not exit repl on SIGINT.
166 ((@@ (ice-9 top-repl) call-with-sigint)
172 (leave (G_ "~a: unknown type of REPL~%") type))))))))
175 ;; eval: (put 'call-with-connection 'scheme-indent-function 1)