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.
 
 
 
 
 
 

593 lines
21 KiB

  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2013, 2014 Ludovic Courtès <ludo@gnu.org>
  3. ;;; Copyright © 2013 Nikita Karetnikov <nikita@karetnikov.org>
  4. ;;; Copyright © 2014 Alex Kost <alezost@gmail.com>
  5. ;;;
  6. ;;; This file is part of GNU Guix.
  7. ;;;
  8. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  9. ;;; under the terms of the GNU General Public License as published by
  10. ;;; the Free Software Foundation; either version 3 of the License, or (at
  11. ;;; your option) any later version.
  12. ;;;
  13. ;;; GNU Guix is distributed in the hope that it will be useful, but
  14. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  15. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. ;;; GNU General Public License for more details.
  17. ;;;
  18. ;;; You should have received a copy of the GNU General Public License
  19. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  20. (define-module (guix profiles)
  21. #:use-module (guix ui)
  22. #:use-module (guix utils)
  23. #:use-module (guix records)
  24. #:use-module (guix derivations)
  25. #:use-module (guix packages)
  26. #:use-module (guix gexp)
  27. #:use-module (guix monads)
  28. #:use-module (ice-9 match)
  29. #:use-module (ice-9 regex)
  30. #:use-module (ice-9 ftw)
  31. #:use-module (ice-9 format)
  32. #:use-module (srfi srfi-1)
  33. #:use-module (srfi srfi-9)
  34. #:use-module (srfi srfi-11)
  35. #:use-module (srfi srfi-19)
  36. #:use-module (srfi srfi-26)
  37. #:export (manifest make-manifest
  38. manifest?
  39. manifest-entries
  40. <manifest-entry> ; FIXME: eventually make it internal
  41. manifest-entry
  42. manifest-entry?
  43. manifest-entry-name
  44. manifest-entry-version
  45. manifest-entry-output
  46. manifest-entry-item
  47. manifest-entry-dependencies
  48. manifest-pattern
  49. manifest-pattern?
  50. manifest-remove
  51. manifest-add
  52. manifest-lookup
  53. manifest-installed?
  54. manifest-matching-entries
  55. manifest-transaction
  56. manifest-transaction?
  57. manifest-transaction-install
  58. manifest-transaction-remove
  59. manifest-perform-transaction
  60. manifest-transaction-effects
  61. manifest-show-transaction
  62. profile-manifest
  63. package->manifest-entry
  64. profile-derivation
  65. generation-number
  66. generation-numbers
  67. profile-generations
  68. previous-generation-number
  69. generation-time
  70. generation-file-name))
  71. ;;; Commentary:
  72. ;;;
  73. ;;; Tools to create and manipulate profiles---i.e., the representation of a
  74. ;;; set of installed packages.
  75. ;;;
  76. ;;; Code:
  77. ;;;
  78. ;;; Manifests.
  79. ;;;
  80. (define-record-type <manifest>
  81. (manifest entries)
  82. manifest?
  83. (entries manifest-entries)) ; list of <manifest-entry>
  84. ;; Convenient alias, to avoid name clashes.
  85. (define make-manifest manifest)
  86. (define-record-type* <manifest-entry> manifest-entry
  87. make-manifest-entry
  88. manifest-entry?
  89. (name manifest-entry-name) ; string
  90. (version manifest-entry-version) ; string
  91. (output manifest-entry-output ; string
  92. (default "out"))
  93. (item manifest-entry-item) ; package | store path
  94. (dependencies manifest-entry-dependencies ; (store path | package)*
  95. (default '())))
  96. (define-record-type* <manifest-pattern> manifest-pattern
  97. make-manifest-pattern
  98. manifest-pattern?
  99. (name manifest-pattern-name) ; string
  100. (version manifest-pattern-version ; string | #f
  101. (default #f))
  102. (output manifest-pattern-output ; string | #f
  103. (default "out")))
  104. (define (profile-manifest profile)
  105. "Return the PROFILE's manifest."
  106. (let ((file (string-append profile "/manifest")))
  107. (if (file-exists? file)
  108. (call-with-input-file file read-manifest)
  109. (manifest '()))))
  110. (define* (package->manifest-entry package #:optional output)
  111. "Return a manifest entry for the OUTPUT of package PACKAGE. When OUTPUT is
  112. omitted or #f, use the first output of PACKAGE."
  113. (let ((deps (map (match-lambda
  114. ((label package)
  115. `(,package "out"))
  116. ((label package output)
  117. `(,package ,output)))
  118. (package-transitive-propagated-inputs package))))
  119. (manifest-entry
  120. (name (package-name package))
  121. (version (package-version package))
  122. (output (or output (car (package-outputs package))))
  123. (item package)
  124. (dependencies (delete-duplicates deps)))))
  125. (define (manifest->gexp manifest)
  126. "Return a representation of MANIFEST as a gexp."
  127. (define (entry->gexp entry)
  128. (match entry
  129. (($ <manifest-entry> name version output (? string? path) (deps ...))
  130. #~(#$name #$version #$output #$path #$deps))
  131. (($ <manifest-entry> name version output (? package? package) (deps ...))
  132. #~(#$name #$version #$output
  133. (ungexp package (or output "out")) #$deps))))
  134. (match manifest
  135. (($ <manifest> (entries ...))
  136. #~(manifest (version 1)
  137. (packages #$(map entry->gexp entries))))))
  138. (define (sexp->manifest sexp)
  139. "Parse SEXP as a manifest."
  140. (match sexp
  141. (('manifest ('version 0)
  142. ('packages ((name version output path) ...)))
  143. (manifest
  144. (map (lambda (name version output path)
  145. (manifest-entry
  146. (name name)
  147. (version version)
  148. (output output)
  149. (item path)))
  150. name version output path)))
  151. ;; Version 1 adds a list of propagated inputs to the
  152. ;; name/version/output/path tuples.
  153. (('manifest ('version 1)
  154. ('packages ((name version output path deps) ...)))
  155. (manifest
  156. (map (lambda (name version output path deps)
  157. ;; Up to Guix 0.7 included, dependencies were listed as ("gmp"
  158. ;; "/gnu/store/...-gmp") for instance. Discard the 'label' in
  159. ;; such lists.
  160. (let ((deps (match deps
  161. (((labels directories) ...)
  162. directories)
  163. ((directories ...)
  164. directories))))
  165. (manifest-entry
  166. (name name)
  167. (version version)
  168. (output output)
  169. (item path)
  170. (dependencies deps))))
  171. name version output path deps)))
  172. (_
  173. (error "unsupported manifest format" manifest))))
  174. (define (read-manifest port)
  175. "Return the packages listed in MANIFEST."
  176. (sexp->manifest (read port)))
  177. (define (entry-predicate pattern)
  178. "Return a procedure that returns #t when passed a manifest entry that
  179. matches NAME/OUTPUT/VERSION. OUTPUT and VERSION may be #f, in which case they
  180. are ignored."
  181. (match pattern
  182. (($ <manifest-pattern> name version output)
  183. (match-lambda
  184. (($ <manifest-entry> entry-name entry-version entry-output)
  185. (and (string=? entry-name name)
  186. (or (not entry-output) (not output)
  187. (string=? entry-output output))
  188. (or (not version)
  189. (string=? entry-version version))))))))
  190. (define (manifest-remove manifest patterns)
  191. "Remove entries for each of PATTERNS from MANIFEST. Each item in PATTERNS
  192. must be a manifest-pattern."
  193. (define (remove-entry pattern lst)
  194. (remove (entry-predicate pattern) lst))
  195. (make-manifest (fold remove-entry
  196. (manifest-entries manifest)
  197. patterns)))
  198. (define (manifest-add manifest entries)
  199. "Add a list of manifest ENTRIES to MANIFEST and return new manifest.
  200. Remove MANIFEST entries that have the same name and output as ENTRIES."
  201. (define (same-entry? entry name output)
  202. (match entry
  203. (($ <manifest-entry> entry-name _ entry-output _ ...)
  204. (and (equal? name entry-name)
  205. (equal? output entry-output)))))
  206. (make-manifest
  207. (append entries
  208. (fold (lambda (entry result)
  209. (match entry
  210. (($ <manifest-entry> name _ out _ ...)
  211. (filter (negate (cut same-entry? <> name out))
  212. result))))
  213. (manifest-entries manifest)
  214. entries))))
  215. (define (manifest-lookup manifest pattern)
  216. "Return the first item of MANIFEST that matches PATTERN, or #f if there is
  217. no match.."
  218. (find (entry-predicate pattern)
  219. (manifest-entries manifest)))
  220. (define (manifest-installed? manifest pattern)
  221. "Return #t if MANIFEST has an entry matching PATTERN (a manifest-pattern),
  222. #f otherwise."
  223. (->bool (manifest-lookup manifest pattern)))
  224. (define (manifest-matching-entries manifest patterns)
  225. "Return all the entries of MANIFEST that match one of the PATTERNS."
  226. (define predicates
  227. (map entry-predicate patterns))
  228. (define (matches? entry)
  229. (any (lambda (pred)
  230. (pred entry))
  231. predicates))
  232. (filter matches? (manifest-entries manifest)))
  233. ;;;
  234. ;;; Manifest transactions.
  235. ;;;
  236. (define-record-type* <manifest-transaction> manifest-transaction
  237. make-manifest-transaction
  238. manifest-transaction?
  239. (install manifest-transaction-install ; list of <manifest-entry>
  240. (default '()))
  241. (remove manifest-transaction-remove ; list of <manifest-pattern>
  242. (default '())))
  243. (define (manifest-transaction-effects manifest transaction)
  244. "Compute the effect of applying TRANSACTION to MANIFEST. Return 3 values:
  245. the list of packages that would be removed, installed, or upgraded when
  246. applying TRANSACTION to MANIFEST. Upgrades are represented as pairs where the
  247. head is the entry being upgraded and the tail is the entry that will replace
  248. it."
  249. (define (manifest-entry->pattern entry)
  250. (manifest-pattern
  251. (name (manifest-entry-name entry))
  252. (output (manifest-entry-output entry))))
  253. (let loop ((input (manifest-transaction-install transaction))
  254. (install '())
  255. (upgrade '()))
  256. (match input
  257. (()
  258. (let ((remove (manifest-transaction-remove transaction)))
  259. (values (manifest-matching-entries manifest remove)
  260. (reverse install) (reverse upgrade))))
  261. ((entry rest ...)
  262. ;; Check whether installing ENTRY corresponds to the installation of a
  263. ;; new package or to an upgrade.
  264. ;; XXX: When the exact same output directory is installed, we're not
  265. ;; really upgrading anything. Add a check for that case.
  266. (let* ((pattern (manifest-entry->pattern entry))
  267. (previous (manifest-lookup manifest pattern)))
  268. (loop rest
  269. (if previous install (cons entry install))
  270. (if previous
  271. (alist-cons previous entry upgrade)
  272. upgrade)))))))
  273. (define (manifest-perform-transaction manifest transaction)
  274. "Perform TRANSACTION on MANIFEST and return new manifest."
  275. (let ((install (manifest-transaction-install transaction))
  276. (remove (manifest-transaction-remove transaction)))
  277. (manifest-add (manifest-remove manifest remove)
  278. install)))
  279. (define (right-arrow port)
  280. "Return either a string containing the 'RIGHT ARROW' character, or an ASCII
  281. replacement if PORT is not Unicode-capable."
  282. (with-fluids ((%default-port-encoding (port-encoding port)))
  283. (let ((arrow "→"))
  284. (catch 'encoding-error
  285. (lambda ()
  286. (call-with-output-string
  287. (lambda (port)
  288. (set-port-conversion-strategy! port 'error)
  289. (display arrow port))))
  290. (lambda (key . args)
  291. "->")))))
  292. (define* (manifest-show-transaction store manifest transaction
  293. #:key dry-run?)
  294. "Display what will/would be installed/removed from MANIFEST by TRANSACTION."
  295. (define (package-strings name version output item)
  296. (map (lambda (name version output item)
  297. (format #f " ~a~:[:~a~;~*~]\t~a\t~a"
  298. name
  299. (equal? output "out") output version
  300. (if (package? item)
  301. (package-output store item output)
  302. item)))
  303. name version output item))
  304. (define → ;an arrow that can be represented on stderr
  305. (right-arrow (current-error-port)))
  306. (define (upgrade-string name old-version new-version output item)
  307. (format #f " ~a~:[:~a~;~*~]\t~a ~a ~a\t~a"
  308. name (equal? output "out") output
  309. old-version → new-version
  310. (if (package? item)
  311. (package-output store item output)
  312. item)))
  313. (let-values (((remove install upgrade)
  314. (manifest-transaction-effects manifest transaction)))
  315. (match remove
  316. ((($ <manifest-entry> name version output item) ..1)
  317. (let ((len (length name))
  318. (remove (package-strings name version output item)))
  319. (if dry-run?
  320. (format (current-error-port)
  321. (N_ "The following package would be removed:~%~{~a~%~}~%"
  322. "The following packages would be removed:~%~{~a~%~}~%"
  323. len)
  324. remove)
  325. (format (current-error-port)
  326. (N_ "The following package will be removed:~%~{~a~%~}~%"
  327. "The following packages will be removed:~%~{~a~%~}~%"
  328. len)
  329. remove))))
  330. (_ #f))
  331. (match upgrade
  332. (((($ <manifest-entry> name old-version)
  333. . ($ <manifest-entry> _ new-version output item)) ..1)
  334. (let ((len (length name))
  335. (upgrade (map upgrade-string
  336. name old-version new-version output item)))
  337. (if dry-run?
  338. (format (current-error-port)
  339. (N_ "The following package would be upgraded:~%~{~a~%~}~%"
  340. "The following packages would be upgraded:~%~{~a~%~}~%"
  341. len)
  342. upgrade)
  343. (format (current-error-port)
  344. (N_ "The following package will be upgraded:~%~{~a~%~}~%"
  345. "The following packages will be upgraded:~%~{~a~%~}~%"
  346. len)
  347. upgrade))))
  348. (_ #f))
  349. (match install
  350. ((($ <manifest-entry> name version output item _) ..1)
  351. (let ((len (length name))
  352. (install (package-strings name version output item)))
  353. (if dry-run?
  354. (format (current-error-port)
  355. (N_ "The following package would be installed:~%~{~a~%~}~%"
  356. "The following packages would be installed:~%~{~a~%~}~%"
  357. len)
  358. install)
  359. (format (current-error-port)
  360. (N_ "The following package will be installed:~%~{~a~%~}~%"
  361. "The following packages will be installed:~%~{~a~%~}~%"
  362. len)
  363. install))))
  364. (_ #f))))
  365. ;;;
  366. ;;; Profiles.
  367. ;;;
  368. (define (manifest-inputs manifest)
  369. "Return the list of inputs for MANIFEST. Each input has one of the
  370. following forms:
  371. (PACKAGE OUTPUT-NAME)
  372. or
  373. STORE-PATH
  374. "
  375. (append-map (match-lambda
  376. (($ <manifest-entry> name version
  377. output (? package? package) deps)
  378. `((,package ,output) ,@deps))
  379. (($ <manifest-entry> name version output path deps)
  380. ;; Assume PATH and DEPS are already valid.
  381. `(,path ,@deps)))
  382. (manifest-entries manifest)))
  383. (define (info-dir-file manifest)
  384. "Return a derivation that builds the 'dir' file for all the entries of
  385. MANIFEST."
  386. (define texinfo ;lazy reference
  387. (module-ref (resolve-interface '(gnu packages texinfo)) 'texinfo))
  388. (define gzip ;lazy reference
  389. (module-ref (resolve-interface '(gnu packages compression)) 'gzip))
  390. (define build
  391. #~(begin
  392. (use-modules (guix build utils)
  393. (srfi srfi-1) (srfi srfi-26)
  394. (ice-9 ftw))
  395. (define (info-file? file)
  396. (or (string-suffix? ".info" file)
  397. (string-suffix? ".info.gz" file)))
  398. (define (info-files top)
  399. (let ((infodir (string-append top "/share/info")))
  400. (map (cut string-append infodir "/" <>)
  401. (or (scandir infodir info-file?) '()))))
  402. (define (install-info info)
  403. (setenv "PATH" (string-append #+gzip "/bin")) ;for info.gz files
  404. (zero?
  405. (system* (string-append #+texinfo "/bin/install-info")
  406. info (string-append #$output "/share/info/dir"))))
  407. (mkdir-p (string-append #$output "/share/info"))
  408. (every install-info
  409. (append-map info-files
  410. '#$(manifest-inputs manifest)))))
  411. ;; Don't depend on Texinfo when there's nothing to do.
  412. (if (null? (manifest-entries manifest))
  413. (gexp->derivation "info-dir" #~(mkdir #$output))
  414. (gexp->derivation "info-dir" build
  415. #:modules '((guix build utils)))))
  416. (define* (profile-derivation manifest #:key (info-dir? #t))
  417. "Return a derivation that builds a profile (aka. 'user environment') with
  418. the given MANIFEST. The profile includes a top-level Info 'dir' file, unless
  419. INFO-DIR? is #f."
  420. (mlet %store-monad ((info-dir (if info-dir?
  421. (info-dir-file manifest)
  422. (return #f))))
  423. (define inputs
  424. (if info-dir
  425. (cons info-dir (manifest-inputs manifest))
  426. (manifest-inputs manifest)))
  427. (define builder
  428. #~(begin
  429. (use-modules (ice-9 pretty-print)
  430. (guix build union))
  431. (setvbuf (current-output-port) _IOLBF)
  432. (setvbuf (current-error-port) _IOLBF)
  433. (union-build #$output '#$inputs
  434. #:log-port (%make-void-port "w"))
  435. (call-with-output-file (string-append #$output "/manifest")
  436. (lambda (p)
  437. (pretty-print '#$(manifest->gexp manifest) p)))))
  438. (gexp->derivation "profile" builder
  439. #:modules '((guix build union))
  440. #:local-build? #t)))
  441. (define (profile-regexp profile)
  442. "Return a regular expression that matches PROFILE's name and number."
  443. (make-regexp (string-append "^" (regexp-quote (basename profile))
  444. "-([0-9]+)")))
  445. (define (generation-number profile)
  446. "Return PROFILE's number or 0. An absolute file name must be used."
  447. (or (and=> (false-if-exception (regexp-exec (profile-regexp profile)
  448. (basename (readlink profile))))
  449. (compose string->number (cut match:substring <> 1)))
  450. 0))
  451. (define (generation-numbers profile)
  452. "Return the sorted list of generation numbers of PROFILE, or '(0) if no
  453. former profiles were found."
  454. (define* (scandir name #:optional (select? (const #t))
  455. (entry<? (@ (ice-9 i18n) string-locale<?)))
  456. ;; XXX: Bug-fix version introduced in Guile v2.0.6-62-g139ce19.
  457. (define (enter? dir stat result)
  458. (and stat (string=? dir name)))
  459. (define (visit basename result)
  460. (if (select? basename)
  461. (cons basename result)
  462. result))
  463. (define (leaf name stat result)
  464. (and result
  465. (visit (basename name) result)))
  466. (define (down name stat result)
  467. (visit "." '()))
  468. (define (up name stat result)
  469. (visit ".." result))
  470. (define (skip name stat result)
  471. ;; All the sub-directories are skipped.
  472. (visit (basename name) result))
  473. (define (error name* stat errno result)
  474. (if (string=? name name*) ; top-level NAME is unreadable
  475. result
  476. (visit (basename name*) result)))
  477. (and=> (file-system-fold enter? leaf down up skip error #f name lstat)
  478. (lambda (files)
  479. (sort files entry<?))))
  480. (match (scandir (dirname profile)
  481. (cute regexp-exec (profile-regexp profile) <>))
  482. (#f ; no profile directory
  483. '(0))
  484. (() ; no profiles
  485. '(0))
  486. ((profiles ...) ; former profiles around
  487. (sort (map (compose string->number
  488. (cut match:substring <> 1)
  489. (cute regexp-exec (profile-regexp profile) <>))
  490. profiles)
  491. <))))
  492. (define (profile-generations profile)
  493. "Return a list of PROFILE's generations."
  494. (let ((generations (generation-numbers profile)))
  495. (if (equal? generations '(0))
  496. '()
  497. generations)))
  498. (define (previous-generation-number profile number)
  499. "Return the number of the generation before generation NUMBER of
  500. PROFILE, or 0 if none exists. It could be NUMBER - 1, but it's not the
  501. case when generations have been deleted (there are \"holes\")."
  502. (fold (lambda (candidate highest)
  503. (if (and (< candidate number) (> candidate highest))
  504. candidate
  505. highest))
  506. 0
  507. (generation-numbers profile)))
  508. (define (generation-file-name profile generation)
  509. "Return the file name for PROFILE's GENERATION."
  510. (format #f "~a-~a-link" profile generation))
  511. (define (generation-time profile number)
  512. "Return the creation time of a generation in the UTC format."
  513. (make-time time-utc 0
  514. (stat:ctime (stat (generation-file-name profile number)))))
  515. ;;; profiles.scm ends here