scripts.scm 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2013, 2014, 2015, 2017, 2018, 2019 Ludovic Courtès <ludo@gnu.org>
  3. ;;; Copyright © 2014 Deck Pickard <deck.r.pickard@gmail.com>
  4. ;;; Copyright © 2015, 2016 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 scripts)
  21. #:use-module (guix grafts)
  22. #:use-module (guix utils)
  23. #:use-module (guix ui)
  24. #:use-module (guix store)
  25. #:use-module (guix monads)
  26. #:use-module (guix packages)
  27. #:use-module (guix derivations)
  28. #:use-module ((guix profiles) #:select (%profile-directory))
  29. #:autoload (guix describe) (current-profile-date)
  30. #:use-module (guix build syscalls)
  31. #:use-module (srfi srfi-1)
  32. #:use-module (srfi srfi-19)
  33. #:use-module (srfi srfi-37)
  34. #:use-module (ice-9 match)
  35. #:export (args-fold*
  36. parse-command-line
  37. maybe-build
  38. build-package
  39. build-package-source
  40. %distro-age-warning
  41. warn-about-old-distro
  42. %disk-space-warning
  43. warn-about-disk-space))
  44. ;;; Commentary:
  45. ;;;
  46. ;;; General code for Guix scripts.
  47. ;;;
  48. ;;; Code:
  49. (define (args-fold* options unrecognized-option-proc operand-proc . seeds)
  50. "A wrapper on top of `args-fold' that does proper user-facing error
  51. reporting."
  52. (catch 'misc-error
  53. (lambda ()
  54. (apply args-fold options unrecognized-option-proc
  55. operand-proc seeds))
  56. (lambda (key proc msg args . rest)
  57. ;; XXX: MSG is not i18n'd.
  58. (leave (G_ "invalid argument: ~a~%")
  59. (apply format #f msg args)))))
  60. (define (environment-build-options)
  61. "Return additional build options passed as environment variables."
  62. (arguments-from-environment-variable "GUIX_BUILD_OPTIONS"))
  63. (define %default-argument-handler
  64. ;; The default handler for non-option command-line arguments.
  65. (lambda (arg result)
  66. (alist-cons 'argument arg result)))
  67. (define* (parse-command-line args options seeds
  68. #:key
  69. (build-options? #t)
  70. (argument-handler %default-argument-handler))
  71. "Parse the command-line arguments ARGS according to OPTIONS (a list of
  72. SRFI-37 options) and return the result, seeded by SEEDS. When BUILD-OPTIONS?
  73. is true, also pass arguments passed via the 'GUIX_BUILD_OPTIONS' environment
  74. variable. Command-line options take precedence those passed via
  75. 'GUIX_BUILD_OPTIONS'.
  76. ARGUMENT-HANDLER is called for non-option arguments, like the 'operand-proc'
  77. parameter of 'args-fold'."
  78. (define (parse-options-from args seeds)
  79. ;; Actual parsing takes place here.
  80. (apply args-fold* args options
  81. (lambda (opt name arg . rest)
  82. (leave (G_ "~A: unrecognized option~%") name))
  83. argument-handler
  84. seeds))
  85. (call-with-values
  86. (lambda ()
  87. (if build-options?
  88. (parse-options-from (environment-build-options) seeds)
  89. (apply values seeds)))
  90. (lambda seeds
  91. ;; ARGS take precedence over what the environment variable specifies.
  92. (parse-options-from args seeds))))
  93. (define* (maybe-build drvs
  94. #:key dry-run? use-substitutes?)
  95. "Show what will/would be built, and actually build DRVS, unless DRY-RUN? is
  96. true."
  97. (with-monad %store-monad
  98. (>>= (show-what-to-build* drvs
  99. #:dry-run? dry-run?
  100. #:use-substitutes? use-substitutes?)
  101. (lambda (_)
  102. (if dry-run?
  103. (return #f)
  104. (built-derivations drvs))))))
  105. (define* (build-package package
  106. #:key dry-run? (use-substitutes? #t)
  107. #:allow-other-keys
  108. #:rest build-options)
  109. "Build PACKAGE using BUILD-OPTIONS acceptable by 'set-build-options'.
  110. Show what and how will/would be built."
  111. (mlet %store-monad ((grafting? ((lift0 %graft? %store-monad))))
  112. (apply set-build-options*
  113. #:use-substitutes? use-substitutes?
  114. (strip-keyword-arguments '(#:dry-run?) build-options))
  115. (mlet %store-monad ((derivation (package->derivation
  116. package #:graft? (and (not dry-run?)
  117. grafting?))))
  118. (mbegin %store-monad
  119. (maybe-build (list derivation)
  120. #:use-substitutes? use-substitutes?
  121. #:dry-run? dry-run?)
  122. (return (show-derivation-outputs derivation))))))
  123. (define* (build-package-source package
  124. #:key dry-run? (use-substitutes? #t)
  125. #:allow-other-keys
  126. #:rest build-options)
  127. "Build PACKAGE source using BUILD-OPTIONS."
  128. (mbegin %store-monad
  129. (apply set-build-options*
  130. #:use-substitutes? use-substitutes?
  131. (strip-keyword-arguments '(#:dry-run?) build-options))
  132. (mlet %store-monad ((derivation (origin->derivation
  133. (package-source package))))
  134. (mbegin %store-monad
  135. (maybe-build (list derivation)
  136. #:use-substitutes? use-substitutes?
  137. #:dry-run? dry-run?)
  138. (return (show-derivation-outputs derivation))))))
  139. (define %distro-age-warning
  140. ;; The age (in seconds) above which we warn that the distro is too old.
  141. (make-parameter (match (and=> (getenv "GUIX_DISTRO_AGE_WARNING")
  142. string->duration)
  143. (#f (* 7 24 3600))
  144. (age (time-second age)))))
  145. (define* (warn-about-old-distro #:optional (old (%distro-age-warning))
  146. #:key (suggested-command
  147. "guix package -u"))
  148. "Emit a warning if Guix is older than OLD seconds."
  149. (define (seconds->days seconds)
  150. (round (/ seconds (* 3600 24))))
  151. (define age
  152. (match (current-profile-date)
  153. (#f #f)
  154. (date (- (time-second (current-time time-utc))
  155. date))))
  156. (when (and age (>= age old))
  157. (warning (N_ "Your Guix installation is ~a day old.\n"
  158. "Your Guix installation is ~a days old.\n"
  159. (seconds->days age))
  160. (seconds->days age)))
  161. (when (and (or (not age) (>= age old))
  162. (not (getenv "GUIX_UNINSTALLED")))
  163. (warning (G_ "Consider running 'guix pull' followed by
  164. '~a' to get up-to-date packages and security updates.\n")
  165. suggested-command)
  166. (newline (guix-warning-port))))
  167. (define %disk-space-warning
  168. ;; The fraction (between 0 and 1) of free disk space below which a warning
  169. ;; is emitted.
  170. (make-parameter (match (and=> (getenv "GUIX_DISK_SPACE_WARNING")
  171. string->number)
  172. (#f .05) ;5%
  173. (threshold (/ threshold 100.)))))
  174. (define* (warn-about-disk-space #:optional profile
  175. #:key
  176. (threshold (%disk-space-warning)))
  177. "Display a hint about 'guix gc' if less than THRESHOLD of /gnu/store is
  178. available."
  179. (let* ((stats (statfs (%store-prefix)))
  180. (block-size (file-system-block-size stats))
  181. (available (* block-size (file-system-blocks-available stats)))
  182. (total (* block-size (file-system-block-count stats)))
  183. (ratio (/ available total 1.)))
  184. (when (< ratio threshold)
  185. (warning (G_ "only ~,1f% of free space available on ~a~%")
  186. (* ratio 100) (%store-prefix))
  187. (display-hint (format #f (G_ "Consider deleting old profile
  188. generations and collecting garbage, along these lines:
  189. @example
  190. guix gc --delete-generations=1m
  191. @end example\n")
  192. profile)))))
  193. ;;; scripts.scm ends here