time-machine.scm 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2019 Konrad Hinsen <konrad.hinsen@fastmail.net>
  3. ;;; Copyright © 2019, 2020, 2021 Ludovic Courtès <ludo@gnu.org>
  4. ;;; Copyright © 2021 Simon Tournier <zimon.toutoune@gmail.com>
  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. (define-module (guix scripts time-machine)
  21. #:use-module (guix ui)
  22. #:use-module (guix scripts)
  23. #:use-module (guix inferior)
  24. #:use-module (guix store)
  25. #:use-module (guix status)
  26. #:use-module ((guix git)
  27. #:select (with-git-error-handling))
  28. #:use-module ((guix utils)
  29. #:select (%current-system))
  30. #:use-module ((guix scripts pull)
  31. #:select (channel-list))
  32. #:use-module ((guix scripts build)
  33. #:select (%standard-build-options
  34. show-build-options-help
  35. set-build-options-from-command-line))
  36. #:use-module (ice-9 match)
  37. #:use-module (srfi srfi-1)
  38. #:use-module (srfi srfi-11)
  39. #:use-module (srfi srfi-26)
  40. #:use-module (srfi srfi-37)
  41. #:export (guix-time-machine))
  42. ;;;
  43. ;;; Command-line options.
  44. ;;;
  45. (define (show-help)
  46. (display (G_ "Usage: guix time-machine [OPTION] -- COMMAND ARGS...
  47. Execute COMMAND ARGS... in an older version of Guix.\n"))
  48. (display (G_ "
  49. -C, --channels=FILE deploy the channels defined in FILE"))
  50. (display (G_ "
  51. --url=URL use the Git repository at URL"))
  52. (display (G_ "
  53. --commit=COMMIT use the specified COMMIT"))
  54. (display (G_ "
  55. --branch=BRANCH use the tip of the specified BRANCH"))
  56. (display (G_ "
  57. --disable-authentication
  58. disable channel authentication"))
  59. (newline)
  60. (show-build-options-help)
  61. (newline)
  62. (display (G_ "
  63. -h, --help display this help and exit"))
  64. (display (G_ "
  65. -V, --version display version information and exit"))
  66. (newline)
  67. (show-bug-report-information))
  68. (define %options
  69. ;; Specifications of the command-line options.
  70. (cons* (option '(#\C "channels") #t #f
  71. (lambda (opt name arg result)
  72. (alist-cons 'channel-file arg result)))
  73. (option '("url") #t #f
  74. (lambda (opt name arg result)
  75. (alist-cons 'repository-url arg
  76. (alist-delete 'repository-url result))))
  77. (option '("commit") #t #f
  78. (lambda (opt name arg result)
  79. (alist-cons 'ref `(commit . ,arg) result)))
  80. (option '("branch") #t #f
  81. (lambda (opt name arg result)
  82. (alist-cons 'ref `(branch . ,arg) result)))
  83. (option '("disable-authentication") #f #f
  84. (lambda (opt name arg result)
  85. (alist-cons 'authenticate-channels? #f result)))
  86. (option '(#\h "help") #f #f
  87. (lambda args
  88. (show-help)
  89. (exit 0)))
  90. (option '(#\V "version") #f #f
  91. (lambda args
  92. (show-version-and-exit "guix time-machine")))
  93. %standard-build-options))
  94. (define %default-options
  95. ;; Alist of default option values.
  96. `((system . ,(%current-system))
  97. (substitutes? . #t)
  98. (offload? . #t)
  99. (print-build-trace? . #t)
  100. (print-extended-build-trace? . #t)
  101. (multiplexed-build-output? . #t)
  102. (authenticate-channels? . #t)
  103. (graft? . #t)
  104. (debug . 0)
  105. (verbosity . 1)))
  106. (define (parse-args args)
  107. "Parse the list of command line arguments ARGS."
  108. ;; The '--' token is used to separate the command to run from the rest of
  109. ;; the operands.
  110. (let-values (((args command) (break (cut string=? "--" <>) args)))
  111. (let ((opts (parse-command-line args %options
  112. (list %default-options))))
  113. (when (assoc-ref opts 'argument)
  114. (leave (G_ "~A: extraneous argument~%")
  115. (assoc-ref opts 'argument)))
  116. (match command
  117. (() opts)
  118. (("--") opts)
  119. (("--" command ...) (alist-cons 'exec command opts))))))
  120. ;;;
  121. ;;; Entry point.
  122. ;;;
  123. (define-command (guix-time-machine . args)
  124. (synopsis "run commands from a different revision")
  125. (with-error-handling
  126. (with-git-error-handling
  127. (let* ((opts (parse-args args))
  128. (channels (channel-list opts))
  129. (command-line (assoc-ref opts 'exec))
  130. (substitutes? (assoc-ref opts 'substitutes?))
  131. (authenticate? (assoc-ref opts 'authenticate-channels?)))
  132. (when command-line
  133. (let* ((directory
  134. (with-store store
  135. (with-status-verbosity (assoc-ref opts 'verbosity)
  136. (with-build-handler (build-notifier #:use-substitutes?
  137. substitutes?
  138. #:verbosity
  139. (assoc-ref opts 'verbosity)
  140. #:dry-run? #f)
  141. (set-build-options-from-command-line store opts)
  142. (cached-channel-instance store channels
  143. #:authenticate? authenticate?)))))
  144. (executable (string-append directory "/bin/guix")))
  145. (apply execl (cons* executable executable command-line))))))))