2 # aside from this initial boilerplate, this is actually -*- scheme -*- code
5 datarootdir
="@datarootdir@"
7 GUILE_LOAD_COMPILED_PATH
="@guilemoduledir@:$GUILE_LOAD_COMPILED_PATH"
8 export GUILE_LOAD_COMPILED_PATH
10 main
='(module-ref (resolve-interface '\''(guix-package)) '\'guix-package
')'
11 exec ${GUILE-@GUILE@} -L "@guilemoduledir@" -l "$0" \
12 -c "(apply $main (cdr (command-line)))" "$@"
14 ;;; GNU Guix
--- Functional package management
for GNU
15 ;;; Copyright ©
2012, 2013 Ludovic Courtès
<ludo@gnu.org
>
16 ;;; Copyright ©
2013 Nikita Karetnikov
<nikita@karetnikov.org
>
18 ;;; This
file is part of GNU Guix.
20 ;;; GNU Guix is free software
; you can redistribute it and
/or modify it
21 ;;; under the terms of the GNU General Public License as published by
22 ;;; the Free Software Foundation
; either version
3 of the License
, or
(at
23 ;;; your option
) any later version.
25 ;;; GNU Guix is distributed
in the hope that it will be useful
, but
26 ;;; WITHOUT ANY WARRANTY
; without even the implied warranty of
27 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 ;;; GNU General Public License
for more details.
30 ;;; You should have received a copy of the GNU General Public License
31 ;;; along with GNU Guix. If not
, see
<http
://www.gnu.org
/licenses
/>.
33 (define-module
(guix-package
)
34 #:use-module (guix ui)
35 #:use-module (guix store)
36 #:use-module (guix derivations)
37 #:use-module (guix packages)
38 #:use-module (guix utils)
39 #:use-module (guix config)
40 #:use-module ((guix build utils) #:select (directory-exists? mkdir-p))
41 #:use-module (ice-9 ftw)
42 #:use-module (ice-9 format)
43 #:use-module (ice-9 match)
44 #:use-module (ice-9 regex)
45 #:use-module (srfi srfi-1)
46 #:use-module (srfi srfi-11)
47 #:use-module (srfi srfi-26)
48 #:use-module (srfi srfi-34)
49 #:use-module (srfi srfi-37)
50 #:use-module (gnu packages)
51 #:use-module ((gnu packages base) #:select (guile-final))
52 #:use-module ((gnu packages bootstrap) #:select (%bootstrap-guile))
53 #:export (guix-package))
63 (define
%user-environment-directory
64 (and
=> (getenv
"HOME")
65 (cut string-append
<> "/.guix-profile")))
67 (define
%profile-directory
68 (string-append
(or
(getenv
"NIX_STATE_DIR") %state-directory
) "/profiles/"
69 (or
(and
=> (getenv
"USER")
70 (cut string-append
"per-user/" <>))
73 (define
%current-profile
74 ;; Call it
`guix-profile', not `profile
', to allow Guix profiles to
75 ;; coexist with Nix profiles.
76 (string-append %profile-directory "/guix-profile"))
78 (define (profile-manifest profile)
79 "Return the PROFILE's manifest.
"
80 (let ((manifest (string-append profile "/manifest
")))
81 (if (file-exists? manifest)
82 (call-with-input-file manifest read)
83 '(manifest (version 0) (packages ())))))
85 (define (manifest-packages manifest)
86 "Return the packages listed
in MANIFEST.
"
88 (('manifest ('version 0) ('packages packages))
91 (error "unsupported manifest format
" manifest))))
93 (define (profile-regexp profile)
94 "Return a regular expression that matches PROFILE
's name and number."
95 (make-regexp (string-append "^" (regexp-quote (basename profile))
98 (define (profile-numbers profile)
99 "Return the list of generation numbers of PROFILE, or '(0) if no
100 former profiles were found.
"
101 (define* (scandir name #:optional (select? (const #t))
102 (entry<? (@ (ice-9 i18n) string-locale<?)))
103 ;; XXX: Bug-fix version introduced in Guile v2.0.6-62-g139ce19.
104 (define (enter? dir stat result)
105 (and stat (string=? dir name)))
107 (define (visit basename result)
108 (if (select? basename)
109 (cons basename result)
112 (define (leaf name stat result)
114 (visit (basename name) result)))
116 (define (down name stat result)
119 (define (up name stat result)
122 (define (skip name stat result)
123 ;; All the sub-directories are skipped.
124 (visit (basename name) result))
126 (define (error name* stat errno result)
127 (if (string=? name name*) ; top-level NAME is unreadable
129 (visit (basename name*) result)))
131 (and=> (file-system-fold enter? leaf down up skip error #f name lstat)
133 (sort files entry<?))))
135 (match (scandir (dirname profile)
136 (cute regexp-exec (profile-regexp profile) <>))
137 (#f ; no profile directory
141 ((profiles ...) ; former profiles around
142 (map (compose string->number
143 (cut match:substring <> 1)
144 (cute regexp-exec (profile-regexp profile) <>))
147 (define (latest-profile-number profile)
148 "Return the identifying number of the latest generation of PROFILE.
149 PROFILE is the name of the symlink to the current generation.
"
150 (fold (lambda (number highest)
151 (if (> number highest)
155 (profile-numbers profile)))
157 (define (previous-profile-number profile number)
158 "Return the number of the generation before generation NUMBER of
159 PROFILE
, or
0 if none exists. It could be NUMBER
- 1, but it
's not the
160 case when generations have been deleted (there are \"holes\")."
161 (fold (lambda (candidate highest)
162 (if (and (< candidate number) (> candidate highest))
166 (profile-numbers profile)))
168 (define (profile-derivation store packages)
169 "Return a derivation that builds a profile (a user environment) with
170 all of PACKAGES, a list of name/version/output/path tuples."
173 (use-modules (ice-9 pretty-print)
176 (setvbuf (current-output-port) _IOLBF)
177 (setvbuf (current-error-port) _IOLBF)
179 (let ((output (assoc-ref %outputs "out"))
180 (inputs (map cdr %build-inputs)))
181 (format #t "building user environment `~a' with ~a packages...~
%"
182 output (length inputs))
183 (union-build output inputs)
184 (call-with-output-file (string-append output "/manifest
")
186 (pretty-print '(manifest (version 0)
187 (packages ,packages))
190 (build-expression->derivation store "user-environment
"
194 ((name version output path)
197 #:modules '((guix build union))))
199 (define (profile-number profile)
200 "Return PROFILE
's number or 0. An absolute file name must be used."
201 (or (and=> (false-if-exception (regexp-exec (profile-regexp profile)
202 (basename (readlink profile))))
203 (compose string->number (cut match:substring <> 1)))
206 (define (roll-back profile)
207 "Roll back to the previous generation of PROFILE."
208 ;; XXX: Get the previous generation number from the manifest?
209 (let* ((number (profile-number profile))
210 (previous-number (previous-profile-number profile number))
211 (previous-profile (format #f "~a/~a-~a-link"
212 (dirname profile) profile
214 (manifest (string-append previous-profile "/manifest")))
216 (define (switch-link)
217 ;; Atomically switch PROFILE to the previous profile.
218 (let ((pivot (string-append previous-profile ".new")))
219 (format #t (_ "switching from generation ~a to ~a~%")
220 number previous-number)
221 (symlink previous-profile pivot)
222 (rename-file pivot profile)))
224 (cond ((zero? number)
225 (format (current-error-port)
226 (_ "error: `~a' is not a valid profile~
%")
228 ((or (zero? previous-number)
229 (not (file-exists? previous-profile)))
230 (leave (_ "error
: no previous profile
; not rolling back~
%")))
231 (else (switch-link)))))
235 ;;; Command-line options.
238 (define %default-options
239 ;; Alist of default option values.
240 `((profile . ,%current-profile)))
243 (display (_ "Usage
: guix-package
[OPTION
]... PACKAGES...
244 Install
, remove
, or upgrade PACKAGES
in a single transaction.
\n"))
246 -i, --install=PACKAGE
install PACKAGE
"))
248 -r, --remove=PACKAGE remove PACKAGE
"))
250 -u, --upgrade=REGEXP upgrade all the installed packages matching REGEXP
"))
252 --roll-back roll back to the previous generation
"))
255 -p, --profile=PROFILE use PROFILE instead of the user
's default profile"))
257 -n, --dry-run show what would be done without actually doing it"))
259 --bootstrap use the bootstrap Guile to build the profile"))
261 --verbose produce verbose output"))
264 -I, --list-installed[=REGEXP]
265 list installed packages matching REGEXP"))
267 -A, --list-available[=REGEXP]
268 list available packages matching REGEXP"))
271 -h, --help display this help and exit"))
273 -V, --version display version information and exit"))
275 (show-bug-report-information))
278 ;; Specification of the command-line options.
279 (list (option '(#\h "help") #f #f
283 (option
'(#\V "version") #f #f
285 (show-version-and-exit "guix-package")))
287 (option '(#\i "install") #t #f
288 (lambda
(opt name arg result
)
289 (alist-cons
'install arg result)))
290 (option '(#\r "remove") #t #f
291 (lambda
(opt name arg result
)
292 (alist-cons
'remove arg result)))
293 (option '("roll-back") #f #f
294 (lambda
(opt name arg result
)
295 (alist-cons
'roll-back? #t result)))
296 (option '(#\p "profile") #t #f
297 (lambda
(opt name arg result
)
298 (alist-cons
'profile arg
299 (alist-delete 'profile result
))))
300 (option
'(#\n "dry-run") #f #f
301 (lambda (opt name arg result)
302 (alist-cons 'dry-run?
#t result)))
303 (option
'("bootstrap") #f #f
304 (lambda (opt name arg result)
305 (alist-cons 'bootstrap?
#t result)))
306 (option
'("verbose") #f #f
307 (lambda (opt name arg result)
308 (alist-cons 'verbose?
#t result)))
309 (option
'(#\I "list-installed") #f #t
310 (lambda (opt name arg result)
311 (cons `(query list-installed ,(or arg ""))
313 (option '(#\A "list-available") #f #t
314 (lambda
(opt name arg result
)
315 (cons
`(query list-available ,(or arg ""))
323 (define (guix-package . args)
324 (define (parse-options)
325 ;; Return the alist of option values.
326 (args-fold args %options
327 (lambda (opt name arg result)
328 (leave (_ "~A: unrecognized option~%") name))
330 (alist-cons 'argument arg result))
333 (define (guile-missing?)
334 ;; Return #t if %GUILE-FOR-BUILD is not available yet.
335 (let ((out (derivation-path->output-path (%guile-for-build))))
336 (not (valid-path? (%store) out))))
338 (define (show-what-to-build drv dry-run?)
339 ;; Show what will/would be built in realizing the derivations listed
341 (let* ((req (append-map (lambda (drv-path)
342 (let ((d (call-with-input-file drv-path
344 (derivation-prerequisites-to-build
347 (req* (delete-duplicates
348 (append (remove (compose (cute valid-path? (%store) <>)
349 derivation-path->output-path)
351 (map derivation-input-path req)))))
353 (format (current-error-port)
354 (N_ "~:[the following derivation would be built:~%~{ ~a~%~}~;~]"
355 "~:[the following derivations would be built:~%~{ ~a~%~}~;~]"
358 (format (current-error-port)
359 (N_ "~:[the following derivation will be built:~%~{ ~a~%~}~;~]"
360 "~:[the following derivations will be built:~%~{ ~a~%~}~;~]"
362 (null? req*) req*))))
364 (define (find-package name)
365 ;; Find the package NAME; NAME may contain a version number and a
366 ;; sub-derivation name.
367 (define request name)
369 (let*-values (((name sub-drv)
370 (match (string-rindex name #\:)
371 (#f (values name "out"))
372 (colon (values (substring name 0 colon)
373 (substring name (+ 1 colon))))))
375 (package-name->name+version name)))
376 (match (find-packages-by-name name version)
378 (list name (package-version p) sub-drv p))
380 (format (current-error-port)
381 (_ "warning: ambiguous package specification `~a
'~%")
383 (format (current-error-port)
384 (_ "warning: choosing ~a from ~a~%")
385 (package-full-name p)
386 (location->string (package-location p)))
387 (list name (package-version p) sub-drv p))
389 (leave (_ "~a: package not found~%") request)))))
391 (define (ensure-default-profile)
392 ;; Ensure the default profile symlink and directory exist.
394 ;; Create ~/.guix-profile if it doesn't exist yet.
395 (when
(and
%user-environment-directory
397 (not
(false-if-exception
398 (lstat
%user-environment-directory
))))
399 (symlink
%current-profile
%user-environment-directory
))
401 ;; Attempt to create
/…
/profiles
/per-user
/$USER if needed.
402 (unless
(directory-exists?
%profile-directory
)
405 (mkdir-p %profile-directory))
407 ;; Often, we cannot create %PROFILE-DIRECTORY because its
408 ;; parent directory is root-owned and we're running
410 (format
(current-error-port
)
411 (_
"error: while creating directory `~a': ~a~%")
413 (strerror
(system-error-errno args
)))
414 (format
(current-error-port
)
415 (_
"Please create the `~a' directory, with you as the owner.~%")
419 (define
(process-actions opts
)
420 ;; Process any install
/remove
/upgrade action from OPTS.
422 (define dry-run?
(assoc-ref opts
'dry-run?))
423 (define verbose? (assoc-ref opts 'verbose?
))
424 (define profile
(assoc-ref opts
'profile))
426 ;; First roll back if asked to.
427 (if (and (assoc-ref opts 'roll-back?
) (not dry-run?
))
430 (process-actions
(alist-delete
'roll-back? opts)))
431 (let* ((install (filter-map (match-lambda
432 (('install .
(? store-path?
))
434 (('install . package)
435 (find-package package))
438 (drv (filter-map (match-lambda
439 ((name version sub-drv
440 (? package? package))
441 (package-derivation (%store) package))
445 (filter-map (match-lambda
446 (('install .
(? store-path? path
))
447 (let-values
(((name version
)
448 (package-name-
>name
+version
449 (store-path-package-name
451 `(,name ,version #f ,path)))
454 (map (lambda (tuple drv)
456 ((name version sub-drv _)
458 (derivation-path->output-path
460 `(,name
,version
,sub-drv
,output-path
)))))
462 (remove
(filter-map
(match-lambda
467 (packages (append install*
468 (fold (lambda (package result)
471 (alist-delete name result))))
474 (profile-manifest profile))
478 (when (equal? profile %current-profile)
479 (ensure-default-profile))
481 (show-what-to-build drv dry-run?)
484 (and (build-derivations (%store) drv)
485 (let* ((prof-drv (profile-derivation (%store) packages))
486 (prof (derivation-path->output-path prof-drv))
487 (old-drv (profile-derivation
488 (%store) (manifest-packages
489 (profile-manifest profile))))
490 (old-prof (derivation-path->output-path old-drv))
491 (number (latest-profile-number profile))
492 (name (format #f "~a/~a-~a-link"
494 (basename profile) (+ 1 number))))
495 (if (string=? old-prof prof)
496 (when (or (pair? install) (pair? remove))
497 (format (current-error-port)
498 (_ "nothing to be done~%")))
499 (and (parameterize ((current-build-output-port
500 ;; Output something when Guile
501 ;; needs to be built.
502 (if (or verbose? (guile-missing?))
504 (%make-void-port "w"))))
505 (build-derivations (%store) (list prof-drv)))
508 (when (file-exists? profile)
509 (delete-file profile))
510 (symlink name profile))))))))))
512 (define (process-query opts)
513 ;; Process any query specified by OPTS. Return #t when a query was
514 ;; actually processed, #f otherwise.
515 (let ((profile (assoc-ref opts 'profile
)))
516 (match
(assoc-ref opts
'query)
517 (('list-installed regexp
)
518 (let* ((regexp
(and regexp
(make-regexp regexp
)))
519 (manifest
(profile-manifest profile
))
520 (installed
(manifest-packages manifest
)))
521 (for-each
(match-lambda
522 ((name version output path
)
523 (when
(or
(not regexp
)
524 (regexp-exec regexp name
))
525 (format
#t "~a\t~a\t~a\t~a~%"
526 name
(or version
"?") output path
))))
529 (('list-available regexp)
530 (let* ((regexp (and regexp (make-regexp regexp)))
531 (available (fold-packages
533 (let ((n (package-name p)))
535 (if (regexp-exec regexp n)
540 (for-each
(lambda
(p
)
541 (format
#t "~a\t~a\t~a\t~a~%"
544 (string-join
(package-outputs p
) ",")
545 (location-
>string
(package-location p
))))
548 (string
<?
(package-name p1
)
549 (package-name p2
)))))
553 (setlocale LC_ALL
"")
555 (setvbuf
(current-output-port
) _IOLBF
)
556 (setvbuf
(current-error-port
) _IOLBF
)
558 (let ((opts
(parse-options
)))
559 (parameterize
((%store
(open-connection
)))
561 (or
(process-query opts
)
562 (parameterize
((%guile-for-build
563 (package-derivation
(%store
)
564 (if (assoc-ref opts
'bootstrap?)
567 (process-actions opts)))))))