1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2016 ng0 <ng0@n0.is>
3 ;;; Copyright © 2016 Sou Bunnbu <iyzsong@member.fsf.org>
4 ;;; Copyright © 2017 Oleg Pykhalov <go.wigust@gmail.com>
5 ;;; Copyright © 2017 Clément Lassieur <clement@lassieur.org>
6 ;;; Copyright © 2018 Christopher Baines <mail@cbaines.net>
8 ;;; This file is part of GNU Guix.
10 ;;; GNU Guix is free software; you can redistribute it and/or modify it
11 ;;; under the terms of the GNU General Public License as published by
12 ;;; the Free Software Foundation; either version 3 of the License, or (at
13 ;;; your option) any later version.
15 ;;; GNU Guix is distributed in the hope that it will be useful, but
16 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;;; GNU General Public License for more details.
20 ;;; You should have received a copy of the GNU General Public License
21 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
23 (define-module (gnu services version-control)
24 #:use-module (gnu services)
25 #:use-module (gnu services base)
26 #:use-module (gnu services shepherd)
27 #:use-module (gnu services web)
28 #:use-module (gnu system shadow)
29 #:use-module (gnu packages version-control)
30 #:use-module (gnu packages admin)
31 #:use-module (guix records)
32 #:use-module (guix gexp)
33 #:use-module (guix store)
34 #:use-module (srfi srfi-1)
35 #:use-module (srfi srfi-26)
36 #:use-module (ice-9 format)
37 #:use-module (ice-9 match)
38 #:export (git-daemon-service
39 git-daemon-service-type
40 git-daemon-configuration
41 git-daemon-configuration?
43 git-http-configuration
44 git-http-configuration?
45 git-http-nginx-location-configuration
47 <gitolite-configuration>
48 gitolite-configuration
49 gitolite-configuration-package
50 gitolite-configuration-user
51 gitolite-configuration-rc-file
52 gitolite-configuration-admin-pubkey
56 gitolite-rc-file-umask
57 gitolite-rc-file-git-config-keys
58 gitolite-rc-file-roles
59 gitolite-rc-file-enable
61 gitolite-service-type))
65 ;;; Version Control related services.
74 (define-record-type* <git-daemon-configuration>
75 git-daemon-configuration
76 make-git-daemon-configuration
77 git-daemon-configuration?
78 (package git-daemon-configuration-package ;package
80 (export-all? git-daemon-configuration-export-all ;boolean
82 (base-path git-daemon-configuration-base-path ;string | #f
84 (user-path git-daemon-configuration-user-path ;string | #f
86 (listen git-daemon-configuration-listen ;list of string
88 (port git-daemon-configuration-port ;number | #f
90 (whitelist git-daemon-configuration-whitelist ;list of string
92 (extra-options git-daemon-configuration-extra-options ;list of string
95 (define git-daemon-shepherd-service
97 (($ <git-daemon-configuration>
98 package export-all? base-path user-path
99 listen port whitelist extra-options)
100 (let* ((git (file-append package "/bin/git"))
102 "daemon" "--syslog" "--reuseaddr"
107 `(,(string-append "--base-path=" base-path))
110 `(,(string-append "--user-path=" user-path))
112 ,@(map (cut string-append "--listen=" <>) listen)
115 "--port=" (number->string port)))
119 (list (shepherd-service
120 (documentation "Run the git-daemon.")
121 (requirement '(networking))
122 (provision '(git-daemon))
123 (start #~(make-forkexec-constructor '#$command
125 #:group "git-daemon"))
126 (stop #~(make-kill-destructor))))))))
128 (define %git-daemon-accounts
129 ;; User account and group for git-daemon.
137 (comment "Git daemon user")
138 (home-directory "/var/empty")
139 (shell (file-append shadow "/sbin/nologin")))))
141 (define (git-daemon-activation config)
142 "Return the activation gexp for git-daemon using CONFIG."
143 (let ((base-path (git-daemon-configuration-base-path config)))
145 (use-modules (guix build utils))
146 ;; Create the 'base-path' directory when it's not '#f'.
147 (and=> #$base-path mkdir-p))))
149 (define git-daemon-service-type
153 (list (service-extension shepherd-root-service-type
154 git-daemon-shepherd-service)
155 (service-extension account-service-type
156 (const %git-daemon-accounts))
157 (service-extension activation-service-type
158 git-daemon-activation)))
160 "Expose Git repositories over the insecure @code{git://} TCP-based
162 (default-value (git-daemon-configuration))))
164 (define* (git-daemon-service #:key (config (git-daemon-configuration)))
165 "Return a service that runs @command{git daemon}, a simple TCP server to
166 expose repositories over the Git protocol for annoymous access.
168 The optional @var{config} argument should be a
169 @code{<git-daemon-configuration>} object, by default it allows read-only
170 access to exported repositories under @file{/srv/git}."
171 (service git-daemon-service-type config))
175 ;;; HTTP access. Add the result of calling
176 ;;; git-http-nginx-location-configuration to an nginx-server-configuration's
177 ;;; "locations" field.
180 (define-record-type* <git-http-configuration>
181 git-http-configuration
182 make-git-http-configuration
183 git-http-configuration?
184 (package git-http-configuration-package ;package
186 (git-root git-http-configuration-git-root ;string
187 (default "/srv/git"))
188 (export-all? git-http-configuration-export-all? ;boolean
190 (uri-path git-http-configuration-uri-path ;string
192 (fcgiwrap-socket git-http-configuration-fcgiwrap-socket ;string
193 (default "127.0.0.1:9000")))
195 (define* (git-http-nginx-location-configuration #:optional
197 (git-http-configuration)))
199 (($ <git-http-configuration> package git-root export-all?
200 uri-path fcgiwrap-socket)
201 (nginx-location-configuration
202 (uri (string-append "~ /" (string-trim-both uri-path #\/) "(/.*)"))
205 (list "fastcgi_pass " fcgiwrap-socket ";")
206 (list "fastcgi_param SCRIPT_FILENAME "
207 package "/libexec/git-core/git-http-backend"
209 "fastcgi_param QUERY_STRING $query_string;"
210 "fastcgi_param REQUEST_METHOD $request_method;"
211 "fastcgi_param CONTENT_TYPE $content_type;"
212 "fastcgi_param CONTENT_LENGTH $content_length;"
214 "fastcgi_param GIT_HTTP_EXPORT_ALL \"\";"
216 (list "fastcgi_param GIT_PROJECT_ROOT " git-root ";")
217 "fastcgi_param PATH_INFO $1;"))))))
224 (define-record-type* <gitolite-rc-file>
225 gitolite-rc-file make-gitolite-rc-file
227 (umask gitolite-rc-file-umask
229 (git-config-keys gitolite-rc-file-git-config-keys
231 (roles gitolite-rc-file-roles
232 (default '(("READERS" . 1)
234 (enable gitolite-rc-file-enable
245 (define-gexp-compiler (gitolite-rc-file-compiler
246 (file <gitolite-rc-file>) system target)
248 (($ <gitolite-rc-file> umask git-config-keys roles enable)
249 (apply text-file* "gitolite.rc"
251 " UMASK => " ,(format #f "~4,'0o" umask) ",\n"
252 " GIT_CONFIG_KEYS => '" ,git-config-keys "',\n"
256 (simple-format #f " ~A => ~A,\n" role value)))
261 ,@(map (lambda (value)
262 (simple-format #f " '~A',\n" value))
269 (define-record-type* <gitolite-configuration>
270 gitolite-configuration make-gitolite-configuration
271 gitolite-configuration?
272 (package gitolite-configuration-package
274 (user gitolite-configuration-user
276 (group gitolite-configuration-group
278 (home-directory gitolite-configuration-home-directory
279 (default "/var/lib/gitolite"))
280 (rc-file gitolite-configuration-rc-file
281 (default (gitolite-rc-file)))
282 (admin-pubkey gitolite-configuration-admin-pubkey))
284 (define gitolite-accounts
286 (($ <gitolite-configuration> package user group home-directory
287 rc-file admin-pubkey)
288 ;; User group and account to run Gitolite.
289 (list (user-group (name user) (system? #t))
294 (comment "Gitolite user")
295 (home-directory home-directory))))))
297 (define gitolite-activation
299 (($ <gitolite-configuration> package user group home
300 rc-file admin-pubkey)
302 (use-modules (ice-9 match)
305 (let* ((user-info (getpwnam #$user))
306 (admin-pubkey #$admin-pubkey)
307 (pubkey-file (string-append
310 (strip-store-file-name admin-pubkey)))))
312 (simple-format #t "guix: gitolite: installing ~A\n" #$rc-file)
313 (copy-file #$rc-file #$(string-append home "/.gitolite.rc"))
315 ;; The key must be writable, so copy it from the store
316 (copy-file admin-pubkey pubkey-file)
318 (chmod pubkey-file #o500)
320 (passwd:uid user-info)
321 (passwd:gid user-info))
323 ;; Set the git configuration, to avoid gitolite trying to use
324 ;; the hostname command, as the network might not be up yet
325 (with-output-to-file #$(string-append home "/.gitconfig")
329 email = guix@localhost
331 ;; Run Gitolite setup, as this updates the hooks and include the
332 ;; admin pubkey if specified. The admin pubkey is required for
333 ;; initial setup, and will replace the previous key if run after
335 (match (primitive-fork)
337 ;; Exit with a non-zero status code if an exception is thrown.
341 (setenv "HOME" (passwd:dir user-info))
342 (setenv "USER" #$user)
343 (setgid (passwd:gid user-info))
344 (setuid (passwd:uid user-info))
346 (system* #$(file-append package "/bin/gitolite")
348 "-m" "gitolite setup by GNU Guix"
351 (primitive-exit 1))))
354 (when (file-exists? pubkey-file)
355 (delete-file pubkey-file)))))))
357 (define gitolite-service-type
361 (list (service-extension activation-service-type
363 (service-extension account-service-type
365 (service-extension profile-service-type
366 ;; The Gitolite package in Guix uses
367 ;; gitolite-shell in the authorized_keys file, so
368 ;; gitolite-shell needs to be on the PATH for
372 (gitolite-configuration-package config))))))
374 "Setup @command{gitolite}, a Git hosting tool providing access over SSH..
375 By default, the @code{git} user is used, but this is configurable.
376 Additionally, Gitolite can integrate with with tools like gitweb or cgit to
377 provide a web interface to view selected repositories.")))