summaryrefslogtreecommitdiff
path: root/gnu/home/services/containers.scm
blob: 89d6ad5f39a18bde40d92d21b9852285ee0b65de (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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
;;; GNU Guix --- Functional package management for GNU
;;; Copyright © 2024 Giacomo Leidi <goodoldpaul@autistici.org>
;;;
;;; 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 containers)
  #:use-module (guix gexp)
  #:use-module (guix packages)
  #:use-module (gnu packages docker)
  #:use-module (gnu services configuration)
  #:use-module (gnu home services)
  #:use-module (ice-9 match)
  #:use-module (ice-9 string-fun)
  #:export (docker-cli-configuration
            docker-cli-configuration?
            docker-cli-configuration-fields
            docker-cli-configuration-docker-cli
            docker-cli-configuration-creds-store
            docker-cli-configuration-cli-plugins

            home-docker-cli-service-type
            home-docker-cli-configuration-file
            docker-cli-configuration->json-fields))

;; Turn field names, which are Scheme symbols into strings
;; cli-plugins-extra-dirs -> cliPluginsExtraDirs
(define (format-name name)
  (define without-dashes (string-replace-substring (symbol->string name) "-" " "))
  (define splitted (string-split without-dashes #\space))
  (string-replace-substring
   (apply string-append
          `(,(car splitted)
            ,@(map string-capitalize (cdr splitted))))
   " " ""))

(define (serialize-json-value name value)
  #~(begin
      (use-modules (ice-9 format))
      (format #f "\"~a\": ~a" #$(format-name name) #$value)))

(define (serialize-json-list values)
  #~(string-append "["
               (string-join
                (map (lambda (s) (string-append "\"" s "\""))
                     (list #$@values))
                ", ")
               "]"))

(define (serialize-string field-name value)
  (serialize-json-value field-name (string-append "\"" value "\"")))

(define (serialize-maybe-string field-name value)
  (if (maybe-value-set? value)
      (serialize-string field-name value)
      '()))

(define (serialize-list-of-strings field-name value)
  (serialize-json-value field-name (serialize-json-list value)))

(define (serialize-list-of-docker-cli-plugins value)
  (serialize-json-value 'cli-plugins-extra-dirs
                        (serialize-json-list
                         (map (lambda (p)
                                (file-append p "/libexec/docker/cli-plugins"))
                              value))))

(define list-of-strings?
  (list-of string?))

(define list-of-docker-cli-plugins?
  (list-of package?))

(define-maybe string)

(define-configuration/no-serialization docker-cli-configuration
  (docker-cli
   (package docker-cli)
   "The Docker cli package installed to the Home profile.")
  (creds-store
   (maybe-string)
   "A native secrets store used to store Docker credentials.")
  (cli-plugins
   (list-of-docker-cli-plugins '())
   "A list of Docker cli plugin package records that will be configured to work with Docker's cli.")
  (extra-content
   (string "")
   "Additional literal content that will be appended to Docker cli config.json."))

(define docker-cli-configuration->json-fields
  (lambda (config)
    (filter (compose not (lambda (f) (or (null? f) (and (string? f) (string-null? f)))))
            (map (lambda (f)
                   (let ((field-name (configuration-field-name f))
                         (type (configuration-field-type f))
                         (value ((configuration-field-getter f) config)))
                     (if (not (member field-name '(docker-cli extra-content)))
                         (match type
                           ('string
                            (serialize-string field-name value))
                           ('maybe-string
                            (serialize-maybe-string field-name value))
                           ('list-of-strings
                            (serialize-list-of-strings field-name value))
                           ('list-of-docker-cli-plugins
                            (serialize-list-of-docker-cli-plugins value))
                           (_
                            (raise
                             (formatted-message
                              (G_ "Unknown docker-cli-configuration field type: ~a")
                              type))))
                         '())))
                 docker-cli-configuration-fields))))

(define (home-docker-cli-configuration-file config)
  `((".docker/config.json"
     ,(computed-file "docker-cli-config.json"
       #~(with-output-to-file #$output
           (lambda _
             (display
              (string-append "{"
                             (string-join (list #$@(docker-cli-configuration->json-fields config)) ",")
                             #$(docker-cli-configuration-extra-content config)
                             "}\n"))))))))

(define home-docker-cli-service-type
  (service-type (name 'docker-cli)
                (extensions (list (service-extension home-profile-service-type
                                                     (lambda (config)
                                                       `(,(docker-cli-configuration-docker-cli config)
                                                         ,@(docker-cli-configuration-cli-plugins config))))
                                  (service-extension home-files-service-type
                                                     home-docker-cli-configuration-file)))
                (default-value (docker-cli-configuration))
                (description
                 "This service install and configures Docker's command line interface.")))