time-machine.scm 5.5 KB

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