Mirror of GNU Guix
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.

553 lines
20 KiB

monads: Move '%store-monad' and related procedures where they belong. This turns (guix monads) into a generic module for monads, and moves the store monad and related monadic procedures in their corresponding module. * guix/monads.scm (store-return, store-bind, %store-monad, store-lift, text-file, interned-file, package-file, package->derivation, package->cross-derivation, origin->derivation, imported-modules, compiled, modules, built-derivations, run-with-store): Move to... * guix/store.scm (store-return, store-bind, %store-monad, store-lift, text-file, interned-file): ... here. (%guile-for-build): New variable. (run-with-store): Moved from monads.scm. Remove default value for #:guile-for-build. * guix/packages.scm (default-guile): Export. (set-guile-for-build): New procedure. (package-file, package->derivation, package->cross-derivation, origin->derivation): Moved from monads.scm. * guix/derivations.scm (%guile-for-build): Remove. (imported-modules): Rename to... (%imported-modules): ... this. (compiled-modules): Rename to... (%compiled-modules): ... this. (built-derivations, imported-modules, compiled-modules): New procedures. * gnu/services/avahi.scm, gnu/services/base.scm, gnu/services/dbus.scm, gnu/services/dmd.scm, gnu/services/networking.scm, gnu/services/ssh.scm, gnu/services/xorg.scm, gnu/system/install.scm, gnu/system/linux-initrd.scm, gnu/system/shadow.scm, guix/download.scm, guix/gexp.scm, guix/git-download.scm, guix/profiles.scm, guix/svn-download.scm, tests/monads.scm: Adjust imports accordingly. * guix/monad-repl.scm (default-guile-derivation): New procedure. (store-monad-language, run-in-store): Use it. * build-aux/hydra/gnu-system.scm (qemu-jobs): Add explicit 'set-guile-for-build' call. * guix/scripts/archive.scm (derivation-from-expression): Likewise. * guix/scripts/build.scm (options/resolve-packages): Likewise. * guix/scripts/environment.scm (guix-environment): Likewise. * guix/scripts/system.scm (guix-system): Likewise. * doc/guix.texi (The Store Monad): Adjust module names accordingly.
7 years ago
monads: Move '%store-monad' and related procedures where they belong. This turns (guix monads) into a generic module for monads, and moves the store monad and related monadic procedures in their corresponding module. * guix/monads.scm (store-return, store-bind, %store-monad, store-lift, text-file, interned-file, package-file, package->derivation, package->cross-derivation, origin->derivation, imported-modules, compiled, modules, built-derivations, run-with-store): Move to... * guix/store.scm (store-return, store-bind, %store-monad, store-lift, text-file, interned-file): ... here. (%guile-for-build): New variable. (run-with-store): Moved from monads.scm. Remove default value for #:guile-for-build. * guix/packages.scm (default-guile): Export. (set-guile-for-build): New procedure. (package-file, package->derivation, package->cross-derivation, origin->derivation): Moved from monads.scm. * guix/derivations.scm (%guile-for-build): Remove. (imported-modules): Rename to... (%imported-modules): ... this. (compiled-modules): Rename to... (%compiled-modules): ... this. (built-derivations, imported-modules, compiled-modules): New procedures. * gnu/services/avahi.scm, gnu/services/base.scm, gnu/services/dbus.scm, gnu/services/dmd.scm, gnu/services/networking.scm, gnu/services/ssh.scm, gnu/services/xorg.scm, gnu/system/install.scm, gnu/system/linux-initrd.scm, gnu/system/shadow.scm, guix/download.scm, guix/gexp.scm, guix/git-download.scm, guix/profiles.scm, guix/svn-download.scm, tests/monads.scm: Adjust imports accordingly. * guix/monad-repl.scm (default-guile-derivation): New procedure. (store-monad-language, run-in-store): Use it. * build-aux/hydra/gnu-system.scm (qemu-jobs): Add explicit 'set-guile-for-build' call. * guix/scripts/archive.scm (derivation-from-expression): Likewise. * guix/scripts/build.scm (options/resolve-packages): Likewise. * guix/scripts/environment.scm (guix-environment): Likewise. * guix/scripts/system.scm (guix-system): Likewise. * doc/guix.texi (The Store Monad): Adjust module names accordingly.
7 years ago
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2013, 2014, 2015 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 utils)
  22. #:use-module (guix records)
  23. #:use-module (guix derivations)
  24. #:use-module (guix packages)
  25. #:use-module (guix gexp)
  26. #:use-module (guix monads)
  27. #:use-module (guix store)
  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. #:use-module (srfi srfi-34)
  38. #:use-module (srfi srfi-35)
  39. #:export (&profile-error
  40. profile-error?
  41. profile-error-profile
  42. &profile-not-found-error
  43. profile-not-found-error?
  44. &missing-generation-error
  45. missing-generation-error?
  46. missing-generation-error-generation
  47. manifest make-manifest
  48. manifest?
  49. manifest-entries
  50. <manifest-entry> ; FIXME: eventually make it internal
  51. manifest-entry
  52. manifest-entry?
  53. manifest-entry-name
  54. manifest-entry-version
  55. manifest-entry-output
  56. manifest-entry-item
  57. manifest-entry-dependencies
  58. manifest-pattern
  59. manifest-pattern?
  60. manifest-remove
  61. manifest-add
  62. manifest-lookup
  63. manifest-installed?
  64. manifest-matching-entries
  65. manifest-transaction
  66. manifest-transaction?
  67. manifest-transaction-install
  68. manifest-transaction-remove
  69. manifest-perform-transaction
  70. manifest-transaction-effects
  71. profile-manifest
  72. package->manifest-entry
  73. profile-derivation
  74. generation-number
  75. generation-numbers
  76. profile-generations
  77. relative-generation
  78. previous-generation-number
  79. generation-time
  80. generation-file-name))
  81. ;;; Commentary:
  82. ;;;
  83. ;;; Tools to create and manipulate profiles---i.e., the representation of a
  84. ;;; set of installed packages.
  85. ;;;
  86. ;;; Code:
  87. ;;;
  88. ;;; Condition types.
  89. ;;;
  90. (define-condition-type &profile-error &error
  91. profile-error?
  92. (profile profile-error-profile))
  93. (define-condition-type &profile-not-found-error &profile-error
  94. profile-not-found-error?)
  95. (define-condition-type &missing-generation-error &profile-error
  96. missing-generation-error?
  97. (generation missing-generation-error-generation))
  98. ;;;
  99. ;;; Manifests.
  100. ;;;
  101. (define-record-type <manifest>
  102. (manifest entries)
  103. manifest?
  104. (entries manifest-entries)) ; list of <manifest-entry>
  105. ;; Convenient alias, to avoid name clashes.
  106. (define make-manifest manifest)
  107. (define-record-type* <manifest-entry> manifest-entry
  108. make-manifest-entry
  109. manifest-entry?
  110. (name manifest-entry-name) ; string
  111. (version manifest-entry-version) ; string
  112. (output manifest-entry-output ; string
  113. (default "out"))
  114. (item manifest-entry-item) ; package | store path
  115. (dependencies manifest-entry-dependencies ; (store path | package)*
  116. (default '())))
  117. (define-record-type* <manifest-pattern> manifest-pattern
  118. make-manifest-pattern
  119. manifest-pattern?
  120. (name manifest-pattern-name) ; string
  121. (version manifest-pattern-version ; string | #f
  122. (default #f))
  123. (output manifest-pattern-output ; string | #f
  124. (default "out")))
  125. (define (profile-manifest profile)
  126. "Return the PROFILE's manifest."
  127. (let ((file (string-append profile "/manifest")))
  128. (if (file-exists? file)
  129. (call-with-input-file file read-manifest)
  130. (manifest '()))))
  131. (define* (package->manifest-entry package #:optional output)
  132. "Return a manifest entry for the OUTPUT of package PACKAGE. When OUTPUT is
  133. omitted or #f, use the first output of PACKAGE."
  134. (let ((deps (map (match-lambda
  135. ((label package)
  136. `(,package "out"))
  137. ((label package output)
  138. `(,package ,output)))
  139. (package-transitive-propagated-inputs package))))
  140. (manifest-entry
  141. (name (package-name package))
  142. (version (package-version package))
  143. (output (or output (car (package-outputs package))))
  144. (item package)
  145. (dependencies (delete-duplicates deps)))))
  146. (define (manifest->gexp manifest)
  147. "Return a representation of MANIFEST as a gexp."
  148. (define (entry->gexp entry)
  149. (match entry
  150. (($ <manifest-entry> name version output (? string? path) (deps ...))
  151. #~(#$name #$version #$output #$path #$deps))
  152. (($ <manifest-entry> name version output (? package? package) (deps ...))
  153. #~(#$name #$version #$output
  154. (ungexp package (or output "out")) #$deps))))
  155. (match manifest
  156. (($ <manifest> (entries ...))
  157. #~(manifest (version 1)
  158. (packages #$(map entry->gexp entries))))))
  159. (define (sexp->manifest sexp)
  160. "Parse SEXP as a manifest."
  161. (match sexp
  162. (('manifest ('version 0)
  163. ('packages ((name version output path) ...)))
  164. (manifest
  165. (map (lambda (name version output path)
  166. (manifest-entry
  167. (name name)
  168. (version version)
  169. (output output)
  170. (item path)))
  171. name version output path)))
  172. ;; Version 1 adds a list of propagated inputs to the
  173. ;; name/version/output/path tuples.
  174. (('manifest ('version 1)
  175. ('packages ((name version output path deps) ...)))
  176. (manifest
  177. (map (lambda (name version output path deps)
  178. ;; Up to Guix 0.7 included, dependencies were listed as ("gmp"
  179. ;; "/gnu/store/...-gmp") for instance. Discard the 'label' in
  180. ;; such lists.
  181. (let ((deps (match deps
  182. (((labels directories) ...)
  183. directories)
  184. ((directories ...)
  185. directories))))
  186. (manifest-entry
  187. (name name)
  188. (version version)
  189. (output output)
  190. (item path)
  191. (dependencies deps))))
  192. name version output path deps)))
  193. (_
  194. (error "unsupported manifest format" manifest))))
  195. (define (read-manifest port)
  196. "Return the packages listed in MANIFEST."
  197. (sexp->manifest (read port)))
  198. (define (entry-predicate pattern)
  199. "Return a procedure that returns #t when passed a manifest entry that
  200. matches NAME/OUTPUT/VERSION. OUTPUT and VERSION may be #f, in which case they
  201. are ignored."
  202. (match pattern
  203. (($ <manifest-pattern> name version output)
  204. (match-lambda
  205. (($ <manifest-entry> entry-name entry-version entry-output)
  206. (and (string=? entry-name name)
  207. (or (not entry-output) (not output)
  208. (string=? entry-output output))
  209. (or (not version)
  210. (string=? entry-version version))))))))
  211. (define (manifest-remove manifest patterns)
  212. "Remove entries for each of PATTERNS from MANIFEST. Each item in PATTERNS
  213. must be a manifest-pattern."
  214. (define (remove-entry pattern lst)
  215. (remove (entry-predicate pattern) lst))
  216. (make-manifest (fold remove-entry
  217. (manifest-entries manifest)
  218. patterns)))
  219. (define (manifest-add manifest entries)
  220. "Add a list of manifest ENTRIES to MANIFEST and return new manifest.
  221. Remove MANIFEST entries that have the same name and output as ENTRIES."
  222. (define (same-entry? entry name output)
  223. (match entry
  224. (($ <manifest-entry> entry-name _ entry-output _ ...)
  225. (and (equal? name entry-name)
  226. (equal? output entry-output)))))
  227. (make-manifest
  228. (append entries
  229. (fold (lambda (entry result)
  230. (match entry
  231. (($ <manifest-entry> name _ out _ ...)
  232. (filter (negate (cut same-entry? <> name out))
  233. result))))
  234. (manifest-entries manifest)
  235. entries))))
  236. (define (manifest-lookup manifest pattern)
  237. "Return the first item of MANIFEST that matches PATTERN, or #f if there is
  238. no match.."
  239. (find (entry-predicate pattern)
  240. (manifest-entries manifest)))
  241. (define (manifest-installed? manifest pattern)
  242. "Return #t if MANIFEST has an entry matching PATTERN (a manifest-pattern),
  243. #f otherwise."
  244. (->bool (manifest-lookup manifest pattern)))
  245. (define (manifest-matching-entries manifest patterns)
  246. "Return all the entries of MANIFEST that match one of the PATTERNS."
  247. (define predicates
  248. (map entry-predicate patterns))
  249. (define (matches? entry)
  250. (any (lambda (pred)
  251. (pred entry))
  252. predicates))
  253. (filter matches? (manifest-entries manifest)))
  254. ;;;
  255. ;;; Manifest transactions.
  256. ;;;
  257. (define-record-type* <manifest-transaction> manifest-transaction
  258. make-manifest-transaction
  259. manifest-transaction?
  260. (install manifest-transaction-install ; list of <manifest-entry>
  261. (default '()))
  262. (remove manifest-transaction-remove ; list of <manifest-pattern>
  263. (default '())))
  264. (define (manifest-transaction-effects manifest transaction)
  265. "Compute the effect of applying TRANSACTION to MANIFEST. Return 4 values:
  266. the list of packages that would be removed, installed, upgraded, or downgraded
  267. when applying TRANSACTION to MANIFEST. Upgrades are represented as pairs
  268. where the head is the entry being upgraded and the tail is the entry that will
  269. replace it."
  270. (define (manifest-entry->pattern entry)
  271. (manifest-pattern
  272. (name (manifest-entry-name entry))
  273. (output (manifest-entry-output entry))))
  274. (let loop ((input (manifest-transaction-install transaction))
  275. (install '())
  276. (upgrade '())
  277. (downgrade '()))
  278. (match input
  279. (()
  280. (let ((remove (manifest-transaction-remove transaction)))
  281. (values (manifest-matching-entries manifest remove)
  282. (reverse install) (reverse upgrade) (reverse downgrade))))
  283. ((entry rest ...)
  284. ;; Check whether installing ENTRY corresponds to the installation of a
  285. ;; new package or to an upgrade.
  286. ;; XXX: When the exact same output directory is installed, we're not
  287. ;; really upgrading anything. Add a check for that case.
  288. (let* ((pattern (manifest-entry->pattern entry))
  289. (previous (manifest-lookup manifest pattern))
  290. (newer? (and previous
  291. (version>=? (manifest-entry-version entry)
  292. (manifest-entry-version previous)))))
  293. (loop rest
  294. (if previous install (cons entry install))
  295. (if (and previous newer?)
  296. (alist-cons previous entry upgrade)
  297. upgrade)
  298. (if (and previous (not newer?))
  299. (alist-cons previous entry downgrade)
  300. downgrade)))))))
  301. (define (manifest-perform-transaction manifest transaction)
  302. "Perform TRANSACTION on MANIFEST and return new manifest."
  303. (let ((install (manifest-transaction-install transaction))
  304. (remove (manifest-transaction-remove transaction)))
  305. (manifest-add (manifest-remove manifest remove)
  306. install)))
  307. ;;;
  308. ;;; Profiles.
  309. ;;;
  310. (define (manifest-inputs manifest)
  311. "Return the list of inputs for MANIFEST. Each input has one of the
  312. following forms:
  313. (PACKAGE OUTPUT-NAME)
  314. or
  315. STORE-PATH
  316. "
  317. (append-map (match-lambda
  318. (($ <manifest-entry> name version
  319. output (? package? package) deps)
  320. `((,package ,output) ,@deps))
  321. (($ <manifest-entry> name version output path deps)
  322. ;; Assume PATH and DEPS are already valid.
  323. `(,path ,@deps)))
  324. (manifest-entries manifest)))
  325. (define (info-dir-file manifest)
  326. "Return a derivation that builds the 'dir' file for all the entries of
  327. MANIFEST."
  328. (define texinfo ;lazy reference
  329. (module-ref (resolve-interface '(gnu packages texinfo)) 'texinfo))
  330. (define gzip ;lazy reference
  331. (module-ref (resolve-interface '(gnu packages compression)) 'gzip))
  332. (define build
  333. #~(begin
  334. (use-modules (guix build utils)
  335. (srfi srfi-1) (srfi srfi-26)
  336. (ice-9 ftw))
  337. (define (info-file? file)
  338. (or (string-suffix? ".info" file)
  339. (string-suffix? ".info.gz" file)))
  340. (define (info-files top)
  341. (let ((infodir (string-append top "/share/info")))
  342. (map (cut string-append infodir "/" <>)
  343. (or (scandir infodir info-file?) '()))))
  344. (define (install-info info)
  345. (setenv "PATH" (string-append #+gzip "/bin")) ;for info.gz files
  346. (zero?
  347. (system* (string-append #+texinfo "/bin/install-info")
  348. info (string-append #$output "/share/info/dir"))))
  349. (mkdir-p (string-append #$output "/share/info"))
  350. (every install-info
  351. (append-map info-files
  352. '#$(manifest-inputs manifest)))))
  353. ;; Don't depend on Texinfo when there's nothing to do.
  354. (if (null? (manifest-entries manifest))
  355. (gexp->derivation "info-dir" #~(mkdir #$output))
  356. (gexp->derivation "info-dir" build
  357. #:modules '((guix build utils)))))
  358. (define* (profile-derivation manifest #:key (info-dir? #t))
  359. "Return a derivation that builds a profile (aka. 'user environment') with
  360. the given MANIFEST. The profile includes a top-level Info 'dir' file, unless
  361. INFO-DIR? is #f."
  362. (mlet %store-monad ((info-dir (if info-dir?
  363. (info-dir-file manifest)
  364. (return #f))))
  365. (define inputs
  366. (if info-dir
  367. ;; XXX: Here we use the tuple (INFO-DIR "out") just so that the list
  368. ;; is unambiguous for the gexp code when MANIFEST has a single input
  369. ;; denoted as a string (the pattern (DRV STRING) is normally
  370. ;; interpreted in a gexp as "the STRING output of DRV".). See
  371. ;; <http://lists.gnu.org/archive/html/guix-devel/2014-12/msg00292.html>.
  372. (cons (list info-dir "out")
  373. (manifest-inputs manifest))
  374. (manifest-inputs manifest)))
  375. (define builder
  376. #~(begin
  377. (use-modules (ice-9 pretty-print)
  378. (guix build union))
  379. (setvbuf (current-output-port) _IOLBF)
  380. (setvbuf (current-error-port) _IOLBF)
  381. (union-build #$output '#$inputs
  382. #:log-port (%make-void-port "w"))
  383. (call-with-output-file (string-append #$output "/manifest")
  384. (lambda (p)
  385. (pretty-print '#$(manifest->gexp manifest) p)))))
  386. (gexp->derivation "profile" builder
  387. #:modules '((guix build union))
  388. #:local-build? #t)))
  389. (define (profile-regexp profile)
  390. "Return a regular expression that matches PROFILE's name and number."
  391. (make-regexp (string-append "^" (regexp-quote (basename profile))
  392. "-([0-9]+)")))
  393. (define (generation-number profile)
  394. "Return PROFILE's number or 0. An absolute file name must be used."
  395. (or (and=> (false-if-exception (regexp-exec (profile-regexp profile)
  396. (basename (readlink profile))))
  397. (compose string->number (cut match:substring <> 1)))
  398. 0))
  399. (define (generation-numbers profile)
  400. "Return the sorted list of generation numbers of PROFILE, or '(0) if no
  401. former profiles were found."
  402. (define* (scandir name #:optional (select? (const #t))
  403. (entry<? (@ (ice-9 i18n) string-locale<?)))
  404. ;; XXX: Bug-fix version introduced in Guile v2.0.6-62-g139ce19.
  405. (define (enter? dir stat result)
  406. (and stat (string=? dir name)))
  407. (define (visit basename result)
  408. (if (select? basename)
  409. (cons basename result)
  410. result))
  411. (define (leaf name stat result)
  412. (and result
  413. (visit (basename name) result)))
  414. (define (down name stat result)
  415. (visit "." '()))
  416. (define (up name stat result)
  417. (visit ".." result))
  418. (define (skip name stat result)
  419. ;; All the sub-directories are skipped.
  420. (visit (basename name) result))
  421. (define (error name* stat errno result)
  422. (if (string=? name name*) ; top-level NAME is unreadable
  423. result
  424. (visit (basename name*) result)))
  425. (and=> (file-system-fold enter? leaf down up skip error #f name lstat)
  426. (lambda (files)
  427. (sort files entry<?))))
  428. (match (scandir (dirname profile)
  429. (cute regexp-exec (profile-regexp profile) <>))
  430. (#f ; no profile directory
  431. '(0))
  432. (() ; no profiles
  433. '(0))
  434. ((profiles ...) ; former profiles around
  435. (sort (map (compose string->number
  436. (cut match:substring <> 1)
  437. (cute regexp-exec (profile-regexp profile) <>))
  438. profiles)
  439. <))))
  440. (define (profile-generations profile)
  441. "Return a list of PROFILE's generations."
  442. (let ((generations (generation-numbers profile)))
  443. (if (equal? generations '(0))
  444. '()
  445. generations)))
  446. (define* (relative-generation profile shift #:optional
  447. (current (generation-number profile)))
  448. "Return PROFILE's generation shifted from the CURRENT generation by SHIFT.
  449. SHIFT is a positive or negative number.
  450. Return #f if there is no such generation."
  451. (let* ((abs-shift (abs shift))
  452. (numbers (profile-generations profile))
  453. (from-current (memq current
  454. (if (negative? shift)
  455. (reverse numbers)
  456. numbers))))
  457. (and from-current
  458. (< abs-shift (length from-current))
  459. (list-ref from-current abs-shift))))
  460. (define* (previous-generation-number profile #:optional
  461. (number (generation-number profile)))
  462. "Return the number of the generation before generation NUMBER of
  463. PROFILE, or 0 if none exists. It could be NUMBER - 1, but it's not the
  464. case when generations have been deleted (there are \"holes\")."
  465. (or (relative-generation profile -1 number)
  466. 0))
  467. (define (generation-file-name profile generation)
  468. "Return the file name for PROFILE's GENERATION."
  469. (format #f "~a-~a-link" profile generation))
  470. (define (generation-time profile number)
  471. "Return the creation time of a generation in the UTC format."
  472. (make-time time-utc 0
  473. (stat:ctime (stat (generation-file-name profile number)))))
  474. ;;; profiles.scm ends here