summaryrefslogtreecommitdiff
path: root/modules/ryan-config/utils.scm
blob: e889e694ba9667fb186f29cbd74f0b26f58b7210 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
(define-module (ryan-config utils)
  #:use-module (gnu packages)
  #:use-module (gnu services)
  #:use-module (gnu home services)
  #:use-module (gnu home services shepherd)
  #:use-module (gnu packages freedesktop)
  #:use-module (gnu services configuration)
  #:use-module (guix gexp)
  #:use-module (ice-9 regex)
  #:use-module (ice-9 pretty-print)
  #:use-module (ice-9 textual-ports)

  #:export (gather-manifest-packages
            apply-template-file))

(define (apply-template template-string value-alist)
  (regexp-substitute/global #f
                            "\\$\\{([A-Za-z/\\-]+)\\}"
                            template-string
                            'pre
                            (lambda (m)
                              (let ((entry (assq (string->symbol (match:substring m 1))
                                                 value-alist)))
                                (if entry
                                    (cdr entry)
                                    "VALUE NOT FOUND")))
                            'post))

(define (apply-template-file file-path value-alist)
  (call-with-input-file file-path
    (lambda (port)
      (apply-template (get-string-all port)
                      value-alist))))