Commit | Line | Data |
---|---|---|
88981dd3 | 1 | ;;; GNU Guix --- Functional package management for GNU |
71c3c3df | 2 | ;;; Copyright © 2013, 2014, 2015, 2017, 2018, 2019, 2020 Ludovic Courtès <ludo@gnu.org> |
88981dd3 | 3 | ;;; Copyright © 2014 Deck Pickard <deck.r.pickard@gmail.com> |
a82a201a | 4 | ;;; Copyright © 2015, 2016 Alex Kost <alezost@gmail.com> |
88981dd3 AK |
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 | ||
21 | (define-module (guix scripts) | |
a82a201a | 22 | #:use-module (guix grafts) |
88981dd3 AK |
23 | #:use-module (guix utils) |
24 | #:use-module (guix ui) | |
430505eb AK |
25 | #:use-module (guix store) |
26 | #:use-module (guix monads) | |
27 | #:use-module (guix packages) | |
28 | #:use-module (guix derivations) | |
795d430d | 29 | #:use-module ((guix profiles) #:select (%profile-directory)) |
55da450a | 30 | #:autoload (guix describe) (current-profile-date) |
62a14bd2 | 31 | #:use-module (guix build syscalls) |
88981dd3 | 32 | #:use-module (srfi srfi-1) |
7fd952e0 | 33 | #:use-module (srfi srfi-19) |
88981dd3 AK |
34 | #:use-module (srfi srfi-37) |
35 | #:use-module (ice-9 match) | |
36 | #:export (args-fold* | |
430505eb AK |
37 | parse-command-line |
38 | maybe-build | |
ad8b83bd | 39 | build-package |
7fd952e0 LC |
40 | build-package-source |
41 | %distro-age-warning | |
62a14bd2 LC |
42 | warn-about-old-distro |
43 | %disk-space-warning | |
44 | warn-about-disk-space)) | |
88981dd3 AK |
45 | |
46 | ;;; Commentary: | |
47 | ;;; | |
48 | ;;; General code for Guix scripts. | |
49 | ;;; | |
50 | ;;; Code: | |
51 | ||
52 | (define (args-fold* options unrecognized-option-proc operand-proc . seeds) | |
53 | "A wrapper on top of `args-fold' that does proper user-facing error | |
54 | reporting." | |
55 | (catch 'misc-error | |
56 | (lambda () | |
57 | (apply args-fold options unrecognized-option-proc | |
58 | operand-proc seeds)) | |
59 | (lambda (key proc msg args . rest) | |
60 | ;; XXX: MSG is not i18n'd. | |
69daee23 | 61 | (leave (G_ "invalid argument: ~a~%") |
88981dd3 AK |
62 | (apply format #f msg args))))) |
63 | ||
64 | (define (environment-build-options) | |
65 | "Return additional build options passed as environment variables." | |
66 | (arguments-from-environment-variable "GUIX_BUILD_OPTIONS")) | |
67 | ||
68 | (define %default-argument-handler | |
69 | ;; The default handler for non-option command-line arguments. | |
70 | (lambda (arg result) | |
71 | (alist-cons 'argument arg result))) | |
72 | ||
73 | (define* (parse-command-line args options seeds | |
74 | #:key | |
a1ff7e1d | 75 | (build-options? #t) |
88981dd3 | 76 | (argument-handler %default-argument-handler)) |
a1ff7e1d LC |
77 | "Parse the command-line arguments ARGS according to OPTIONS (a list of |
78 | SRFI-37 options) and return the result, seeded by SEEDS. When BUILD-OPTIONS? | |
79 | is true, also pass arguments passed via the 'GUIX_BUILD_OPTIONS' environment | |
80 | variable. Command-line options take precedence those passed via | |
81 | 'GUIX_BUILD_OPTIONS'. | |
88981dd3 AK |
82 | |
83 | ARGUMENT-HANDLER is called for non-option arguments, like the 'operand-proc' | |
84 | parameter of 'args-fold'." | |
85 | (define (parse-options-from args seeds) | |
86 | ;; Actual parsing takes place here. | |
87 | (apply args-fold* args options | |
88 | (lambda (opt name arg . rest) | |
69daee23 | 89 | (leave (G_ "~A: unrecognized option~%") name)) |
88981dd3 AK |
90 | argument-handler |
91 | seeds)) | |
92 | ||
93 | (call-with-values | |
94 | (lambda () | |
a1ff7e1d LC |
95 | (if build-options? |
96 | (parse-options-from (environment-build-options) seeds) | |
97 | (apply values seeds))) | |
88981dd3 AK |
98 | (lambda seeds |
99 | ;; ARGS take precedence over what the environment variable specifies. | |
100 | (parse-options-from args seeds)))) | |
101 | ||
430505eb AK |
102 | (define* (maybe-build drvs |
103 | #:key dry-run? use-substitutes?) | |
104 | "Show what will/would be built, and actually build DRVS, unless DRY-RUN? is | |
105 | true." | |
106 | (with-monad %store-monad | |
107 | (>>= (show-what-to-build* drvs | |
108 | #:dry-run? dry-run? | |
109 | #:use-substitutes? use-substitutes?) | |
110 | (lambda (_) | |
111 | (if dry-run? | |
112 | (return #f) | |
113 | (built-derivations drvs)))))) | |
114 | ||
115 | (define* (build-package package | |
116 | #:key dry-run? (use-substitutes? #t) | |
117 | #:allow-other-keys | |
118 | #:rest build-options) | |
119 | "Build PACKAGE using BUILD-OPTIONS acceptable by 'set-build-options'. | |
120 | Show what and how will/would be built." | |
a82a201a | 121 | (mlet %store-monad ((grafting? ((lift0 %graft? %store-monad)))) |
430505eb AK |
122 | (apply set-build-options* |
123 | #:use-substitutes? use-substitutes? | |
124 | (strip-keyword-arguments '(#:dry-run?) build-options)) | |
a82a201a AK |
125 | (mlet %store-monad ((derivation (package->derivation |
126 | package #:graft? (and (not dry-run?) | |
127 | grafting?)))) | |
430505eb AK |
128 | (mbegin %store-monad |
129 | (maybe-build (list derivation) | |
130 | #:use-substitutes? use-substitutes? | |
131 | #:dry-run? dry-run?) | |
132 | (return (show-derivation-outputs derivation)))))) | |
133 | ||
ad8b83bd AK |
134 | (define* (build-package-source package |
135 | #:key dry-run? (use-substitutes? #t) | |
136 | #:allow-other-keys | |
137 | #:rest build-options) | |
138 | "Build PACKAGE source using BUILD-OPTIONS." | |
139 | (mbegin %store-monad | |
140 | (apply set-build-options* | |
141 | #:use-substitutes? use-substitutes? | |
142 | (strip-keyword-arguments '(#:dry-run?) build-options)) | |
143 | (mlet %store-monad ((derivation (origin->derivation | |
144 | (package-source package)))) | |
145 | (mbegin %store-monad | |
146 | (maybe-build (list derivation) | |
147 | #:use-substitutes? use-substitutes? | |
148 | #:dry-run? dry-run?) | |
149 | (return (show-derivation-outputs derivation)))))) | |
150 | ||
7fd952e0 LC |
151 | (define %distro-age-warning |
152 | ;; The age (in seconds) above which we warn that the distro is too old. | |
153 | (make-parameter (match (and=> (getenv "GUIX_DISTRO_AGE_WARNING") | |
154 | string->duration) | |
155 | (#f (* 7 24 3600)) | |
156 | (age (time-second age))))) | |
157 | ||
158 | (define* (warn-about-old-distro #:optional (old (%distro-age-warning)) | |
159 | #:key (suggested-command | |
160 | "guix package -u")) | |
161 | "Emit a warning if Guix is older than OLD seconds." | |
55da450a LC |
162 | (define (seconds->days seconds) |
163 | (round (/ seconds (* 3600 24)))) | |
164 | ||
165 | (define age | |
166 | (match (current-profile-date) | |
167 | (#f #f) | |
168 | (date (- (time-second (current-time time-utc)) | |
169 | date)))) | |
170 | ||
171 | (when (and age (>= age old)) | |
172 | (warning (N_ "Your Guix installation is ~a day old.\n" | |
173 | "Your Guix installation is ~a days old.\n" | |
174 | (seconds->days age)) | |
175 | (seconds->days age))) | |
9e5f2060 LC |
176 | (when (and (or (not age) (>= age old)) |
177 | (not (getenv "GUIX_UNINSTALLED"))) | |
55da450a | 178 | (warning (G_ "Consider running 'guix pull' followed by |
7fd952e0 | 179 | '~a' to get up-to-date packages and security updates.\n") |
55da450a LC |
180 | suggested-command) |
181 | (newline (guix-warning-port)))) | |
7fd952e0 | 182 | |
62a14bd2 | 183 | (define %disk-space-warning |
fb7eec3a PN |
184 | ;; Return a pair of absolute threshold (number of bytes) and relative |
185 | ;; threshold (fraction between 0 and 1) for the free disk space below which | |
186 | ;; a warning is emitted. | |
187 | ;; GUIX_DISK_SPACE_WARNING can contain both thresholds. A value in [0;100) | |
188 | ;; is a relative threshold, otherwise it's absolute. The following | |
189 | ;; example values are valid: | |
190 | ;; - 1GiB;10% ;1 GiB absolute, and 10% relative. | |
191 | ;; - 15G ;15 GiB absolute, and default relative. | |
192 | ;; - 99% ;99% relative, and default absolute. | |
193 | ;; - 99 ;Same. | |
194 | ;; - 100 ;100 absolute, and default relative. | |
195 | (let* ((default-absolute-threshold (size->number "5GiB")) | |
196 | (default-relative-threshold 0.05) | |
197 | (percentage->float (lambda (percentage) | |
198 | (or (and=> (string->number | |
199 | (car (string-split percentage #\%))) | |
200 | (lambda (n) (/ n 100.0))) | |
201 | default-relative-threshold))) | |
202 | (size->number* (lambda (size) | |
203 | (or (false-if-exception (size->number size)) | |
204 | default-absolute-threshold))) | |
205 | (absolute? (lambda (size) | |
206 | (not (or (string-suffix? "%" size) | |
207 | (false-if-exception (< (size->number size) 100))))))) | |
208 | (make-parameter | |
209 | (match (getenv "GUIX_DISK_SPACE_WARNING") | |
210 | (#f (list default-absolute-threshold | |
211 | default-relative-threshold)) | |
212 | (env-string (match (string-split env-string #\;) | |
213 | ((threshold) | |
214 | (if (absolute? threshold) | |
215 | (list (size->number* threshold) | |
216 | default-relative-threshold) | |
217 | (list default-absolute-threshold | |
218 | (percentage->float threshold)))) | |
219 | ((threshold1 threshold2) | |
220 | (if (absolute? threshold1) | |
221 | (list (size->number* threshold1) | |
222 | (percentage->float threshold2)) | |
223 | (list (size->number* threshold2) | |
224 | (percentage->float threshold1)))))))))) | |
62a14bd2 LC |
225 | |
226 | (define* (warn-about-disk-space #:optional profile | |
227 | #:key | |
fb7eec3a | 228 | (thresholds (%disk-space-warning))) |
62a14bd2 | 229 | "Display a hint about 'guix gc' if less than THRESHOLD of /gnu/store is |
fb7eec3a | 230 | available. |
71c3c3df LC |
231 | THRESHOLDS is a pair (ABSOLUTE-THRESHOLD . RELATIVE-THRESHOLD)." |
232 | (define GiB (expt 2 30)) | |
233 | ||
62a14bd2 LC |
234 | (let* ((stats (statfs (%store-prefix))) |
235 | (block-size (file-system-block-size stats)) | |
236 | (available (* block-size (file-system-blocks-available stats))) | |
237 | (total (* block-size (file-system-block-count stats))) | |
fb7eec3a | 238 | (relative-threshold-in-bytes (* total (cadr thresholds))) |
71c3c3df | 239 | (absolute-threshold-in-bytes (car thresholds))) |
1d24cc6d | 240 | (when (< available (min relative-threshold-in-bytes |
fb7eec3a PN |
241 | absolute-threshold-in-bytes)) |
242 | (warning (G_ "only ~,1f GiB of free space available on ~a~%") | |
71c3c3df | 243 | (/ available 1. GiB) (%store-prefix)) |
9c074f61 | 244 | (display-hint (format #f (G_ "Consider deleting old profile |
62a14bd2 LC |
245 | generations and collecting garbage, along these lines: |
246 | ||
247 | @example | |
9c074f61 | 248 | guix gc --delete-generations=1m |
fb7eec3a | 249 | @end example\n")))))) |
62a14bd2 | 250 | |
88981dd3 | 251 | ;;; scripts.scm ends here |