version-control.scm 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2016 ng0 <ng0@we.make.ritual.n0.is>
  3. ;;; Copyright © 2016 Sou Bunnbu <iyzsong@member.fsf.org>
  4. ;;; Copyright © 2017 Oleg Pykhalov <go.wigust@gmail.com>
  5. ;;;
  6. ;;; This file is part of GNU Guix.
  7. ;;;
  8. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  9. ;;; under the terms of the GNU General Public License as published by
  10. ;;; the Free Software Foundation; either version 3 of the License, or (at
  11. ;;; your option) any later version.
  12. ;;;
  13. ;;; GNU Guix is distributed in the hope that it will be useful, but
  14. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  15. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. ;;; GNU General Public License for more details.
  17. ;;;
  18. ;;; You should have received a copy of the GNU General Public License
  19. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  20. (define-module (gnu services version-control)
  21. #:use-module (gnu services)
  22. #:use-module (gnu services base)
  23. #:use-module (gnu services shepherd)
  24. #:use-module (gnu services web)
  25. #:use-module (gnu system shadow)
  26. #:use-module (gnu packages version-control)
  27. #:use-module (gnu packages admin)
  28. #:use-module (guix records)
  29. #:use-module (guix gexp)
  30. #:use-module (guix store)
  31. #:use-module (srfi srfi-1)
  32. #:use-module (srfi srfi-26)
  33. #:use-module (ice-9 match)
  34. #:export (git-daemon-service
  35. git-daemon-service-type
  36. git-daemon-configuration
  37. git-daemon-configuration?
  38. <cgit-configuration-file>
  39. cgit-configuration-file
  40. cgit-configuration-file?
  41. cgit-configuration-file-css
  42. cgit-configuration-file-logo
  43. cgit-configuration-file-robots
  44. cgit-configuration-file-virtual-root
  45. cgit-configuration-file-repository-directory
  46. <cgit-configuration>
  47. cgit-configuration
  48. cgit-configuration?
  49. cgit-configuration-config-file
  50. cgit-configuration-package
  51. %cgit-configuration-nginx
  52. cgit-configuration-nginx-config
  53. cgit-service-type))
  54. ;;; Commentary:
  55. ;;;
  56. ;;; Version Control related services.
  57. ;;;
  58. ;;; Code:
  59. ;;;
  60. ;;; Git daemon.
  61. ;;;
  62. (define-record-type* <git-daemon-configuration>
  63. git-daemon-configuration
  64. make-git-daemon-configuration
  65. git-daemon-configuration?
  66. (package git-daemon-configuration-package ;package
  67. (default git))
  68. (export-all? git-daemon-configuration-export-all ;boolean
  69. (default #f))
  70. (base-path git-daemon-configuration-base-path ;string | #f
  71. (default "/srv/git"))
  72. (user-path git-daemon-configuration-user-path ;string | #f
  73. (default #f))
  74. (listen git-daemon-configuration-listen ;list of string
  75. (default '()))
  76. (port git-daemon-configuration-port ;number | #f
  77. (default #f))
  78. (whitelist git-daemon-configuration-whitelist ;list of string
  79. (default '()))
  80. (extra-options git-daemon-configuration-extra-options ;list of string
  81. (default '())))
  82. (define git-daemon-shepherd-service
  83. (match-lambda
  84. (($ <git-daemon-configuration>
  85. package export-all? base-path user-path
  86. listen port whitelist extra-options)
  87. (let* ((git (file-append package "/bin/git"))
  88. (command `(,git
  89. "daemon" "--syslog" "--reuseaddr"
  90. ,@(if export-all?
  91. '("--export-all")
  92. '())
  93. ,@(if base-path
  94. `(,(string-append "--base-path=" base-path))
  95. '())
  96. ,@(if user-path
  97. `(,(string-append "--user-path=" user-path))
  98. '())
  99. ,@(map (cut string-append "--listen=" <>) listen)
  100. ,@(if port
  101. `(,(string-append
  102. "--port=" (number->string port)))
  103. '())
  104. ,@extra-options
  105. ,@whitelist)))
  106. (list (shepherd-service
  107. (documentation "Run the git-daemon.")
  108. (requirement '(networking))
  109. (provision '(git-daemon))
  110. (start #~(make-forkexec-constructor '#$command
  111. #:user "git-daemon"
  112. #:group "git-daemon"))
  113. (stop #~(make-kill-destructor))))))))
  114. (define %git-daemon-accounts
  115. ;; User account and group for git-daemon.
  116. (list (user-group
  117. (name "git-daemon")
  118. (system? #t))
  119. (user-account
  120. (name "git-daemon")
  121. (system? #t)
  122. (group "git-daemon")
  123. (comment "Git daemon user")
  124. (home-directory "/var/empty")
  125. (shell (file-append shadow "/sbin/nologin")))))
  126. (define (git-daemon-activation config)
  127. "Return the activation gexp for git-daemon using CONFIG."
  128. (let ((base-path (git-daemon-configuration-base-path config)))
  129. #~(begin
  130. (use-modules (guix build utils))
  131. ;; Create the 'base-path' directory when it's not '#f'.
  132. (and=> #$base-path mkdir-p))))
  133. (define git-daemon-service-type
  134. (service-type
  135. (name 'git-daemon)
  136. (extensions
  137. (list (service-extension shepherd-root-service-type
  138. git-daemon-shepherd-service)
  139. (service-extension account-service-type
  140. (const %git-daemon-accounts))
  141. (service-extension activation-service-type
  142. git-daemon-activation)))))
  143. (define* (git-daemon-service #:key (config (git-daemon-configuration)))
  144. "Return a service that runs @command{git daemon}, a simple TCP server to
  145. expose repositories over the Git protocol for annoymous access.
  146. The optional @var{config} argument should be a
  147. @code{<git-daemon-configuration>} object, by default it allows read-only
  148. access to exported repositories under @file{/srv/git}."
  149. (service git-daemon-service-type config))
  150. ;;;
  151. ;;; Cgit
  152. ;;;
  153. (define-record-type* <cgit-configuration-file>
  154. cgit-configuration-file
  155. make-cgit-configuration-file
  156. cgit-configuration-file?
  157. (css cgit-configuration-file-css ; string
  158. (default "/share/cgit/cgit.css"))
  159. (logo cgit-configuration-file-logo ; string
  160. (default "/share/cgit/cgit.png"))
  161. (robots cgit-configuration-file-robots ; list
  162. (default '("noindex" "nofollow")))
  163. (virtual-root cgit-configuration-file-virtual-root ; string
  164. (default "/"))
  165. (repository-directory cgit-configuration-file-repository-directory ; string
  166. (default "/srv/git")))
  167. (define (cgit-configuration-robots-string robots)
  168. (string-join robots ", "))
  169. (define-gexp-compiler (cgit-configuration-file-compiler
  170. (file <cgit-configuration-file>) system target)
  171. (match file
  172. (($ <cgit-configuration-file> css logo
  173. robots virtual-root repository-directory)
  174. (apply text-file* "cgitrc"
  175. (letrec-syntax ((option (syntax-rules ()
  176. ((_ key value)
  177. (if value
  178. `(,key "=" ,value "\n")
  179. '()))))
  180. (key/value (syntax-rules ()
  181. ((_ (key value) rest ...)
  182. (append (option key value)
  183. (key/value rest ...)))
  184. ((_)
  185. '()))))
  186. (key/value ("css" css)
  187. ("logo" logo)
  188. ("robots" (cgit-configuration-robots-string robots))
  189. ("virtual-root" virtual-root)
  190. ("scan-path" repository-directory)))))))
  191. (define %cgit-configuration-nginx
  192. (list
  193. (nginx-server-configuration
  194. (root cgit)
  195. (locations
  196. (list
  197. (nginx-location-configuration
  198. (uri "@cgit")
  199. (body '("fastcgi_param SCRIPT_FILENAME $document_root/lib/cgit/cgit.cgi;"
  200. "fastcgi_param PATH_INFO $uri;"
  201. "fastcgi_param QUERY_STRING $args;"
  202. "fastcgi_param HTTP_HOST $server_name;"
  203. "fastcgi_pass 127.0.0.1:9000;")))))
  204. (try-files (list "$uri" "@cgit"))
  205. (https-port #f)
  206. (ssl-certificate #f)
  207. (ssl-certificate-key #f))))
  208. (define-record-type* <cgit-configuration>
  209. cgit-configuration make-cgit-configuration
  210. cgit-configuration?
  211. (config-file cgit-configuration-config-file
  212. (default (cgit-configuration-file)))
  213. (package cgit-configuration-package
  214. (default cgit))
  215. (nginx cgit-configuration-nginx
  216. (default %cgit-configuration-nginx)))
  217. (define (cgit-activation config)
  218. ;; Cgit compiled with default configuration path
  219. #~(begin
  220. (use-modules (guix build utils))
  221. (mkdir-p "/var/cache/cgit")
  222. (copy-file #$(cgit-configuration-config-file config) "/etc/cgitrc")))
  223. (define (cgit-configuration-nginx-config config)
  224. (cgit-configuration-nginx config))
  225. (define cgit-service-type
  226. (service-type
  227. (name 'cgit)
  228. (extensions
  229. (list (service-extension activation-service-type
  230. cgit-activation)
  231. (service-extension nginx-service-type
  232. cgit-configuration-nginx-config)))
  233. (default-value (cgit-configuration))))