scripts.scm 13 KB

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