home: services: configuration: Support file-like objects.

* gnu/home/services/configuration.scm (interpose): Operate only with file-like
objects.
(string-or-gexp?): Delete procedure.
(serialize-string-or-gexp): Rename to 'serialize-file-like'.
(text-config?): Call 'file-like' intead of 'string-or-gexp?'.
* guix/scripts/home/import.scm:
(generate-bash-module+configuration): Don't call slurp-file-gexp.
* gnu/home/services/configuration.scm: Move content ...
* gnu/services/configuration.scm: here.
* gnu/home/services/shells.scm: Delete (gnu home services configuration).
* gnu/home/services/xdg.scm: Same.
* gnu/local.mk: Same.
* tests/guix-home.sh:
Test home-bash-service-type and extension with home-bash-extension.
This commit is contained in:
Oleg Pykhalov 2021-10-02 19:05:02 +03:00
parent 5e68f7a7b3
commit fee0bced7f
No known key found for this signature in database
GPG key ID: 167F8EA5001AFA9C
7 changed files with 117 additions and 120 deletions

View file

@ -1,109 +0,0 @@
;;; GNU Guix --- Functional package management for GNU
;;; Copyright © 2021 Andrew Tropin <andrew@trop.in>
;;; Copyright © 2021 Xinglu Chen <public@yoctocell.xyz>
;;;
;;; This file is part of GNU Guix.
;;;
;;; GNU Guix is free software; you can redistribute it and/or modify it
;;; under the terms of the GNU General Public License as published by
;;; the Free Software Foundation; either version 3 of the License, or (at
;;; your option) any later version.
;;;
;;; GNU Guix is distributed in the hope that it will be useful, but
;;; WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;;; GNU General Public License for more details.
;;;
;;; You should have received a copy of the GNU General Public License
;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
(define-module (gnu home services configuration)
#:use-module (gnu services configuration)
#:use-module (guix gexp)
#:use-module (srfi srfi-1)
#:use-module (ice-9 curried-definitions)
#:use-module (ice-9 match)
#:use-module (guix i18n)
#:use-module (guix diagnostics)
#:export (filter-configuration-fields
interpose
list-of
list-of-strings?
alist?
string-or-gexp?
serialize-string-or-gexp
text-config?
serialize-text-config
generic-serialize-alist-entry
generic-serialize-alist))
(define* (filter-configuration-fields configuration-fields fields
#:optional negate?)
"Retrieve the fields listed in FIELDS from CONFIGURATION-FIELDS.
If NEGATE? is @code{#t}, retrieve all fields except FIELDS."
(filter (lambda (field)
(let ((member? (member (configuration-field-name field) fields)))
(if (not negate?) member? (not member?))))
configuration-fields))
(define* (interpose ls #:optional (delimiter "\n") (grammar 'infix))
"Same as @code{string-join}, but without join and string, returns an
DELIMITER interposed LS. Support 'infix and 'suffix GRAMMAR values."
(when (not (member grammar '(infix suffix)))
(raise
(formatted-message
(G_ "The GRAMMAR value must be 'infix or 'suffix, but ~a provided.")
grammar)))
(fold-right (lambda (e acc)
(cons e
(if (and (null? acc) (eq? grammar 'infix))
acc
(cons delimiter acc))))
'() ls))
(define (list-of pred?)
"Return a procedure that takes a list and check if all the elements of
the list result in @code{#t} when applying PRED? on them."
(lambda (x)
(if (list? x)
(every pred? x)
#f)))
(define list-of-strings?
(list-of string?))
(define alist? list?)
(define (string-or-gexp? sg) (or (string? sg) (gexp? sg)))
(define (serialize-string-or-gexp field-name val) "")
(define (text-config? config)
(and (list? config) (every string-or-gexp? config)))
(define (serialize-text-config field-name val)
#~(string-append #$@(interpose val "\n" 'suffix)))
(define ((generic-serialize-alist-entry serialize-field) entry)
"Apply the SERIALIZE-FIELD procedure on the field and value of ENTRY."
(match entry
((field . val) (serialize-field field val))))
(define (generic-serialize-alist combine serialize-field fields)
"Generate a configuration from an association list FIELDS.
SERIALIZE-FIELD is a procedure that takes two arguments, it will be
applied on the fields and values of FIELDS using the
@code{generic-serialize-alist-entry} procedure.
COMBINE is a procedure that takes one or more arguments and combines
all the alist entries into one value, @code{string-append} or
@code{append} are usually good candidates for this.
See the @code{serialize-alist} procedure in `@code{(gnu home-services
version-control}' for an example usage.)}"
(apply combine
(map (generic-serialize-alist-entry serialize-field) fields)))

View file

@ -19,7 +19,6 @@
(define-module (gnu home services shells) (define-module (gnu home services shells)
#:use-module (gnu services configuration) #:use-module (gnu services configuration)
#:use-module (gnu home services configuration)
#:use-module (gnu home services utils) #:use-module (gnu home services utils)
#:use-module (gnu home services) #:use-module (gnu home services)
#:use-module (gnu packages shells) #:use-module (gnu packages shells)

View file

@ -19,7 +19,6 @@
(define-module (gnu home services xdg) (define-module (gnu home services xdg)
#:use-module (gnu services configuration) #:use-module (gnu services configuration)
#:use-module (gnu home services configuration)
#:use-module (gnu home services) #:use-module (gnu home services)
#:use-module (gnu packages freedesktop) #:use-module (gnu packages freedesktop)
#:use-module (gnu home services utils) #:use-module (gnu home services utils)

View file

@ -77,7 +77,6 @@ GNU_SYSTEM_MODULES = \
%D%/home/services.scm \ %D%/home/services.scm \
%D%/home/services/symlink-manager.scm \ %D%/home/services/symlink-manager.scm \
%D%/home/services/fontutils.scm \ %D%/home/services/fontutils.scm \
%D%/home/services/configuration.scm \
%D%/home/services/shells.scm \ %D%/home/services/shells.scm \
%D%/home/services/shepherd.scm \ %D%/home/services/shepherd.scm \
%D%/home/services/mcron.scm \ %D%/home/services/mcron.scm \

View file

@ -4,6 +4,7 @@
;;; Copyright © 2017, 2018 Clément Lassieur <clement@lassieur.org> ;;; Copyright © 2017, 2018 Clément Lassieur <clement@lassieur.org>
;;; Copyright © 2021 Xinglu Chen <public@yoctocell.xyz> ;;; Copyright © 2021 Xinglu Chen <public@yoctocell.xyz>
;;; Copyright © 2021 Maxim Cournoyer <maxim.cournoyer@gmail.com> ;;; Copyright © 2021 Maxim Cournoyer <maxim.cournoyer@gmail.com>
;;; Copyright © 2021 Andrew Tropin <andrew@trop.in>
;;; ;;;
;;; This file is part of GNU Guix. ;;; This file is part of GNU Guix.
;;; ;;;
@ -25,10 +26,12 @@ (define-module (gnu services configuration)
#:use-module (guix records) #:use-module (guix records)
#:use-module (guix gexp) #:use-module (guix gexp)
#:use-module ((guix utils) #:select (source-properties->location)) #:use-module ((guix utils) #:select (source-properties->location))
#:use-module ((guix diagnostics) #:select (location-file)) #:use-module ((guix diagnostics) #:select (formatted-message location-file))
#:use-module ((guix modules) #:select (file-name->module-name)) #:use-module ((guix modules) #:select (file-name->module-name))
#:use-module (guix i18n)
#:autoload (texinfo) (texi-fragment->stexi) #:autoload (texinfo) (texi-fragment->stexi)
#:autoload (texinfo serialize) (stexi->texi) #:autoload (texinfo serialize) (stexi->texi)
#:use-module (ice-9 curried-definitions)
#:use-module (ice-9 match) #:use-module (ice-9 match)
#:use-module (srfi srfi-1) #:use-module (srfi srfi-1)
#:use-module (srfi srfi-34) #:use-module (srfi srfi-34)
@ -56,7 +59,20 @@ (define-module (gnu services configuration)
generate-documentation generate-documentation
configuration->documentation configuration->documentation
empty-serializer empty-serializer
serialize-package)) serialize-package
filter-configuration-fields
interpose
list-of
list-of-strings?
alist?
serialize-file-like
text-config?
serialize-text-config
generic-serialize-alist-entry
generic-serialize-alist))
;;; Commentary: ;;; Commentary:
;;; ;;;
@ -323,3 +339,73 @@ (define (configuration->documentation configuration-symbol)
'-fields)))) '-fields))))
(display (generate-documentation `((,configuration-symbol ,fields-getter)) (display (generate-documentation `((,configuration-symbol ,fields-getter))
configuration-symbol)))) configuration-symbol))))
(define* (filter-configuration-fields configuration-fields fields
#:optional negate?)
"Retrieve the fields listed in FIELDS from CONFIGURATION-FIELDS.
If NEGATE? is @code{#t}, retrieve all fields except FIELDS."
(filter (lambda (field)
(let ((member? (member (configuration-field-name field) fields)))
(if (not negate?) member? (not member?))))
configuration-fields))
(define* (interpose ls #:optional (delimiter "\n") (grammar 'infix))
"Same as @code{string-join}, but without join and string, returns an
DELIMITER interposed LS. Support 'infix and 'suffix GRAMMAR values."
(when (not (member grammar '(infix suffix)))
(raise
(formatted-message
(G_ "The GRAMMAR value must be 'infix or 'suffix, but ~a provided.")
grammar)))
(fold-right (lambda (e acc)
(cons #~(begin
(use-modules (ice-9 rdelim))
(with-fluids ((%default-port-encoding "UTF-8"))
(with-input-from-file #$e read-string)))
(if (and (null? acc) (eq? grammar 'infix))
acc
(cons delimiter acc))))
'() ls))
(define (list-of pred?)
"Return a procedure that takes a list and check if all the elements of
the list result in @code{#t} when applying PRED? on them."
(lambda (x)
(if (list? x)
(every pred? x)
#f)))
(define list-of-strings?
(list-of string?))
(define alist? list?)
(define serialize-file-like empty-serializer)
(define (text-config? config)
(list-of file-like?))
(define (serialize-text-config field-name val)
#~(string-append #$@(interpose val "\n" 'suffix)))
(define ((generic-serialize-alist-entry serialize-field) entry)
"Apply the SERIALIZE-FIELD procedure on the field and value of ENTRY."
(match entry
((field . val) (serialize-field field val))))
(define (generic-serialize-alist combine serialize-field fields)
"Generate a configuration from an association list FIELDS.
SERIALIZE-FIELD is a procedure that takes two arguments, it will be
applied on the fields and values of FIELDS using the
@code{generic-serialize-alist-entry} procedure.
COMBINE is a procedure that takes one or more arguments and combines
all the alist entries into one value, @code{string-append} or
@code{append} are usually good candidates for this.
See the @code{serialize-alist} procedure in `@code{(gnu home services
version-control}' for an example usage.)}"
(apply combine
(map (generic-serialize-alist-entry serialize-field) fields)))

View file

@ -46,17 +46,15 @@ (define (generate-bash-module+configuration)
(home-bash-configuration (home-bash-configuration
,@(if (file-exists? rc) ,@(if (file-exists? rc)
`((bashrc `((bashrc
(list (slurp-file-gexp (local-file ,rc))))) (list (local-file ,rc))))
'()) '())
,@(if (file-exists? profile) ,@(if (file-exists? profile)
`((bash-profile `((bash-profile
(list (slurp-file-gexp (list (local-file ,profile))))
(local-file ,profile)))))
'()) '())
,@(if (file-exists? logout) ,@(if (file-exists? logout)
`((bash-logout `((bash-logout
(list (slurp-file-gexp (list (local-file ,logout))))
(local-file ,logout)))))
'())))))) '()))))))

View file

@ -54,10 +54,13 @@ trap 'chmod -Rf +w "$test_directory"; rm -rf "$test_directory"' EXIT
# Test 'guix home reconfigure'. # Test 'guix home reconfigure'.
# #
printf "# dot-bashrc test file for guix home" > "dot-bashrc"
cat > "home.scm" <<'EOF' cat > "home.scm" <<'EOF'
(use-modules (guix gexp) (use-modules (guix gexp)
(gnu home) (gnu home)
(gnu home services) (gnu home services)
(gnu home services shells)
(gnu services)) (gnu services))
(home-environment (home-environment
@ -68,11 +71,33 @@ trap 'chmod -Rf +w "$test_directory"; rm -rf "$test_directory"' EXIT
(list `("config/test.conf" (list `("config/test.conf"
,(plain-file ,(plain-file
"tmp-file.txt" "tmp-file.txt"
"the content of ~/.config/test.conf"))))))) "the content of ~/.config/test.conf"))))
(service home-bash-service-type
(home-bash-configuration
(guix-defaults? #t)
(bashrc
(list
(local-file (string-append (dirname (current-filename))
"/dot-bashrc"))))))
(simple-service 'home-bash-service-extension-test
home-bash-service-type
(home-bash-extension
(bashrc
(list
(plain-file
"bashrc-test-config.sh"
"# the content of bashrc-test-config.sh"))))))))
EOF EOF
guix home reconfigure "${test_directory}/home.scm" guix home reconfigure "${test_directory}/home.scm"
test -d "${HOME}/.guix-home" test -d "${HOME}/.guix-home"
test -h "${HOME}/.bash_profile"
test -h "${HOME}/.bashrc"
test "$(tail -n 2 "${HOME}/.bashrc")" == "\
# dot-bashrc test file for guix home
# the content of bashrc-test-config.sh"
grep -q "the content of ~/.config/test.conf" "${HOME}/.config/test.conf" grep -q "the content of ~/.config/test.conf" "${HOME}/.config/test.conf"
# #