home-import.scm 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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 read-print) #:select (blank?))
  26. #:use-module ((guix profiles) #:hide (manifest->code))
  27. #:use-module ((guix build syscalls) #:select (mkdtemp!))
  28. #:use-module ((guix scripts package)
  29. #:select (manifest-entry-version-prefix))
  30. #:use-module (gnu packages)
  31. #:use-module (srfi srfi-1)
  32. #:use-module (srfi srfi-26)
  33. #:use-module (srfi srfi-64))
  34. ;; Test the (guix scripts home import) tools.
  35. (test-begin "home-import")
  36. ;; Example manifest entries.
  37. (define guile-2.0.9
  38. (manifest-entry
  39. (name "guile")
  40. (version "2.0.9")
  41. (item "/gnu/store/...")))
  42. (define glibc
  43. (manifest-entry
  44. (name "glibc")
  45. (version "2.19")
  46. (item "/gnu/store/...")))
  47. (define gcc
  48. (manifest-entry
  49. (name "gcc")
  50. (version "")
  51. (output "lib")
  52. (item "/gnu/store/...")))
  53. ;; Helpers for checking and generating home environments.
  54. (define %destination-directory "/tmp/guix-config")
  55. (mkdir-p %destination-directory)
  56. (define %temporary-home-directory (mkdtemp! "/tmp/guix-home-import.XXXXXX"))
  57. (define-syntax-rule (define-home-environment-matcher name pattern)
  58. (define (name obj)
  59. (match obj
  60. (pattern #t)
  61. (x (pk 'fail x #f)))))
  62. (define (create-temporary-home files-alist)
  63. "Create a temporary home directory in '%temporary-home-directory'.
  64. FILES-ALIST is an association list of files and the content of the
  65. corresponding file."
  66. (define (create-file file content)
  67. (let ((absolute-path (string-append %temporary-home-directory "/" file)))
  68. (unless (file-exists? absolute-path)
  69. (mkdir-p (dirname absolute-path)))
  70. (call-with-output-file absolute-path
  71. (cut display content <>))))
  72. (for-each (match-lambda
  73. ((file . content) (create-file file content)))
  74. files-alist))
  75. (define (remove-recursively pred sexp)
  76. "Like SRFI-1 'remove', but recurse within SEXP."
  77. (let loop ((sexp sexp))
  78. (match sexp
  79. ((lst ...)
  80. (map loop (remove pred lst)))
  81. (x x))))
  82. (define (eval-test-with-home-environment files-alist manifest matcher)
  83. (create-temporary-home files-alist)
  84. (setenv "HOME" %temporary-home-directory)
  85. (mkdir-p %temporary-home-directory)
  86. (let* ((home-environment (manifest+configuration-files->code
  87. manifest %destination-directory))
  88. (result (matcher (remove-recursively blank? home-environment))))
  89. (delete-file-recursively %temporary-home-directory)
  90. result))
  91. (define-home-environment-matcher match-home-environment-no-services
  92. ('begin
  93. ('use-modules
  94. ('gnu 'home)
  95. ('gnu 'packages)
  96. ('gnu 'services))
  97. ('home-environment
  98. ('packages
  99. ('specifications->packages
  100. ('list "guile@2.0.9" "gcc:lib" "glibc@2.19")))
  101. ('services
  102. ('list)))))
  103. (define-home-environment-matcher match-home-environment-transformations
  104. ('begin
  105. ('use-modules
  106. ('gnu 'home)
  107. ('gnu 'packages)
  108. ('gnu 'services)
  109. ('guix 'transformations))
  110. ('define transform ('options->transformation _))
  111. ('home-environment
  112. ('packages
  113. ('list (transform ('specification->package "guile@2.0.9"))
  114. ('list ('specification->package "gcc") "lib")
  115. ('specification->package "glibc@2.19")))
  116. ('services ('list)))))
  117. (define-home-environment-matcher match-home-environment-no-services-nor-packages
  118. ('begin
  119. ('use-modules
  120. ('gnu 'home)
  121. ('gnu 'packages)
  122. ('gnu 'services))
  123. ('home-environment
  124. ('packages
  125. ('specifications->packages ('list)))
  126. ('services
  127. ('list)))))
  128. (define-home-environment-matcher match-home-environment-bash-service
  129. ('begin
  130. ('use-modules
  131. ('gnu 'home)
  132. ('gnu 'packages)
  133. ('gnu 'services)
  134. ('guix 'gexp)
  135. ('gnu 'home 'services 'shells))
  136. ('home-environment
  137. ('packages
  138. ('specifications->packages ('list)))
  139. ('services
  140. ('list ('service
  141. 'home-bash-service-type
  142. ('home-bash-configuration
  143. ('aliases ('quote ()))
  144. ('bashrc
  145. ('list ('local-file "/tmp/guix-config/.bashrc"
  146. "bashrc"))))))))))
  147. (define-home-environment-matcher match-home-environment-bash-service-with-alias
  148. ('begin
  149. ('use-modules
  150. ('gnu 'home)
  151. ('gnu 'packages)
  152. ('gnu 'services)
  153. ('guix 'gexp)
  154. ('gnu 'home 'services 'shells))
  155. ('home-environment
  156. ('packages
  157. ('specifications->packages ('list)))
  158. ('services
  159. ('list ('service
  160. 'home-bash-service-type
  161. ('home-bash-configuration
  162. ('aliases
  163. ('quote (("grep" . "grep --exclude-from=\"$HOME/.grep-exclude\"")
  164. ("ls" . "ls -p"))))
  165. ('bashrc
  166. ('list ('local-file "/tmp/guix-config/.bashrc"
  167. "bashrc"))))))))))
  168. (test-assert "manifest->code: No services"
  169. (eval-test-with-home-environment
  170. '()
  171. (make-manifest (list guile-2.0.9 gcc glibc))
  172. match-home-environment-no-services))
  173. (test-assert "manifest->code: No services, package transformations"
  174. (eval-test-with-home-environment
  175. '()
  176. (make-manifest (list (manifest-entry
  177. (inherit guile-2.0.9)
  178. (properties `((transformations
  179. . ((foo . "bar"))))))
  180. gcc glibc))
  181. match-home-environment-transformations))
  182. (test-assert "manifest->code: No packages nor services"
  183. (eval-test-with-home-environment
  184. '()
  185. (make-manifest '())
  186. match-home-environment-no-services-nor-packages))
  187. (test-assert "manifest->code: Bash service"
  188. (eval-test-with-home-environment
  189. '((".bashrc" . "echo 'hello guix'"))
  190. (make-manifest '())
  191. match-home-environment-bash-service))
  192. (test-assert "manifest->code: Bash service with aliases"
  193. (eval-test-with-home-environment
  194. '((".bashrc"
  195. . "# Aliases
  196. alias ls=\"ls -p\"; alias grep='grep --exclude-from=\"$HOME/.grep-exclude\"'\n"))
  197. (make-manifest '())
  198. match-home-environment-bash-service-with-alias))
  199. (test-end "home-import")