home-import.scm 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2021 Xinglu Chen <public@yoctocell.xyz>
  3. ;;; Copyright © 2022 Arjan Adriaanse <arjan@adriaan.se>
  4. ;;;
  5. ;;; This file is part of GNU Guix.
  6. ;;;
  7. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  8. ;;; under the terms of the GNU General Public License as published by
  9. ;;; the Free Software Foundation; either version 3 of the License, or (at
  10. ;;; your option) any later version.
  11. ;;;
  12. ;;; GNU Guix is distributed in the hope that it will be useful, but
  13. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. ;;; GNU General Public License for more details.
  16. ;;;
  17. ;;; You should have received a copy of the GNU General Public License
  18. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  19. (define-module (test-home-import)
  20. #:use-module (guix scripts home import)
  21. #:use-module (guix utils)
  22. #:use-module (guix build utils)
  23. #:use-module (guix packages)
  24. #:use-module (ice-9 match)
  25. #:use-module ((guix profiles) #:hide (manifest->code))
  26. #:use-module ((guix build syscalls) #:select (mkdtemp!))
  27. #:use-module ((guix scripts package)
  28. #:select (manifest-entry-version-prefix))
  29. #:use-module (gnu packages)
  30. #:use-module (srfi srfi-1)
  31. #:use-module (srfi srfi-26)
  32. #:use-module (srfi srfi-64))
  33. ;; Test the (guix scripts home import) tools.
  34. (test-begin "home-import")
  35. ;; Example manifest entries.
  36. (define guile-2.0.9
  37. (manifest-entry
  38. (name "guile")
  39. (version "2.0.9")
  40. (item "/gnu/store/...")))
  41. (define glibc
  42. (manifest-entry
  43. (name "glibc")
  44. (version "2.19")
  45. (item "/gnu/store/...")))
  46. (define gcc
  47. (manifest-entry
  48. (name "gcc")
  49. (version "")
  50. (output "lib")
  51. (item "/gnu/store/...")))
  52. ;; Helpers for checking and generating home environments.
  53. (define %destination-directory "/tmp/guix-config")
  54. (mkdir-p %destination-directory)
  55. (define %temporary-home-directory (mkdtemp! "/tmp/guix-home-import.XXXXXX"))
  56. (define-syntax-rule (define-home-environment-matcher name pattern)
  57. (define (name obj)
  58. (match obj
  59. (pattern #t)
  60. (x (pk 'fail x #f)))))
  61. (define (create-temporary-home files-alist)
  62. "Create a temporary home directory in '%temporary-home-directory'.
  63. FILES-ALIST is an association list of files and the content of the
  64. corresponding file."
  65. (define (create-file file content)
  66. (let ((absolute-path (string-append %temporary-home-directory "/" file)))
  67. (unless (file-exists? absolute-path)
  68. (mkdir-p (dirname absolute-path)))
  69. (call-with-output-file absolute-path
  70. (cut display content <>))))
  71. (for-each (match-lambda
  72. ((file . content) (create-file file content)))
  73. files-alist))
  74. (define (eval-test-with-home-environment files-alist manifest matcher)
  75. (create-temporary-home files-alist)
  76. (setenv "HOME" %temporary-home-directory)
  77. (mkdir-p %temporary-home-directory)
  78. (let* ((home-environment (manifest+configuration-files->code
  79. manifest %destination-directory))
  80. (result (matcher home-environment)))
  81. (delete-file-recursively %temporary-home-directory)
  82. result))
  83. (define-home-environment-matcher match-home-environment-no-services
  84. ('begin
  85. ('use-modules
  86. ('gnu 'home)
  87. ('gnu 'packages)
  88. ('gnu 'services))
  89. ('home-environment
  90. ('packages
  91. ('map ('compose 'list 'specification->package+output)
  92. ('list "guile@2.0.9" "gcc:lib" "glibc@2.19")))
  93. ('services
  94. ('list)))))
  95. (define-home-environment-matcher match-home-environment-transformations
  96. ('begin
  97. ('use-modules
  98. ('gnu 'home)
  99. ('gnu 'packages)
  100. ('gnu 'services)
  101. ('guix 'transformations))
  102. ('define transform ('options->transformation _))
  103. ('home-environment
  104. ('packages
  105. ('list (transform ('specification->package "guile@2.0.9"))
  106. ('list ('specification->package "gcc") "lib")
  107. ('specification->package "glibc@2.19")))
  108. ('services ('list)))))
  109. (define-home-environment-matcher match-home-environment-no-services-nor-packages
  110. ('begin
  111. ('use-modules
  112. ('gnu 'home)
  113. ('gnu 'packages)
  114. ('gnu 'services))
  115. ('home-environment
  116. ('packages
  117. ('map ('compose 'list 'specification->package+output)
  118. ('list)))
  119. ('services
  120. ('list)))))
  121. (define-home-environment-matcher match-home-environment-bash-service
  122. ('begin
  123. ('use-modules
  124. ('gnu 'home)
  125. ('gnu 'packages)
  126. ('gnu 'services)
  127. ('guix 'gexp)
  128. ('gnu 'home 'services 'shells))
  129. ('home-environment
  130. ('packages
  131. ('map ('compose 'list 'specification->package+output)
  132. ('list)))
  133. ('services
  134. ('list ('service
  135. 'home-bash-service-type
  136. ('home-bash-configuration
  137. ('aliases ('quote ()))
  138. ('bashrc
  139. ('list ('local-file "/tmp/guix-config/.bashrc"
  140. "bashrc"))))))))))
  141. (test-assert "manifest->code: No services"
  142. (eval-test-with-home-environment
  143. '()
  144. (make-manifest (list guile-2.0.9 gcc glibc))
  145. match-home-environment-no-services))
  146. (test-assert "manifest->code: No services, package transformations"
  147. (eval-test-with-home-environment
  148. '()
  149. (make-manifest (list (manifest-entry
  150. (inherit guile-2.0.9)
  151. (properties `((transformations
  152. . ((foo . "bar"))))))
  153. gcc glibc))
  154. match-home-environment-transformations))
  155. (test-assert "manifest->code: No packages nor services"
  156. (eval-test-with-home-environment
  157. '()
  158. (make-manifest '())
  159. match-home-environment-no-services-nor-packages))
  160. (test-assert "manifest->code: Bash service"
  161. (eval-test-with-home-environment
  162. '((".bashrc" . "echo 'hello guix'"))
  163. (make-manifest '())
  164. match-home-environment-bash-service))
  165. (test-end "home-import")