scripts.scm 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2013, 2014, 2015, 2017, 2018, 2019, 2020, 2021, 2021 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 utils)
  25. #:use-module (guix ui)
  26. #:use-module (guix store)
  27. #:use-module (guix monads)
  28. #:use-module (guix packages)
  29. #:use-module (guix derivations)
  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. (extension (G_ "extension commands")))
  83. (define-syntax define-command
  84. (syntax-rules (category synopsis)
  85. "Define the given command as a procedure along with its synopsis and,
  86. optionally, its category. The synopsis becomes the docstring of the
  87. procedure, but both the category and synopsis are meant to be read (parsed) by
  88. 'guix help'."
  89. ;; The (synopsis ...) form is here so that xgettext sees those strings as
  90. ;; translatable.
  91. ((_ (name . args)
  92. (synopsis doc) body ...)
  93. (define (name . args)
  94. doc
  95. body ...))
  96. ((_ (name . args)
  97. (category cat) (synopsis doc)
  98. body ...)
  99. (begin
  100. (assert-valid-command-category cat)
  101. (define (name . args)
  102. doc
  103. body ...)))))
  104. (define (option-hint guess options)
  105. "Return the closest long-name OPTIONS from GUESS,
  106. according to'string-distance'."
  107. (define (options->long-names options)
  108. (filter string? (append-map option-names options)))
  109. (match guess
  110. ((? string?)
  111. (match (string-split guess #\=)
  112. ((name rest ...)
  113. (string-closest name (options->long-names options) #:threshold 3))))
  114. (_ #f)))
  115. (define (args-fold* args options unrecognized-option-proc operand-proc . seeds)
  116. "A wrapper on top of `args-fold' that does proper user-facing error
  117. reporting."
  118. (catch 'misc-error
  119. (lambda ()
  120. (apply args-fold args options unrecognized-option-proc
  121. operand-proc seeds))
  122. (lambda (key proc msg args . rest)
  123. ;; XXX: MSG is not i18n'd.
  124. (leave (G_ "invalid argument: ~a~%")
  125. (apply format #f msg args)))))
  126. (define (environment-build-options)
  127. "Return additional build options passed as environment variables."
  128. (arguments-from-environment-variable "GUIX_BUILD_OPTIONS"))
  129. (define %default-argument-handler
  130. ;; The default handler for non-option command-line arguments.
  131. (lambda (arg result)
  132. (alist-cons 'argument arg result)))
  133. (define* (parse-command-line args options seeds
  134. #:key
  135. (build-options? #t)
  136. (argument-handler %default-argument-handler))
  137. "Parse the command-line arguments ARGS according to OPTIONS (a list of
  138. SRFI-37 options) and return the result, seeded by SEEDS. When BUILD-OPTIONS?
  139. is true, also pass arguments passed via the 'GUIX_BUILD_OPTIONS' environment
  140. variable. Command-line options take precedence those passed via
  141. 'GUIX_BUILD_OPTIONS'.
  142. ARGUMENT-HANDLER is called for non-option arguments, like the 'operand-proc'
  143. parameter of 'args-fold'."
  144. (define (parse-options-from args seeds)
  145. ;; Actual parsing takes place here.
  146. (apply args-fold* args options
  147. (lambda (opt name arg . rest)
  148. (let ((hint (option-hint name options)))
  149. (report-error (G_ "~A: unrecognized option~%") name)
  150. (when hint
  151. (display-hint
  152. (format #f (G_ "Did you mean @code{~a}?~%") hint)))
  153. (exit 1)))
  154. argument-handler
  155. seeds))
  156. (call-with-values
  157. (lambda ()
  158. (if build-options?
  159. (parse-options-from (environment-build-options) seeds)
  160. (apply values seeds)))
  161. (lambda seeds
  162. ;; ARGS take precedence over what the environment variable specifies.
  163. (parse-options-from args seeds))))
  164. (define* (maybe-build drvs
  165. #:key dry-run? use-substitutes?)
  166. "Show what will/would be built, and actually build DRVS, unless DRY-RUN? is
  167. true."
  168. (with-monad %store-monad
  169. (>>= (show-what-to-build* drvs
  170. #:dry-run? dry-run?
  171. #:use-substitutes? use-substitutes?)
  172. (lambda (_)
  173. (if dry-run?
  174. (return #f)
  175. (built-derivations drvs))))))
  176. (define* (build-package package
  177. #:key dry-run? (use-substitutes? #t)
  178. #:allow-other-keys
  179. #:rest build-options)
  180. "Build PACKAGE using BUILD-OPTIONS acceptable by 'set-build-options'.
  181. Show what and how will/would be built."
  182. (mlet %store-monad ((grafting? ((lift0 %graft? %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 (package->derivation
  187. package #:graft? (and (not dry-run?)
  188. grafting?))))
  189. (mbegin %store-monad
  190. (maybe-build (list derivation)
  191. #:use-substitutes? use-substitutes?
  192. #:dry-run? dry-run?)
  193. (return (show-derivation-outputs derivation))))))
  194. (define* (build-package-source package
  195. #:key dry-run? (use-substitutes? #t)
  196. #:allow-other-keys
  197. #:rest build-options)
  198. "Build PACKAGE source using BUILD-OPTIONS."
  199. (mbegin %store-monad
  200. (apply set-build-options*
  201. #:use-substitutes? use-substitutes?
  202. (strip-keyword-arguments '(#:dry-run?) build-options))
  203. (mlet %store-monad ((derivation (origin->derivation
  204. (package-source package))))
  205. (mbegin %store-monad
  206. (maybe-build (list derivation)
  207. #:use-substitutes? use-substitutes?
  208. #:dry-run? dry-run?)
  209. (return (show-derivation-outputs derivation))))))
  210. (define %distro-age-warning
  211. ;; The age (in seconds) above which we warn that the distro is too old.
  212. (make-parameter (match (and=> (getenv "GUIX_DISTRO_AGE_WARNING")
  213. string->duration)
  214. (#f (* 7 24 3600))
  215. (age (time-second age)))))
  216. (define* (warn-about-old-distro #:optional (old (%distro-age-warning))
  217. #:key (suggested-command
  218. "guix package -u"))
  219. "Emit a warning if Guix is older than OLD seconds."
  220. (define (seconds->days seconds)
  221. (round (/ seconds (* 3600 24))))
  222. (define age
  223. (match (current-profile-date)
  224. (#f #f)
  225. (date (- (time-second (current-time time-utc))
  226. date))))
  227. (when (and age (>= age old))
  228. (warning (N_ "Your Guix installation is ~a day old.\n"
  229. "Your Guix installation is ~a days old.\n"
  230. (seconds->days age))
  231. (seconds->days age)))
  232. (when (and (or (not age) (>= age old))
  233. (not (getenv "GUIX_UNINSTALLED")))
  234. (warning (G_ "Consider running 'guix pull' followed by
  235. '~a' to get up-to-date packages and security updates.\n")
  236. suggested-command)
  237. (newline (guix-warning-port))))
  238. (define %disk-space-warning
  239. ;; Return a pair of absolute threshold (number of bytes) and relative
  240. ;; threshold (fraction between 0 and 1) for the free disk space below which
  241. ;; a warning is emitted.
  242. ;; GUIX_DISK_SPACE_WARNING can contain both thresholds. A value in [0;100)
  243. ;; is a relative threshold, otherwise it's absolute. The following
  244. ;; example values are valid:
  245. ;; - 1GiB;10% ;1 GiB absolute, and 10% relative.
  246. ;; - 15G ;15 GiB absolute, and default relative.
  247. ;; - 99% ;99% relative, and default absolute.
  248. ;; - 99 ;Same.
  249. ;; - 100 ;100 absolute, and default relative.
  250. (let* ((default-absolute-threshold (size->number "5GiB"))
  251. (default-relative-threshold 0.05)
  252. (percentage->float (lambda (percentage)
  253. (or (and=> (string->number
  254. (car (string-split percentage #\%)))
  255. (lambda (n) (/ n 100.0)))
  256. default-relative-threshold)))
  257. (size->number* (lambda (size)
  258. (or (false-if-exception (size->number size))
  259. default-absolute-threshold)))
  260. (absolute? (lambda (size)
  261. (not (or (string-suffix? "%" size)
  262. (false-if-exception (< (size->number size) 100)))))))
  263. (make-parameter
  264. (match (getenv "GUIX_DISK_SPACE_WARNING")
  265. (#f (list default-absolute-threshold
  266. default-relative-threshold))
  267. (env-string (match (string-split env-string #\;)
  268. ((threshold)
  269. (if (absolute? threshold)
  270. (list (size->number* threshold)
  271. default-relative-threshold)
  272. (list default-absolute-threshold
  273. (percentage->float threshold))))
  274. ((threshold1 threshold2)
  275. (if (absolute? threshold1)
  276. (list (size->number* threshold1)
  277. (percentage->float threshold2))
  278. (list (size->number* threshold2)
  279. (percentage->float threshold1))))))))))
  280. (define* (warn-about-disk-space #:optional profile
  281. #:key
  282. (thresholds (%disk-space-warning)))
  283. "Display a hint about 'guix gc' if less than THRESHOLD of /gnu/store is
  284. available.
  285. THRESHOLDS is a pair (ABSOLUTE-THRESHOLD . RELATIVE-THRESHOLD)."
  286. (define GiB (expt 2 30))
  287. (let* ((stats (statfs (%store-prefix)))
  288. (block-size (file-system-block-size stats))
  289. (available (* block-size (file-system-blocks-available stats)))
  290. (total (* block-size (file-system-block-count stats)))
  291. (relative-threshold-in-bytes (* total (cadr thresholds)))
  292. (absolute-threshold-in-bytes (car thresholds)))
  293. (when (< available (min relative-threshold-in-bytes
  294. absolute-threshold-in-bytes))
  295. (warning (G_ "only ~,1f GiB of free space available on ~a~%")
  296. (/ available 1. GiB) (%store-prefix))
  297. (display-hint (G_ "Consider deleting old profile
  298. generations and collecting garbage, along these lines:
  299. @example
  300. guix gc --delete-generations=1m
  301. @end example\n")))))
  302. ;;; scripts.scm ends here