scripts.scm 13 KB

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