scripts.scm 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2013, 2014, 2015, 2017, 2018, 2019, 2020 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. ;;; Copyright © 2020 Jan (janneke) Nieuwenhuizen <janneke@gnu.org>
  6. ;;;
  7. ;;; This file is part of GNU Guix.
  8. ;;;
  9. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  10. ;;; under the terms of the GNU General Public License as published by
  11. ;;; the Free Software Foundation; either version 3 of the License, or (at
  12. ;;; your option) any later version.
  13. ;;;
  14. ;;; GNU Guix is distributed in the hope that it will be useful, but
  15. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  16. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. ;;; GNU General Public License for more details.
  18. ;;;
  19. ;;; You should have received a copy of the GNU General Public License
  20. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  21. (define-module (guix scripts)
  22. #:use-module (guix grafts)
  23. #:use-module (guix utils)
  24. #:use-module (guix ui)
  25. #:use-module (guix store)
  26. #:use-module (guix monads)
  27. #:use-module (guix packages)
  28. #:use-module (guix derivations)
  29. #:use-module ((guix profiles) #:select (%profile-directory))
  30. #:autoload (guix describe) (current-profile-date)
  31. #:use-module (guix build syscalls)
  32. #:use-module (srfi srfi-1)
  33. #:use-module (srfi srfi-19)
  34. #:use-module (srfi srfi-37)
  35. #:use-module (ice-9 match)
  36. #:export (synopsis
  37. category
  38. define-command
  39. %command-categories
  40. args-fold*
  41. parse-command-line
  42. maybe-build
  43. build-package
  44. build-package-source
  45. %distro-age-warning
  46. warn-about-old-distro
  47. %disk-space-warning
  48. warn-about-disk-space))
  49. ;;; Commentary:
  50. ;;;
  51. ;;; General code for Guix scripts.
  52. ;;;
  53. ;;; Code:
  54. ;; Syntactic keywords.
  55. (define synopsis 'command-synopsis)
  56. (define category 'command-category)
  57. (define-syntax define-command-categories
  58. (syntax-rules (G_)
  59. "Define command categories."
  60. ((_ name assert-valid (identifiers (G_ synopses)) ...)
  61. (begin
  62. (define-public identifiers
  63. ;; Define and export syntactic keywords.
  64. (list 'syntactic-keyword-for-command-category))
  65. ...
  66. (define-syntax assert-valid
  67. ;; Validate at expansion time that we're passed a valid category.
  68. (syntax-rules (identifiers ...)
  69. ((_ identifiers) #t)
  70. ...))
  71. (define name
  72. ;; Alist mapping category name to synopsis.
  73. `((identifiers . synopses) ...))))))
  74. ;; Command categories.
  75. (define-command-categories %command-categories
  76. assert-valid-command-category
  77. (main (G_ "main commands"))
  78. (development (G_ "software development commands"))
  79. (packaging (G_ "packaging commands"))
  80. (plumbing (G_ "plumbing commands"))
  81. (internal (G_ "internal commands")))
  82. (define-syntax define-command
  83. (syntax-rules (category synopsis)
  84. "Define the given command as a procedure along with its synopsis and,
  85. optionally, its category. The synopsis becomes the docstring of the
  86. procedure, but both the category and synopsis are meant to be read (parsed) by
  87. 'guix help'."
  88. ;; The (synopsis ...) form is here so that xgettext sees those strings as
  89. ;; translatable.
  90. ((_ (name . args)
  91. (synopsis doc) body ...)
  92. (define (name . args)
  93. doc
  94. body ...))
  95. ((_ (name . args)
  96. (category cat) (synopsis doc)
  97. body ...)
  98. (begin
  99. (assert-valid-command-category cat)
  100. (define (name . args)
  101. doc
  102. body ...)))))
  103. (define (args-fold* args options unrecognized-option-proc operand-proc . seeds)
  104. "A wrapper on top of `args-fold' that does proper user-facing error
  105. reporting."
  106. (catch 'misc-error
  107. (lambda ()
  108. (apply args-fold args options unrecognized-option-proc
  109. operand-proc seeds))
  110. (lambda (key proc msg args . rest)
  111. ;; XXX: MSG is not i18n'd.
  112. (leave (G_ "invalid argument: ~a~%")
  113. (apply format #f msg args)))))
  114. (define (environment-build-options)
  115. "Return additional build options passed as environment variables."
  116. (arguments-from-environment-variable "GUIX_BUILD_OPTIONS"))
  117. (define %default-argument-handler
  118. ;; The default handler for non-option command-line arguments.
  119. (lambda (arg result)
  120. (alist-cons 'argument arg result)))
  121. (define* (parse-command-line args options seeds
  122. #:key
  123. (build-options? #t)
  124. (argument-handler %default-argument-handler))
  125. "Parse the command-line arguments ARGS according to OPTIONS (a list of
  126. SRFI-37 options) and return the result, seeded by SEEDS. When BUILD-OPTIONS?
  127. is true, also pass arguments passed via the 'GUIX_BUILD_OPTIONS' environment
  128. variable. Command-line options take precedence those passed via
  129. 'GUIX_BUILD_OPTIONS'.
  130. ARGUMENT-HANDLER is called for non-option arguments, like the 'operand-proc'
  131. parameter of 'args-fold'."
  132. (define (parse-options-from args seeds)
  133. ;; Actual parsing takes place here.
  134. (apply args-fold* args options
  135. (lambda (opt name arg . rest)
  136. (leave (G_ "~A: unrecognized option~%") name))
  137. argument-handler
  138. seeds))
  139. (call-with-values
  140. (lambda ()
  141. (if build-options?
  142. (parse-options-from (environment-build-options) seeds)
  143. (apply values seeds)))
  144. (lambda seeds
  145. ;; ARGS take precedence over what the environment variable specifies.
  146. (parse-options-from args seeds))))
  147. (define* (maybe-build drvs
  148. #:key dry-run? use-substitutes?)
  149. "Show what will/would be built, and actually build DRVS, unless DRY-RUN? is
  150. true."
  151. (with-monad %store-monad
  152. (>>= (show-what-to-build* drvs
  153. #:dry-run? dry-run?
  154. #:use-substitutes? use-substitutes?)
  155. (lambda (_)
  156. (if dry-run?
  157. (return #f)
  158. (built-derivations drvs))))))
  159. (define* (build-package package
  160. #:key dry-run? (use-substitutes? #t)
  161. #:allow-other-keys
  162. #:rest build-options)
  163. "Build PACKAGE using BUILD-OPTIONS acceptable by 'set-build-options'.
  164. Show what and how will/would be built."
  165. (mlet %store-monad ((grafting? ((lift0 %graft? %store-monad))))
  166. (apply set-build-options*
  167. #:use-substitutes? use-substitutes?
  168. (strip-keyword-arguments '(#:dry-run?) build-options))
  169. (mlet %store-monad ((derivation (package->derivation
  170. package #:graft? (and (not dry-run?)
  171. grafting?))))
  172. (mbegin %store-monad
  173. (maybe-build (list derivation)
  174. #:use-substitutes? use-substitutes?
  175. #:dry-run? dry-run?)
  176. (return (show-derivation-outputs derivation))))))
  177. (define* (build-package-source package
  178. #:key dry-run? (use-substitutes? #t)
  179. #:allow-other-keys
  180. #:rest build-options)
  181. "Build PACKAGE source using BUILD-OPTIONS."
  182. (mbegin %store-monad
  183. (apply set-build-options*
  184. #:use-substitutes? use-substitutes?
  185. (strip-keyword-arguments '(#:dry-run?) build-options))
  186. (mlet %store-monad ((derivation (origin->derivation
  187. (package-source package))))
  188. (mbegin %store-monad
  189. (maybe-build (list derivation)
  190. #:use-substitutes? use-substitutes?
  191. #:dry-run? dry-run?)
  192. (return (show-derivation-outputs derivation))))))
  193. (define %distro-age-warning
  194. ;; The age (in seconds) above which we warn that the distro is too old.
  195. (make-parameter (match (and=> (getenv "GUIX_DISTRO_AGE_WARNING")
  196. string->duration)
  197. (#f (* 7 24 3600))
  198. (age (time-second age)))))
  199. (define* (warn-about-old-distro #:optional (old (%distro-age-warning))
  200. #:key (suggested-command
  201. "guix package -u"))
  202. "Emit a warning if Guix is older than OLD seconds."
  203. (define (seconds->days seconds)
  204. (round (/ seconds (* 3600 24))))
  205. (define age
  206. (match (current-profile-date)
  207. (#f #f)
  208. (date (- (time-second (current-time time-utc))
  209. date))))
  210. (when (and age (>= age old))
  211. (warning (N_ "Your Guix installation is ~a day old.\n"
  212. "Your Guix installation is ~a days old.\n"
  213. (seconds->days age))
  214. (seconds->days age)))
  215. (when (and (or (not age) (>= age old))
  216. (not (getenv "GUIX_UNINSTALLED")))
  217. (warning (G_ "Consider running 'guix pull' followed by
  218. '~a' to get up-to-date packages and security updates.\n")
  219. suggested-command)
  220. (newline (guix-warning-port))))
  221. (define %disk-space-warning
  222. ;; Return a pair of absolute threshold (number of bytes) and relative
  223. ;; threshold (fraction between 0 and 1) for the free disk space below which
  224. ;; a warning is emitted.
  225. ;; GUIX_DISK_SPACE_WARNING can contain both thresholds. A value in [0;100)
  226. ;; is a relative threshold, otherwise it's absolute. The following
  227. ;; example values are valid:
  228. ;; - 1GiB;10% ;1 GiB absolute, and 10% relative.
  229. ;; - 15G ;15 GiB absolute, and default relative.
  230. ;; - 99% ;99% relative, and default absolute.
  231. ;; - 99 ;Same.
  232. ;; - 100 ;100 absolute, and default relative.
  233. (let* ((default-absolute-threshold (size->number "5GiB"))
  234. (default-relative-threshold 0.05)
  235. (percentage->float (lambda (percentage)
  236. (or (and=> (string->number
  237. (car (string-split percentage #\%)))
  238. (lambda (n) (/ n 100.0)))
  239. default-relative-threshold)))
  240. (size->number* (lambda (size)
  241. (or (false-if-exception (size->number size))
  242. default-absolute-threshold)))
  243. (absolute? (lambda (size)
  244. (not (or (string-suffix? "%" size)
  245. (false-if-exception (< (size->number size) 100)))))))
  246. (make-parameter
  247. (match (getenv "GUIX_DISK_SPACE_WARNING")
  248. (#f (list default-absolute-threshold
  249. default-relative-threshold))
  250. (env-string (match (string-split env-string #\;)
  251. ((threshold)
  252. (if (absolute? threshold)
  253. (list (size->number* threshold)
  254. default-relative-threshold)
  255. (list default-absolute-threshold
  256. (percentage->float threshold))))
  257. ((threshold1 threshold2)
  258. (if (absolute? threshold1)
  259. (list (size->number* threshold1)
  260. (percentage->float threshold2))
  261. (list (size->number* threshold2)
  262. (percentage->float threshold1))))))))))
  263. (define* (warn-about-disk-space #:optional profile
  264. #:key
  265. (thresholds (%disk-space-warning)))
  266. "Display a hint about 'guix gc' if less than THRESHOLD of /gnu/store is
  267. available.
  268. THRESHOLDS is a pair (ABSOLUTE-THRESHOLD . RELATIVE-THRESHOLD)."
  269. (define GiB (expt 2 30))
  270. (let* ((stats (statfs (%store-prefix)))
  271. (block-size (file-system-block-size stats))
  272. (available (* block-size (file-system-blocks-available stats)))
  273. (total (* block-size (file-system-block-count stats)))
  274. (relative-threshold-in-bytes (* total (cadr thresholds)))
  275. (absolute-threshold-in-bytes (car thresholds)))
  276. (when (< available (min relative-threshold-in-bytes
  277. absolute-threshold-in-bytes))
  278. (warning (G_ "only ~,1f GiB of free space available on ~a~%")
  279. (/ available 1. GiB) (%store-prefix))
  280. (display-hint (format #f (G_ "Consider deleting old profile
  281. generations and collecting garbage, along these lines:
  282. @example
  283. guix gc --delete-generations=1m
  284. @end example\n"))))))
  285. ;;; scripts.scm ends here