You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

392 lines
15 KiB

  1. #!/bin/sh
  2. # aside from this initial boilerplate, this is actually -*- scheme -*- code
  3. prefix="@prefix@"
  4. datarootdir="@datarootdir@"
  5. GUILE_LOAD_COMPILED_PATH="@guilemoduledir@:$GUILE_LOAD_COMPILED_PATH"
  6. export GUILE_LOAD_COMPILED_PATH
  7. main='(module-ref (resolve-interface '\''(guix-package)) '\'guix-package')'
  8. exec ${GUILE-@GUILE@} -L "@guilemoduledir@" -l "$0" \
  9. -c "(apply $main (cdr (command-line)))" "$@"
  10. !#
  11. ;;; Guix --- Nix package management from Guile. -*- coding: utf-8 -*-
  12. ;;; Copyright (C) 2012 Ludovic Courtès <ludo@gnu.org>
  13. ;;;
  14. ;;; This file is part of Guix.
  15. ;;;
  16. ;;; Guix is free software; you can redistribute it and/or modify it
  17. ;;; under the terms of the GNU General Public License as published by
  18. ;;; the Free Software Foundation; either version 3 of the License, or (at
  19. ;;; your option) any later version.
  20. ;;;
  21. ;;; Guix is distributed in the hope that it will be useful, but
  22. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  23. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. ;;; GNU General Public License for more details.
  25. ;;;
  26. ;;; You should have received a copy of the GNU General Public License
  27. ;;; along with Guix. If not, see <http://www.gnu.org/licenses/>.
  28. (define-module (guix-package)
  29. #:use-module (guix store)
  30. #:use-module (guix derivations)
  31. #:use-module (guix packages)
  32. #:use-module (guix utils)
  33. #:use-module (ice-9 ftw)
  34. #:use-module (ice-9 format)
  35. #:use-module (ice-9 match)
  36. #:use-module (ice-9 regex)
  37. #:use-module (srfi srfi-1)
  38. #:use-module (srfi srfi-11)
  39. #:use-module (srfi srfi-26)
  40. #:use-module (srfi srfi-34)
  41. #:use-module (srfi srfi-37)
  42. #:autoload (distro) (find-packages-by-name)
  43. #:use-module (distro packages base)
  44. #:export (guix-package))
  45. (define _ (cut gettext <> "guix"))
  46. (define N_ (cut ngettext <> <> <> "guix"))
  47. (define %store
  48. (open-connection))
  49. ;;;
  50. ;;; User environment.
  51. ;;;
  52. (define %user-environment-directory
  53. (and=> (getenv "HOME")
  54. (cut string-append <> "/.guix-profile")))
  55. (define %profile-directory
  56. (string-append "/nix/var/nix/profiles/"
  57. "guix/"
  58. (or (and=> (getenv "USER")
  59. (cut string-append "per-user/" <>))
  60. "default")))
  61. (define %current-profile
  62. (string-append %profile-directory "/profile"))
  63. (define (profile-manifest profile)
  64. "Return the PROFILE's manifest."
  65. (let ((manifest (string-append profile "/manifest")))
  66. (if (file-exists? manifest)
  67. (call-with-input-file manifest read)
  68. '(manifest (version 0) (packages ())))))
  69. (define (manifest-packages manifest)
  70. "Return the packages listed in MANIFEST."
  71. (match manifest
  72. (('manifest ('version 0) ('packages packages))
  73. packages)
  74. (_
  75. (error "unsupported manifest format" manifest))))
  76. (define (latest-profile-number profile)
  77. "Return the identifying number of the latest generation of PROFILE.
  78. PROFILE is the name of the symlink to the current generation."
  79. (define %profile-rx
  80. (make-regexp (string-append "^" (regexp-quote (basename profile))
  81. "-([0-9]+)")))
  82. (define* (scandir name #:optional (select? (const #t))
  83. (entry<? (@ (ice-9 i18n) string-locale<?)))
  84. ;; XXX: Bug-fix version introduced in Guile v2.0.6-62-g139ce19.
  85. (define (enter? dir stat result)
  86. (and stat (string=? dir name)))
  87. (define (visit basename result)
  88. (if (select? basename)
  89. (cons basename result)
  90. result))
  91. (define (leaf name stat result)
  92. (and result
  93. (visit (basename name) result)))
  94. (define (down name stat result)
  95. (visit "." '()))
  96. (define (up name stat result)
  97. (visit ".." result))
  98. (define (skip name stat result)
  99. ;; All the sub-directories are skipped.
  100. (visit (basename name) result))
  101. (define (error name* stat errno result)
  102. (if (string=? name name*) ; top-level NAME is unreadable
  103. result
  104. (visit (basename name*) result)))
  105. (and=> (file-system-fold enter? leaf down up skip error #f name lstat)
  106. (lambda (files)
  107. (sort files entry<?))))
  108. (match (scandir (dirname profile)
  109. (cut regexp-exec %profile-rx <>))
  110. (#f ; no profile directory
  111. 0)
  112. (() ; no profiles
  113. 0)
  114. ((profiles ...) ; former profiles around
  115. (let ((numbers (map (compose string->number
  116. (cut match:substring <> 1)
  117. (cut regexp-exec %profile-rx <>))
  118. profiles)))
  119. (fold (lambda (number highest)
  120. (if (> number highest)
  121. number
  122. highest))
  123. 0
  124. numbers)))))
  125. (define (profile-derivation store packages)
  126. "Return a derivation that builds a profile (a user environment) with
  127. all of PACKAGES, a list of name/version/output/path tuples."
  128. (define builder
  129. `(begin
  130. (use-modules (ice-9 pretty-print)
  131. (guix build union))
  132. (setvbuf (current-output-port) _IOLBF)
  133. (setvbuf (current-error-port) _IOLBF)
  134. (let ((output (assoc-ref %outputs "out"))
  135. (inputs (map cdr %build-inputs)))
  136. (format #t "building user environment `~a' with ~a packages...~%"
  137. output (length inputs))
  138. (union-build output inputs)
  139. (call-with-output-file (string-append output "/manifest")
  140. (lambda (p)
  141. (pretty-print '(manifest (version 0)
  142. (packages ,packages))
  143. p))))))
  144. (build-expression->derivation store "user-environment"
  145. (%current-system)
  146. builder
  147. (map (match-lambda
  148. ((name version output path)
  149. `(,name ,path)))
  150. packages)
  151. #:modules '((guix build union))))
  152. ;;;
  153. ;;; Command-line options.
  154. ;;;
  155. (define %default-options
  156. ;; Alist of default option values.
  157. `((profile . ,%current-profile)))
  158. (define-syntax-rule (leave fmt args ...)
  159. "Format FMT and ARGS to the error port and exit."
  160. (begin
  161. (format (current-error-port) fmt args ...)
  162. (exit 1)))
  163. (define (show-version)
  164. (display "guix-package (@PACKAGE_NAME@) @PACKAGE_VERSION@\n"))
  165. (define (show-help)
  166. (display (_ "Usage: guix-package [OPTION]... PACKAGES...
  167. Install, remove, or upgrade PACKAGES in a single transaction.\n"))
  168. (display (_ "
  169. -i, --install=PACKAGE install PACKAGE"))
  170. (display (_ "
  171. -r, --remove=PACKAGE remove PACKAGE"))
  172. (display (_ "
  173. -u, --upgrade=REGEXP upgrade all the installed packages matching REGEXP"))
  174. (newline)
  175. (display (_ "
  176. -p, --profile=PROFILE use PROFILE instead of the user's default profile"))
  177. (display (_ "
  178. -n, --dry-run show what would be done without actually doing it"))
  179. (display (_ "
  180. -b, --bootstrap use the bootstrap Guile to build the profile"))
  181. (newline)
  182. (display (_ "
  183. -h, --help display this help and exit"))
  184. (display (_ "
  185. -V, --version display version information and exit"))
  186. (newline)
  187. (format #t (_ "
  188. Report bugs to: ~a.~%") "@PACKAGE_BUGREPORT@"))
  189. (define %options
  190. ;; Specification of the command-line options.
  191. (list (option '(#\h "help") #f #f
  192. (lambda args
  193. (show-help)
  194. (exit 0)))
  195. (option '(#\V "version") #f #f
  196. (lambda args
  197. (show-version)
  198. (exit 0)))
  199. (option '(#\i "install") #t #f
  200. (lambda (opt name arg result)
  201. (alist-cons 'install arg result)))
  202. (option '(#\r "remove") #t #f
  203. (lambda (opt name arg result)
  204. (alist-cons 'remove arg result)))
  205. (option '(#\p "profile") #t #f
  206. (lambda (opt name arg result)
  207. (alist-cons 'profile arg
  208. (alist-delete 'profile result))))
  209. (option '(#\n "dry-run") #f #f
  210. (lambda (opt name arg result)
  211. (alist-cons 'dry-run? #t result)))
  212. (option '(#\b "bootstrap") #f #f
  213. (lambda (opt name arg result)
  214. (alist-cons 'bootstrap? #t result)))))
  215. ;;;
  216. ;;; Entry point.
  217. ;;;
  218. (define (guix-package . args)
  219. (define (parse-options)
  220. ;; Return the alist of option values.
  221. (args-fold args %options
  222. (lambda (opt name arg result)
  223. (leave (_ "~A: unrecognized option~%") name))
  224. (lambda (arg result)
  225. (alist-cons 'argument arg result))
  226. %default-options))
  227. (define (show-what-to-build drv dry-run?)
  228. ;; Show what will/would be built in realizing the derivations listed
  229. ;; in DRV.
  230. (let* ((req (append-map (lambda (drv-path)
  231. (let ((d (call-with-input-file drv-path
  232. read-derivation)))
  233. (derivation-prerequisites-to-build %store d)))
  234. drv))
  235. (req* (delete-duplicates
  236. (append (remove (compose (cut valid-path? %store <>)
  237. derivation-path->output-path)
  238. drv)
  239. (map derivation-input-path req)))))
  240. (if dry-run?
  241. (format (current-error-port)
  242. (N_ "~:[the following derivation would be built:~%~{ ~a~%~}~;~]"
  243. "~:[the following derivations would be built:~%~{ ~a~%~}~;~]"
  244. (length req*))
  245. (null? req*) req*)
  246. (format (current-error-port)
  247. (N_ "~:[the following derivation will be built:~%~{ ~a~%~}~;~]"
  248. "~:[the following derivations will be built:~%~{ ~a~%~}~;~]"
  249. (length req*))
  250. (null? req*) req*))))
  251. (define (find-package name)
  252. ;; Find the package NAME; NAME may contain a version number and a
  253. ;; sub-derivation name.
  254. (define request name)
  255. (define versioned-rx
  256. (make-regexp "^(.*)-([0-9][^-]*)$"))
  257. (let*-values (((name sub-drv)
  258. (match (string-rindex name #\:)
  259. (#f (values name "out"))
  260. (colon (values (substring name (+ 1 colon))
  261. (substring name colon)))))
  262. ((name version)
  263. (match (regexp-exec versioned-rx name)
  264. (#f (values name #f))
  265. (m (values (match:substring m 1)
  266. (match:substring m 2))))))
  267. (match (find-packages-by-name name version)
  268. ((p)
  269. (list name version sub-drv p))
  270. ((p _ ...)
  271. (format (current-error-port)
  272. (_ "warning: ambiguous package specification `~a'~%")
  273. request)
  274. (format (current-error-port)
  275. (_ "warning: choosing ~s~%")
  276. p)
  277. (list name version sub-drv p))
  278. (()
  279. (leave (_ "~a: package not found~%") request)))))
  280. (setlocale LC_ALL "")
  281. (textdomain "guix")
  282. (setvbuf (current-output-port) _IOLBF)
  283. (setvbuf (current-error-port) _IOLBF)
  284. (let ((opts (parse-options)))
  285. (parameterize ((%guile-for-build
  286. (package-derivation %store
  287. (if (assoc-ref opts 'bootstrap?)
  288. (@@ (distro packages base)
  289. %bootstrap-guile)
  290. guile-2.0))))
  291. (let* ((dry-run? (assoc-ref opts 'dry-run?))
  292. (profile (assoc-ref opts 'profile))
  293. (install (filter-map (match-lambda
  294. (('install . (? store-path?))
  295. #f)
  296. (('install . package)
  297. (find-package package))
  298. (_ #f))
  299. opts))
  300. (drv (filter-map (match-lambda
  301. ((name version sub-drv (? package? package))
  302. (package-derivation %store package))
  303. (_ #f))
  304. install))
  305. (install* (append
  306. (filter-map (match-lambda
  307. (('install . (? store-path? path))
  308. `(,(store-path-package-name path)
  309. #f #f ,path))
  310. (_ #f))
  311. opts)
  312. (map (lambda (tuple drv)
  313. (match tuple
  314. ((name version sub-drv _)
  315. (let ((output-path
  316. (derivation-path->output-path drv
  317. sub-drv)))
  318. `(,name ,version ,sub-drv ,output-path)))))
  319. install drv)))
  320. (remove (filter-map (match-lambda
  321. (('remove . package)
  322. package)
  323. (_ #f))
  324. opts))
  325. (packages (append install*
  326. (fold alist-delete
  327. (manifest-packages (profile-manifest profile))
  328. remove))))
  329. (show-what-to-build drv dry-run?)
  330. (or dry-run?
  331. (and (build-derivations %store drv)
  332. (let* ((prof-drv (profile-derivation %store packages))
  333. (prof (derivation-path->output-path prof-drv))
  334. (number (latest-profile-number profile))
  335. (name (format #f "~a/~a-~a-link"
  336. (dirname profile)
  337. (basename profile) (+ 1 number))))
  338. (and (build-derivations %store (list prof-drv))
  339. (begin
  340. (symlink prof name)
  341. (when (file-exists? profile)
  342. (delete-file profile))
  343. (symlink name profile))))))))))
  344. ;; Local Variables:
  345. ;; eval: (put 'guard 'scheme-indent-function 1)
  346. ;; End: