1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
| | ;;; GNU Guix --- Functional package management for GNU
;;; Copyright © 2017 Andy Wingo <wingo@pobox.com>
;;;
;;; 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 potluck)
#:use-module (guix config)
#:use-module (guix base32)
#:use-module ((guix build-system) #:select (build-system-description))
#:use-module ((guix licenses) #:select (license-uri))
#:use-module (guix git)
#:use-module (guix ui)
#:use-module (guix utils)
#:use-module (guix potluck build-systems)
#:use-module (guix potluck host)
#:use-module (guix potluck licenses)
#:use-module (guix potluck packages)
#:use-module (guix scripts)
#:use-module (guix scripts hash)
#:use-module (srfi srfi-1)
#:use-module (srfi srfi-34)
#:use-module (srfi srfi-35)
#:use-module (srfi srfi-37)
#:use-module (ice-9 format)
#:use-module (ice-9 match)
#:use-module (ice-9 pretty-print)
#:use-module (json)
#:use-module (web client)
#:use-module (web response)
#:use-module (web uri)
#:export (guix-potluck))
\f
;;;
;;; guix potluck init
;;;
(define* (init-potluck host remote-git-url #:key
(build-system 'gnu) (autoreconf? #f)
(license 'gplv3+))
(let* ((cwd (getcwd))
(dot-git (in-vicinity cwd ".git"))
(potluck-dir (in-vicinity cwd "guix-potluck"))
(package-name (basename cwd)))
(unless (and (file-exists? dot-git)
(file-is-directory? dot-git))
(leave (_ "init: must be run from the root of a git checkout~%")))
(when (file-exists? potluck-dir)
(leave (_ "init: ~a already exists~%") potluck-dir))
(let* ((user-name (git-config "user.name"))
(pkg-name (basename cwd))
(pkg-commit (git-rev-parse "HEAD"))
(pkg-version
(catch #t
(lambda () (git-describe pkg-commit))
(lambda _
(format (current-error-port)
"guix potluck init: git describe failed\n")
(format (current-error-port)
"Add a tag so that git can compute a version.\n")
(exit 1))))
;; FIXME: Race condition if HEAD changes between git-rev-parse and
;; here.
(pkg-sha256 (guix-hash-git-checkout cwd)))
(format #t (_ "Creating guix-potluck/~%"))
(mkdir potluck-dir)
(format #t (_ "Creating guix-potluck/README.md~%"))
(call-with-output-file (in-vicinity potluck-dir "README.md")
(lambda (port)
(format port
"\
This directory defines potluck packages. Each file in this directory should
define one package. See https://guix-potluck.org/ for more information.
")))
(format #t (_ "Creating guix-potluck/~a.scm~%") package-name)
(call-with-output-file (in-vicinity potluck-dir
(string-append package-name ".scm"))
(lambda (port)
(define-syntax-rule (dsp exp) (display exp port))
(dsp ";;; guix potluck package\n")
(dsp ";;; Copyright (C) 2017 ")
(dsp user-name)
(dsp "\n")
(dsp "
;;; This file 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. No warranty. See
;;; https://www.gnu.org/licenses/gpl.html for a copy of the GPLv3.
")
(pretty-print-potluck-package
port
(potluck-package
(name pkg-name)
(version pkg-version)
(source
(potluck-source
(git-uri remote-git-url)
(git-commit pkg-commit)
(sha256 (bytevector->nix-base32-string pkg-sha256))))
(build-system build-system)
(inputs '())
(native-inputs
(if autoreconf?
'("autoconf" "automake" "libtool" "pkg-config")
'()))
(arguments
(if autoreconf?
'(#:phases (modify-phases %standard-phases
(add-before 'configure 'autoconf
(lambda _
(zero?
(system* "autoreconf" "-vfi"))))))
'()))
(home-page remote-git-url)
(synopsis "Declarative synopsis here")
(description
(string-append (string-titlecase pkg-name)
" is a ..."))
(license license)))))
(format #t (_ "
Done. Now open guix-potluck/~a.scm in your editor, fill out its \"synopsis\"
and \"description\" fields, add dependencies to the 'inputs' field, and try to
build with
guix build --file=guix-potluck/~a.scm
When you get that working, commit your results to git via:
git add guix-potluck && git commit -m 'Add initial Guix potluck files.'
Once you push them out, add your dish to the communal potluck by running:
guix potluck update ~a
") pkg-name pkg-name remote-git-url))))
;;;
;;; guix potluck update
;;;
(define (request-potluck-update host git-url branch)
(call-with-values (lambda ()
(http-post (build-uri 'https
#:host host
#:path "/api/enqueue-update")
#:body (scm->json-string
`((git-url . ,git-url)
(branch . ,branch)))))
(lambda (response body)
(unless (eqv? (response-code response) 200)
(error "request failed"
(response-code response)
(response-reason-phrase response)
body)))))
\f
;;;
;;; Options.
;;;
(define (show-help)
(display (_ "Usage: guix potluck [OPTION ...] ACTION [ARG ...]
Create \"potluck\" packages, register them with a central service, and arrange
to serve those packages as a Guix channel. Some ACTIONS require additional
ARGS.\n"))
(newline)
(display (_ "The valid values for ACTION are:\n"))
(newline)
(display (_ "\
init create potluck recipe for current working directory\n"))
(display (_ "\
update ask potluck host to add or update a potluck package\n"))
(display (_ "\
host-channel run web service providing potluck packages as Guix channel\n"))
(newline)
(display (_ "The available OPTION flags are:\n"))
(display (_ "
--host=HOST for 'update' and 'host-channel', the name of the
channel host
(default: guix-potluck.org)"))
(display (_ "
--port=PORT for 'host-channel', the local TCP port on which to
listen for HTTP connections
(default: 8080)"))
(display (_ "
--scratch=DIR for 'host-channel', the path to a local directory
that will be used as a scratch space to check out
remote git repositories"))
(display (_ "
--source=DIR for 'host-channel', the path to a local checkout
of guix potluck source packages to be managed by
host-channel"))
(display (_ "
--target=DIR for 'host-channel', the path to a local checkout
of a guix channel to be managed by host-channel"))
(display (_ "
--build-system=SYS for 'init', specify the build system. Use
--build-system=help for all available options."))
(display (_ "
--autotools for 'init', like --build-system=gnu but additionally
indicating that the package needs autoreconf before
running ./configure"))
(display (_ "
--license=LICENSE for 'init', specify the license of the package. Use
--license=help for all available options."))
(display (_ "
--verbosity=LEVEL use the given verbosity LEVEL"))
(newline)
(display (_ "
-h, --help display this help and exit"))
(display (_ "
-V, --version display version information and exit"))
(newline)
(show-bug-report-information))
(define %options
;; Specifications of the command-line 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 potluck")))
(option '("build-system") #t #f
(lambda (opt name arg result)
(alist-cons 'build-system arg result)))
(option '("autotools") #f #f
(lambda (opt name arg result)
(alist-cons 'autoreconf? #t
(alist-cons 'build-system "gnu" result))))
(option '("license") #t #f
(lambda (opt name arg result)
(alist-cons 'license arg result)))
(option '("host") #t #f
(lambda (opt name arg result)
(alist-cons 'host arg result)))
(option '("port") #t #f
(lambda (opt name arg result)
(alist-cons 'port arg result)))
(option '("scratch") #t #f
(lambda (opt name arg result)
(alist-cons 'scratch arg result)))
(option '("source") #t #f
(lambda (opt name arg result)
(alist-cons 'source arg result)))
(option '("target") #t #f
(lambda (opt name arg result)
(alist-cons 'target arg result)))
(option '("verbosity") #t #f
(lambda (opt name arg result)
(alist-cons 'verbosity (string->number arg) result)))))
(define %default-options
;; Alist of default option values.
`((host . "guix-potluck.org")
(port . "8080")
(verbosity . 0)))
(define (parse-host host-str)
;; Will throw if the host is invalid somehow.
(build-uri 'https #:host host-str)
host-str)
(define (parse-url url-str)
(unless (string->uri url-str)
(leave (_ "invalid url: ~a~%") url-str))
url-str)
(define (parse-port port-str)
(let ((port (string->number port-str)))
(cond
((and port (exact-integer? port) (<= 0 port #xffff))
port)
(else
(leave (_ "invalid port: ~a~%") port-str)))))
(define (parse-absolute-directory-name str)
(unless (and (absolute-file-name? str)
(file-exists? str)
(file-is-directory? str))
(leave (_ "invalid absolute directory name: ~a~%") str))
str)
(define (parse-build-system sys-str)
(unless sys-str
(leave (_ "\
init: missing --build-system; try --build-system=help for options~%")))
(let ((sys (string->symbol (string-downcase sys-str))))
(when (eq? sys 'help)
(format #t "guix potluck: Available build systems:~%")
(for-each
(lambda (name)
(let ((sys (build-system-by-name name)))
(format #t " ~a ~25t~a~%" name (build-system-description sys))))
(all-potluck-build-system-names))
(format #t "
Additionally, --autotools is like --build-system=gnu, but also indicating
that the package needs autoreconf before running ./configure.~%")
(exit 0))
(unless (build-system-by-name sys)
(leave (_ "invalid build system: ~a; try --build-system=help~%") sys))
sys))
(define (parse-license license-str)
(unless license-str
(leave (_ "init: missing --license; try --license=help for options~%")))
(let ((license (string->symbol (string-downcase license-str))))
(when (eq? license 'help)
(format #t "guix potluck: Available licenses:~%")
(for-each
(lambda (name)
(let ((license (license-by-name name)))
(format #t " ~a ~25t~a~%" name (license-uri license))))
(all-potluck-license-names))
(format #t "
If your package's license is not in this list, add it to Guix first.~%")
(exit 0))
(unless (license-by-name license)
(leave (_ "invalid license: ~a; try --license=help~%") license))
license))
\f
;;;
;;; Entry point.
;;;
(define (guix-potluck . args)
(define (parse-sub-command arg result)
(if (assoc-ref result 'action)
(alist-cons 'argument arg result)
(alist-cons 'action (string->symbol arg) result)))
(define (match-pair car)
;; Return a procedure that matches a pair with CAR.
(match-lambda
((head . tail)
(and (eq? car head) tail))
(_ #f)))
(with-error-handling
(let* ((opts (parse-command-line args %options
(list %default-options)
#:argument-handler
parse-sub-command))
(action (assoc-ref opts 'action))
(args (reverse (filter-map (match-pair 'argument) opts))))
(define (see-help)
(format (current-error-port)
(_ "Try 'guix potluck --help' for more information.~%")))
(define (wrong-number-of-args usage)
(format (current-error-port)
(_ "guix potluck ~a: wrong number of arguments~%")
action)
(display usage (current-error-port))
(newline (current-error-port))
(see-help)
(exit 1))
(match action
(#f
(format (current-error-port)
(_ "guix potluck: missing command name~%"))
(see-help)
(exit 1))
('init
(match args
((remote-git-url)
(init-potluck (parse-host (assoc-ref opts 'host))
(parse-url remote-git-url)
#:build-system (parse-build-system
(assoc-ref opts 'build-system))
#:autoreconf? (assoc-ref opts 'autoreconf?)
#:license (parse-license
(assoc-ref opts 'license))))
(args
(wrong-number-of-args
(_ "usage: guix potluck init [OPT...] REMOTE-GIT-URL")))))
('update
(match args
((remote-git-url branch)
(request-potluck-update (parse-host (assoc-ref opts 'host))
(parse-url remote-git-url)
branch))
(args
(wrong-number-of-args
(_ "usage: guix potluck update REMOTE-GIT-URL BRANCH-NAME")))))
('host-channel
(match args
(()
(host-potluck (parse-host (assoc-ref opts 'host))
(parse-port (assoc-ref opts 'port))
(parse-absolute-directory-name
(or (assoc-ref opts 'scratch)
(leave (_ "missing --scratch argument~%"))))
(parse-absolute-directory-name
(or (assoc-ref opts 'source)
(leave (_ "missing --source argument~%"))))
(parse-absolute-directory-name
(or (assoc-ref opts 'target)
(leave (_ "missing --target argument~%"))))))
(args
(wrong-number-of-args
(_ "usage: guix potluck host-channel --scratch=DIR \
--source=DIR --target=DIR"))
(exit 1))))
(action
(leave (_ "~a: unknown action~%") action))))))
|