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 scripts build) #:select (%standard-build-options))
24 #:use-module (guix repl)
25 #:use-module (guix utils)
26 #:use-module (guix packages)
27 #:use-module (gnu packages)
28 #:use-module (srfi srfi-1)
29 #:use-module (srfi srfi-37)
30 #:use-module (ice-9 match)
31 #:use-module (rnrs bytevectors)
32 #:autoload (system repl repl) (start-repl)
33 #:autoload (system repl server)
34 (make-tcp-server-socket make-unix-domain-server-socket)
39 ;;; This command provides a Guile REPL
41 (define %default-options
45 (list (option '(#\h "help") #f #f
49 (option '(#\V "version") #f #f
51 (show-version-and-exit "guix repl")))
52 (option '(#\t "type") #t #f
53 (lambda (opt name arg result)
54 (alist-cons 'type (string->symbol arg) result)))
55 (option '("listen") #t #f
56 (lambda (opt name arg result)
57 (alist-cons 'listen arg result)))
59 (lambda (opt name arg result)
60 (alist-cons 'ignore-dot-guile? #t result)))
61 (find (lambda (option)
62 (member "load-path" (option-names option)))
63 %standard-build-options)))
67 (display (G_ "Usage: guix repl [OPTIONS...]
68 Start a Guile REPL in the Guix execution environment.\n"))
70 -t, --type=TYPE start a REPL of the given TYPE"))
72 --listen=ENDPOINT listen ENDPOINT instead of standard I/O"))
74 -q inhibit loading of ~/.guile"))
77 -L, --load-path=DIR prepend DIR to the package module search path"))
80 -h, --help display this help and exit"))
82 -V, --version display version information and exit"))
84 (show-bug-report-information))
87 ;; Module where we execute user code.
88 (let ((module (resolve-module '(guix-user) #f #f #:ensure #t)))
89 (beautify-user-module! module)
92 (define (call-with-connection spec thunk)
93 "Dynamically-bind the current input and output ports according to SPEC and
98 ;; Note: the "PROTO:" prefix in SPEC is here so that we can eventually
99 ;; parse things like "fd:123" in a non-ambiguous way.
100 (match (string-index spec #\:)
102 (leave (G_ "~A: invalid listen specification~%") spec))
104 (let ((protocol (string-take spec index))
105 (address (string-drop spec (+ index 1))))
109 (make-tcp-server-socket #:port (string->number address)))
111 (make-unix-domain-server-socket #:path address))
113 (leave (G_ "~A: unsupported protocol family~%")
118 (match (accept socket)
119 ((connection . address)
120 (if (= AF_UNIX (sockaddr:fam address))
121 (info (G_ "accepted connection~%"))
122 (info (G_ "accepted connection from ~a~%")
123 (inet-ntop (sockaddr:fam address)
124 (sockaddr:addr address))))
128 (parameterize ((current-input-port connection)
129 (current-output-port connection))
132 (false-if-exception (close-port connection))
133 (info (G_ "connection closed~%"))))))
137 (define (guix-repl . args)
139 ;; Return the list of package names.
140 (args-fold* args %options
141 (lambda (opt name arg result)
142 (leave (G_ "~A: unrecognized option~%") name))
144 (leave (G_ "~A: extraneous argument~%") arg))
148 (and=> (getenv "HOME")
150 (string-append home "/.guile"))))
153 (let ((type (assoc-ref opts 'type)))
154 (call-with-connection (assoc-ref opts 'listen)
158 (save-module-excursion
160 (set-current-module user-module)
161 (when (and (not (assoc-ref opts 'ignore-dot-guile?))
163 (file-exists? user-config))
166 ;; Do not exit repl on SIGINT.
167 ((@@ (ice-9 top-repl) call-with-sigint)
173 (leave (G_ "~a: unknown type of REPL~%") type))))))))
176 ;; eval: (put 'call-with-connection 'scheme-indent-function 1)