summary refs log tree commit diff
path: root/gnu/services/potluck.scm
blob: cbabaa5acdbf7a69718006ccf420d8618e67b271 (plain) (blame)
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
;;; GNU Guix --- Functional package management for GNU
;;; Copyright © 2017 Andy Wingo <wingo@igalia.com>
;;;
;;; 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 services potluck)
  #:use-module (gnu services)
  #:use-module (gnu services shepherd)
  #:use-module (gnu system shadow)
  #:use-module (gnu packages admin)
  #:use-module (gnu packages package-management)
  #:use-module (gnu packages version-control)
  #:use-module (guix records)
  #:use-module (guix gexp)
  #:use-module (srfi srfi-1)
  #:use-module (ice-9 match)
  #:export (potluck-configuration
            potluck-configuration?
            potluck-service-type))

;;; Commentary:
;;;
;;; Guix potluck services.
;;;
;;; Code:

;; FIXME: Replace with guix version that has potluck.
(define guix/potluck guix)

(define-record-type* <potluck-configuration> potluck-configuration
  make-potluck-configuration
  potluck-configuration?
  (package       potluck-configuration-package ;<package>
                 (default guix/potluck))
  (scratch       potluck-configuration-scratch
                 (default "/var/cache/potluck/scratch"))
  (source        potluck-configuration-source
                 (default "/var/cache/potluck/source"))
  (source-repo   potluck-configuration-source-repo
                 (default "/srv/git/source.git"))
  (target        potluck-configuration-target
                 (default "/var/cache/potluck/target"))
  (target-repo   potluck-configuration-target-repo
                 (default "/srv/git/target.git"))
  (user          potluck-configuration-user
                 (default "potluck"))
  (group         potluck-configuration-group
                 (default "potluck")))

(define potluck-accounts
  (match-lambda
    (($ <potluck-configuration>
        package scratch source source-repo target target-repo user group)
     (filter identity
             (list
              (and (equal? group "potluck")
                   (user-group
                    (name "potluck")
                    (system? #t)))
              (and (equal? user "potluck")
                   (user-account
                    (name "potluck")
                    (group group)
                    (system? #t)
                    (comment "Potluck Service")
                    (home-directory "/var/empty")
                    (shell (file-append shadow "/sbin/nologin")))))))))

(define potluck-activation-service
  (match-lambda
    (($ <potluck-configuration>
        package scratch source source-repo target target-repo user group)
     (with-imported-modules '((guix build utils))
       #~(begin
	   (use-modules (guix build utils) (ice-9 ftw))
	   (define (chown-r dir user group)
             (let ((uid (passwd:uid (getpwnam user)))
		   (gid (group:gid (getgrnam group))))
	       (for-each (lambda (f) (chown f uid gid))
			 (find-files dir #:directories? #t))))
	   (define (ensure-git-repo dir)
	     (unless (file-exists? dir)
	       (mkdir-p dir)
	       (unless (zero? (system* (string-append #$git "/bin/git")
				       "init" "--bare" dir))
		 (error "failed to create repository" dir))
	       (chown-r dir #$user #$group)))
	   (define (ensure-checkout repo dir)
	     (unless (file-exists? dir)
	       (mkdir-p (dirname dir))
	       (unless (zero? (system* (string-append #$git "/bin/git")
				       "clone" repo dir))
	         (error "failed to check out repository" repo dir))
	       (chown-r dir #$user #$group)))
	   (mkdir-p #$scratch)
	   (chown-r #$scratch #$user #$group)
	   (ensure-git-repo #$source-repo)
	   (ensure-git-repo #$target-repo)
	   (ensure-checkout #$source-repo #$source)
	   (ensure-checkout #$target-repo #$target))))))

(define potluck-shepherd-service
  (match-lambda
    (($ <potluck-configuration>
        package scratch source source-repo target target-repo user group)
     (list (shepherd-service
            (provision '(potluck))
            (documentation "Run the potluck daemon.")
            (requirement '(networking))

            (start #~(make-forkexec-constructor
                      '(#$(file-append package "/bin/guix") "host-channel"
                        #$(format #f "--scratch=~a" scratch)
			#$(format #f "--source=~a" source)
			#$(format #f "--target=~a" target))
		      #:user #$user #:group #$group))
            (stop #~(make-kill-destructor)))))))

(define potluck-service-type
  (service-type (name 'potluck)
                (extensions
                 (list (service-extension activation-service-type
                                          potluck-activation-service)
		       (service-extension shepherd-root-service-type
                                          potluck-shepherd-service)
		       (service-extension account-service-type
					  potluck-accounts)))
                (default-value (potluck-configuration))))