1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2021 Xinglu Chen <public@yoctocell.xyz>
3 ;;; Copyright © 2022 Arjan Adriaanse <arjan@adriaan.se>
5 ;;; This file is part of GNU Guix.
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.
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.
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/>.
20 (define-module (test-home-import)
21 #:use-module (guix scripts home import)
22 #:use-module (guix utils)
23 #:use-module (guix build utils)
24 #:use-module (guix packages)
25 #:use-module (ice-9 match)
26 #:use-module ((guix read-print) #:select (blank?))
27 #:use-module ((guix profiles) #:hide (manifest->code))
28 #:use-module ((guix build syscalls) #:select (mkdtemp!))
29 #:use-module ((guix scripts package)
30 #:select (manifest-entry-version-prefix))
31 #:use-module (gnu packages)
32 #:use-module (srfi srfi-1)
33 #:use-module (srfi srfi-26)
34 #:use-module (srfi srfi-64))
36 ;; Test the (guix scripts home import) tools.
38 (test-begin "home-import")
40 ;; Example manifest entries.
46 (item "/gnu/store/...")))
52 (item "/gnu/store/...")))
59 (item "/gnu/store/...")))
61 ;; Helpers for checking and generating home environments.
63 (define %destination-directory "/tmp/guix-config")
64 (mkdir-p %destination-directory)
66 (define %temporary-home-directory (mkdtemp! "/tmp/guix-home-import.XXXXXX"))
68 (define-syntax-rule (define-home-environment-matcher name pattern)
72 (x (pk 'fail x #f)))))
74 (define (create-temporary-home files-alist)
75 "Create a temporary home directory in '%temporary-home-directory'.
76 FILES-ALIST is an association list of files and the content of the
78 (define (create-file file content)
79 (let ((absolute-path (string-append %temporary-home-directory "/" file)))
80 (unless (file-exists? absolute-path)
81 (mkdir-p (dirname absolute-path)))
82 (call-with-output-file absolute-path
83 (cut display content <>))))
85 (for-each (match-lambda
86 ((file . content) (create-file file content)))
89 (define (remove-recursively pred sexp)
90 "Like SRFI-1 'remove', but recurse within SEXP."
91 (let loop ((sexp sexp))
94 (map loop (remove pred lst)))
97 (define (eval-test-with-home-environment files-alist manifest matcher)
98 (create-temporary-home files-alist)
99 (setenv "HOME" %temporary-home-directory)
100 (mkdir-p %temporary-home-directory)
101 (let* ((home-environment (manifest+configuration-files->code
102 manifest %destination-directory))
103 (result (matcher (remove-recursively blank? home-environment))))
104 (delete-file-recursively %temporary-home-directory)
107 (define-home-environment-matcher match-home-environment-no-services
115 ('specifications->packages
116 ('list "guile@2.0.9" "gcc:lib" "glibc@2.19")))
120 (define-home-environment-matcher match-home-environment-transformations
126 ('guix 'transformations))
128 ('define transform ('options->transformation _))
131 ('list (transform ('specification->package "guile@2.0.9"))
132 ('list ('specification->package "gcc") "lib")
133 ('specification->package "glibc@2.19")))
134 ('services ('list)))))
136 (define-home-environment-matcher match-home-environment-no-services-nor-packages
144 ('specifications->packages ('list)))
148 (define-home-environment-matcher match-home-environment-bash-service
155 ('gnu 'home 'services 'shells))
158 ('specifications->packages ('list)))
161 'home-bash-service-type
162 ('home-bash-configuration
163 ('aliases ('quote ()))
165 ('list ('local-file "/tmp/guix-config/.bashrc"
168 (define-home-environment-matcher match-home-environment-bash-service-with-alias
175 ('gnu 'home 'services 'shells))
178 ('specifications->packages ('list)))
181 'home-bash-service-type
182 ('home-bash-configuration
184 ('quote (("grep" . "grep --exclude-from=\"$HOME/.grep-exclude\"")
187 ('list ('local-file "/tmp/guix-config/.bashrc"
191 (test-assert "manifest->code: No services"
192 (eval-test-with-home-environment
194 (make-manifest (list guile-2.0.9 gcc glibc))
195 match-home-environment-no-services))
197 (test-assert "manifest->code: No services, package transformations"
198 (eval-test-with-home-environment
200 (make-manifest (list (manifest-entry
201 (inherit guile-2.0.9)
202 (properties `((transformations
203 . ((foo . "bar"))))))
205 match-home-environment-transformations))
207 (test-assert "manifest->code: No packages nor services"
208 (eval-test-with-home-environment
211 match-home-environment-no-services-nor-packages))
213 (test-assert "manifest->code: Bash service"
214 (eval-test-with-home-environment
215 '((".bashrc" . "echo 'hello guix'"))
217 match-home-environment-bash-service))
219 (test-assert "manifest->code: Bash service with aliases"
220 (eval-test-with-home-environment
223 alias ls=\"ls -p\"; alias grep='grep --exclude-from=\"$HOME/.grep-exclude\"'\n"))
225 match-home-environment-bash-service-with-alias))
227 (test-end "home-import")