all messages for Guix-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
blob f78751b10f296e13c730171498828d20877d4ad5 7085 bytes (raw)
name: gnu/home/services/state.scm 	 # note: path name is non-authoritative(*)

  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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
 
(define-module (gnu home services state)
  #:use-module (srfi srfi-1)
  #:use-module (ice-9 match)
  #:use-module (gnu home services)
  #:use-module (gnu home services utils)
  #:use-module (gnu home services shepherd)
  #:use-module (gnu home services version-control)
  #:use-module (gnu packages rsync)
  #:use-module (gnu packages version-control)
  #:use-module (gnu services shepherd)
  #:use-module (gnu services configuration)
  #:use-module (gnu packages ssh)
  #:use-module (guix packages)
  #:use-module (guix gexp)
  #:use-module (guix monads)
  #:use-module (guix modules)
  #:use-module (guix records)

  #:export (home-state-service-type
            state-generic
            state-git
            state-hg
	    state-rsync))

(define* (state-hg path remote #:key (config #f))
  (state-generic
   path
   #:init-gexp
   #~(lambda* (_ self)
       (let* ((meta (car (action self 'metadata)))
              (path (assoc-ref meta 'path))
              (remote (assoc-ref meta 'remote)))
         (format #t "Initializing ~a.\n" self)
         (let* ((port ((@@ (guix build utils) open-pipe-with-stderr)
                       #$(file-append mercurial "/bin/hg") "clone" remote path)))
           (waitpid WAIT_ANY)
           (display ((@@ (ice-9 rdelim) read-delimited) "" port))
           (close-port port))

         (when '#$config
           (call-with-output-file (string-append path "/.hg/hgrc")
             (lambda (port) (display (string-append
                                      #$@(serialize-hg-config config)) port))))))
   #:additional-metadata `((remote . ,remote)
                           (general-sync? . #f))))

(define* (state-git path remote #:key (config #f))
  (state-generic
   path
   #:init-gexp
   #~(lambda* (_ self)
       (let* ((meta (car (action self 'metadata)))
	      (path (assoc-ref meta 'path))
	      (remote (assoc-ref meta 'remote)))
	 (format #t "Initializing ~a.\n" self)
	 ;; TODO: revisit git clone implementation
	 ;; FIXME: Hang up shepherd if username/password asked
	 (let* ((port ((@@ (guix build utils) open-pipe-with-stderr)
		       #$(file-append git "/bin/git") "clone" remote path)))
	   (waitpid WAIT_ANY)
	   (display ((@@ (ice-9 rdelim) read-delimited) "" port))
	   (close-port port))

	 (when #$config
	   (call-with-output-file (string-append path "/.git/config")
	     (lambda (port) (display #$config port))))))
   #:additional-metadata `((remote . ,remote)
			   (general-sync? . #f))))

(define* (state-rsync path remote)
  (state-generic
   path
   #:init-gexp
   #~(lambda* (_ self)
       (let* ((meta (car (action self 'metadata)))
	      (path (assoc-ref meta 'path))
	      (remote (assoc-ref meta 'remote)))
	 (format #t "Initializing ~a.\n" self)
	 ;; TODO: revisit git clone implementation
	 (let* ((port ((@@ (guix build utils) open-pipe-with-stderr)
		       #$(file-append rsync "/bin/rsync") "-aP" remote path)))
	   (waitpid WAIT_ANY)
	   (display ((@@ (ice-9 rdelim) read-delimited) "" port))
	   (close-port port))))
   #:sync-gexp
   #~(lambda* (_ self)
       (let* ((meta (car (action self 'metadata)))
	      (path (assoc-ref meta 'path))
	      (remote (assoc-ref meta 'remote)))
	 (format #t "Synchronizing ~a.\n" self)
	 (let* ((port ((@@ (guix build utils) open-pipe-with-stderr)
		       #$(file-append rsync "/bin/rsync") "-aP" path remote)))
	   (waitpid WAIT_ANY)
	   (display ((@@ (ice-9 rdelim) read-delimited) "" port))
	   (close-port port))))
   #:additional-metadata `((remote . ,remote)
			   (general-sync? . #t))))

(define* (state-generic
	  path
	  #:key
	  (init-gexp
	   #~(lambda* (_ self)
	       (let ((path (assoc-ref (car (action self 'metadata)) 'path)))
		 (format #t "Initializing ~a.\n" self)
		 (format #t "Creating ~a directory..." path)
		 (mkdir-p path)
		 (display " done\n"))))
	  (sync-gexp
	   #~(lambda* (_ self)
	       (let ((path (assoc-ref (car (action self 'metadata)) 'path)))
		 (format #t "Synchronizing ~a.\n" self)
		 (format #t "Nothing to synchronize.\n"))))
	  (additional-metadata '((general-sync? . #f))))
  "A function which returns a shepherd-service with all required
actions for state management, should be used as a basis for other
state related items like git-state, rsync-state, etc."
  (let ((self (string->symbol
	       (format #f "state-~a" path))))
    (shepherd-service
     (documentation (format #f "Managing state at ~a." path))
     (provision (list self))
     (auto-start? #f)
     (start #~(lambda ()
		(if (car (action '#$self 'state-exists?))
		    #t
		    (begin
		      (format #t "~a is not initilized yet." '#$self)
		      #f))))
     (actions (list
	       (shepherd-action
		(name 'state-exists?)
		(documentation "Check if state file/directory exists.")
		(procedure #~(lambda* (#:rest rest)
			       (file-exists? #$path))))
	       (shepherd-action
		(name 'unchecked-init)
		(documentation "Do not use this action directly.")
		(procedure init-gexp))
	       (shepherd-action
		(name 'metadata)
		(documentation "Returns metadata related to the state.")
		(procedure #~(lambda* _
			       (append
				'((path . #$path)
				  (self . #$self))
				'#$additional-metadata))))
	       (shepherd-action
		(name 'sync)
		(documentation "Sync the state.")
		(procedure sync-gexp))
	       (shepherd-action
		(name 'init)
		(documentation "Generic initialize.")
		(procedure #~(lambda* (#:rest rest)
			       (if (car (action '#$self 'state-exists?))
				   (format #t "~a already initialized.\n" '#$self)
				   (begin
				     (action '#$self 'unchecked-init '#$self)
				     (start '#$self)))))))))))

(define (add-shepherd-services services)
  (let* ((service-names
	  (map
	   (lambda (service) (car (shepherd-service-provision service)))
	   services)))
    (append
     services
     (list
      (shepherd-service
       (documentation "Init, update and maybe destroy state.")
       (provision '(state))
       (auto-start? #t)
       (start #~(lambda ()
		  (map (lambda (name)
			 (when (car (action name 'state-exists?))
			   (start name)))
		       '#$service-names)))
       (actions (list
		 (shepherd-action
		  (name 'sync)
		  (documentation
                   "Sync all the state. Highly dependent on state type.")
		  (procedure
		   #~(lambda _
		       (map (lambda (name)
			      (when (assoc-ref (car (action name 'metadata))
					       'general-sync?)
				(action name 'sync name)))
			    '#$service-names))))
		 (shepherd-action
		  (name 'init)
		  (documentation "Initialize all the state.")
		  (procedure #~(lambda _
				 (map (lambda (name)
					(when (not (car (action name 'state-exists?)))
					  (action name 'init)
					  (start name)))
				      '#$service-names)))))))))))

(define home-state-service-type
  (service-type (name 'home-state)
                (extensions
                 (list (service-extension
                        home-shepherd-service-type
                        add-shepherd-services)))
                (default-value '())
		(compose concatenate)
		(extend append)
                (description "A toolset for initializing state.")))

debug log:

solving f78751b10f ...
found f78751b10f in https://yhetil.org/guix/20211023180654.3760-1-go.wigust@gmail.com/

applying [1/1] https://yhetil.org/guix/20211023180654.3760-1-go.wigust@gmail.com/
diff --git a/gnu/home/services/state.scm b/gnu/home/services/state.scm
new file mode 100644
index 0000000000..f78751b10f

Checking patch gnu/home/services/state.scm...
Applied patch gnu/home/services/state.scm cleanly.

index at:
100644 f78751b10f296e13c730171498828d20877d4ad5	gnu/home/services/state.scm

(*) Git path names are given by the tree(s) the blob belongs to.
    Blobs themselves have no identifier aside from the hash of its contents.^

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/guix.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.