version-control.scm 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2016 Nikita <nikita@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>
  7. ;;;
  8. ;;; This file is part of GNU Guix.
  9. ;;;
  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.
  14. ;;;
  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.
  19. ;;;
  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/>.
  22. (define-module (gnu services version-control)
  23. #:use-module (gnu services)
  24. #:use-module (gnu services base)
  25. #:use-module (gnu services shepherd)
  26. #:use-module (gnu services web)
  27. #:use-module (gnu system shadow)
  28. #:use-module (gnu packages version-control)
  29. #:use-module (gnu packages admin)
  30. #:use-module (guix records)
  31. #:use-module (guix gexp)
  32. #:use-module (guix store)
  33. #:use-module (srfi srfi-1)
  34. #:use-module (srfi srfi-26)
  35. #:use-module (ice-9 format)
  36. #:use-module (ice-9 match)
  37. #:export (git-daemon-service
  38. git-daemon-service-type
  39. git-daemon-configuration
  40. git-daemon-configuration?
  41. git-http-configuration
  42. git-http-configuration?
  43. git-http-nginx-location-configuration
  44. <gitolite-configuration>
  45. gitolite-configuration
  46. gitolite-configuration-package
  47. gitolite-configuration-user
  48. gitolite-configuration-rc-file
  49. gitolite-configuration-admin-pubkey
  50. <gitolite-rc-file>
  51. gitolite-rc-file
  52. gitolite-rc-file-umask
  53. gitolite-rc-file-git-config-keys
  54. gitolite-rc-file-roles
  55. gitolite-rc-file-enable
  56. gitolite-service-type))
  57. ;;; Commentary:
  58. ;;;
  59. ;;; Version Control related services.
  60. ;;;
  61. ;;; Code:
  62. ;;;
  63. ;;; Git daemon.
  64. ;;;
  65. (define-record-type* <git-daemon-configuration>
  66. git-daemon-configuration
  67. make-git-daemon-configuration
  68. git-daemon-configuration?
  69. (package git-daemon-configuration-package ;package
  70. (default git))
  71. (export-all? git-daemon-configuration-export-all ;boolean
  72. (default #f))
  73. (base-path git-daemon-configuration-base-path ;string | #f
  74. (default "/srv/git"))
  75. (user-path git-daemon-configuration-user-path ;string | #f
  76. (default #f))
  77. (listen git-daemon-configuration-listen ;list of string
  78. (default '()))
  79. (port git-daemon-configuration-port ;number | #f
  80. (default #f))
  81. (whitelist git-daemon-configuration-whitelist ;list of string
  82. (default '()))
  83. (extra-options git-daemon-configuration-extra-options ;list of string
  84. (default '())))
  85. (define git-daemon-shepherd-service
  86. (match-lambda
  87. (($ <git-daemon-configuration>
  88. package export-all? base-path user-path
  89. listen port whitelist extra-options)
  90. (let* ((git (file-append package "/bin/git"))
  91. (command `(,git
  92. "daemon" "--syslog" "--reuseaddr"
  93. ,@(if export-all?
  94. '("--export-all")
  95. '())
  96. ,@(if base-path
  97. `(,(string-append "--base-path=" base-path))
  98. '())
  99. ,@(if user-path
  100. `(,(string-append "--user-path=" user-path))
  101. '())
  102. ,@(map (cut string-append "--listen=" <>) listen)
  103. ,@(if port
  104. `(,(string-append
  105. "--port=" (number->string port)))
  106. '())
  107. ,@extra-options
  108. ,@whitelist)))
  109. (list (shepherd-service
  110. (documentation "Run the git-daemon.")
  111. (requirement '(networking))
  112. (provision '(git-daemon))
  113. (start #~(make-forkexec-constructor '#$command
  114. #:user "git-daemon"
  115. #:group "git-daemon"))
  116. (stop #~(make-kill-destructor))))))))
  117. (define %git-daemon-accounts
  118. ;; User account and group for git-daemon.
  119. (list (user-group
  120. (name "git-daemon")
  121. (system? #t))
  122. (user-account
  123. (name "git-daemon")
  124. (system? #t)
  125. (group "git-daemon")
  126. (comment "Git daemon user")
  127. (home-directory "/var/empty")
  128. (shell (file-append shadow "/sbin/nologin")))))
  129. (define (git-daemon-activation config)
  130. "Return the activation gexp for git-daemon using CONFIG."
  131. (let ((base-path (git-daemon-configuration-base-path config)))
  132. #~(begin
  133. (use-modules (guix build utils))
  134. ;; Create the 'base-path' directory when it's not '#f'.
  135. (and=> #$base-path mkdir-p))))
  136. (define git-daemon-service-type
  137. (service-type
  138. (name 'git-daemon)
  139. (extensions
  140. (list (service-extension shepherd-root-service-type
  141. git-daemon-shepherd-service)
  142. (service-extension account-service-type
  143. (const %git-daemon-accounts))
  144. (service-extension activation-service-type
  145. git-daemon-activation)))
  146. (description
  147. "Expose Git repositories over the insecure @code{git://} TCP-based
  148. protocol.")
  149. (default-value (git-daemon-configuration))))
  150. (define* (git-daemon-service #:key (config (git-daemon-configuration)))
  151. "Return a service that runs @command{git daemon}, a simple TCP server to
  152. expose repositories over the Git protocol for annoymous access.
  153. The optional @var{config} argument should be a
  154. @code{<git-daemon-configuration>} object, by default it allows read-only
  155. access to exported repositories under @file{/srv/git}."
  156. (service git-daemon-service-type config))
  157. ;;;
  158. ;;; HTTP access. Add the result of calling
  159. ;;; git-http-nginx-location-configuration to an nginx-server-configuration's
  160. ;;; "locations" field.
  161. ;;;
  162. (define-record-type* <git-http-configuration>
  163. git-http-configuration
  164. make-git-http-configuration
  165. git-http-configuration?
  166. (package git-http-configuration-package ;package
  167. (default git))
  168. (git-root git-http-configuration-git-root ;string
  169. (default "/srv/git"))
  170. (export-all? git-http-configuration-export-all? ;boolean
  171. (default #f))
  172. (uri-path git-http-configuration-uri-path ;string
  173. (default "/git/"))
  174. (fcgiwrap-socket git-http-configuration-fcgiwrap-socket ;string
  175. (default "127.0.0.1:9000")))
  176. (define* (git-http-nginx-location-configuration #:optional
  177. (config
  178. (git-http-configuration)))
  179. (match config
  180. (($ <git-http-configuration> package git-root export-all?
  181. uri-path fcgiwrap-socket)
  182. (nginx-location-configuration
  183. (uri (string-append "~ /" (string-trim-both uri-path #\/) "(/.*)"))
  184. (body
  185. (list
  186. (list "fastcgi_pass " fcgiwrap-socket ";")
  187. (list "fastcgi_param SCRIPT_FILENAME "
  188. package "/libexec/git-core/git-http-backend"
  189. ";")
  190. "fastcgi_param QUERY_STRING $query_string;"
  191. "fastcgi_param REQUEST_METHOD $request_method;"
  192. "fastcgi_param CONTENT_TYPE $content_type;"
  193. "fastcgi_param CONTENT_LENGTH $content_length;"
  194. (if export-all?
  195. "fastcgi_param GIT_HTTP_EXPORT_ALL \"\";"
  196. "")
  197. (list "fastcgi_param GIT_PROJECT_ROOT " git-root ";")
  198. "fastcgi_param PATH_INFO $1;"))))))
  199. ;;;
  200. ;;; Gitolite
  201. ;;;
  202. (define-record-type* <gitolite-rc-file>
  203. gitolite-rc-file make-gitolite-rc-file
  204. gitolite-rc-file?
  205. (umask gitolite-rc-file-umask
  206. (default #o0077))
  207. (git-config-keys gitolite-rc-file-git-config-keys
  208. (default ""))
  209. (roles gitolite-rc-file-roles
  210. (default '(("READERS" . 1)
  211. ("WRITERS" . 1))))
  212. (enable gitolite-rc-file-enable
  213. (default '("help"
  214. "desc"
  215. "info"
  216. "perms"
  217. "writable"
  218. "ssh-authkeys"
  219. "git-config"
  220. "daemon"
  221. "gitweb"))))
  222. (define-gexp-compiler (gitolite-rc-file-compiler
  223. (file <gitolite-rc-file>) system target)
  224. (match file
  225. (($ <gitolite-rc-file> umask git-config-keys roles enable)
  226. (apply text-file* "gitolite.rc"
  227. `("%RC = (\n"
  228. " UMASK => " ,(format #f "~4,'0o" umask) ",\n"
  229. " GIT_CONFIG_KEYS => '" ,git-config-keys "',\n"
  230. " ROLES => {\n"
  231. ,@(map (match-lambda
  232. ((role . value)
  233. (simple-format #f " ~A => ~A,\n" role value)))
  234. roles)
  235. " },\n"
  236. "\n"
  237. " ENABLE => [\n"
  238. ,@(map (lambda (value)
  239. (simple-format #f " '~A',\n" value))
  240. enable)
  241. " ],\n"
  242. ");\n"
  243. "\n"
  244. "1;\n")))))
  245. (define-record-type* <gitolite-configuration>
  246. gitolite-configuration make-gitolite-configuration
  247. gitolite-configuration?
  248. (package gitolite-configuration-package
  249. (default gitolite))
  250. (user gitolite-configuration-user
  251. (default "git"))
  252. (group gitolite-configuration-group
  253. (default "git"))
  254. (home-directory gitolite-configuration-home-directory
  255. (default "/var/lib/gitolite"))
  256. (rc-file gitolite-configuration-rc-file
  257. (default (gitolite-rc-file)))
  258. (admin-pubkey gitolite-configuration-admin-pubkey))
  259. (define gitolite-accounts
  260. (match-lambda
  261. (($ <gitolite-configuration> package user group home-directory
  262. rc-file admin-pubkey)
  263. ;; User group and account to run Gitolite.
  264. (list (user-group (name user) (system? #t))
  265. (user-account
  266. (name user)
  267. (group group)
  268. (system? #t)
  269. (comment "Gitolite user")
  270. (home-directory home-directory))))))
  271. (define gitolite-activation
  272. (match-lambda
  273. (($ <gitolite-configuration> package user group home
  274. rc-file admin-pubkey)
  275. #~(begin
  276. (use-modules (ice-9 match)
  277. (guix build utils))
  278. (let* ((user-info (getpwnam #$user))
  279. (admin-pubkey #$admin-pubkey)
  280. (pubkey-file (string-append
  281. #$home "/"
  282. (basename
  283. (strip-store-file-name admin-pubkey))))
  284. (rc-file #$(string-append home "/.gitolite.rc")))
  285. (simple-format #t "guix: gitolite: installing ~A\n" #$rc-file)
  286. (copy-file #$rc-file rc-file)
  287. ;; ensure gitolite's user can read the configuration
  288. (chown rc-file
  289. (passwd:uid user-info)
  290. (passwd:gid user-info))
  291. ;; The key must be writable, so copy it from the store
  292. (copy-file admin-pubkey pubkey-file)
  293. (chmod pubkey-file #o500)
  294. (chown pubkey-file
  295. (passwd:uid user-info)
  296. (passwd:gid user-info))
  297. ;; Set the git configuration, to avoid gitolite trying to use
  298. ;; the hostname command, as the network might not be up yet
  299. (with-output-to-file #$(string-append home "/.gitconfig")
  300. (lambda ()
  301. (display "[user]
  302. name = GNU Guix
  303. email = guix@localhost
  304. ")))
  305. ;; Run Gitolite setup, as this updates the hooks and include the
  306. ;; admin pubkey if specified. The admin pubkey is required for
  307. ;; initial setup, and will replace the previous key if run after
  308. ;; initial setup
  309. (match (primitive-fork)
  310. (0
  311. ;; Exit with a non-zero status code if an exception is thrown.
  312. (dynamic-wind
  313. (const #t)
  314. (lambda ()
  315. (setenv "HOME" (passwd:dir user-info))
  316. (setenv "USER" #$user)
  317. (setgid (passwd:gid user-info))
  318. (setuid (passwd:uid user-info))
  319. (primitive-exit
  320. (system* #$(file-append package "/bin/gitolite")
  321. "setup"
  322. "-m" "gitolite setup by GNU Guix"
  323. "-pk" pubkey-file)))
  324. (lambda ()
  325. (primitive-exit 1))))
  326. (pid (waitpid pid)))
  327. (when (file-exists? pubkey-file)
  328. (delete-file pubkey-file)))))))
  329. (define gitolite-service-type
  330. (service-type
  331. (name 'gitolite)
  332. (extensions
  333. (list (service-extension activation-service-type
  334. gitolite-activation)
  335. (service-extension account-service-type
  336. gitolite-accounts)
  337. (service-extension profile-service-type
  338. ;; The Gitolite package in Guix uses
  339. ;; gitolite-shell in the authorized_keys file, so
  340. ;; gitolite-shell needs to be on the PATH for
  341. ;; gitolite to work.
  342. (lambda (config)
  343. (list
  344. (gitolite-configuration-package config))))))
  345. (description
  346. "Setup @command{gitolite}, a Git hosting tool providing access over SSH..
  347. By default, the @code{git} user is used, but this is configurable.
  348. Additionally, Gitolite can integrate with with tools like gitweb or cgit to
  349. provide a web interface to view selected repositories.")))