1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2018 Oleg Pykhalov <go.wigust@gmail.com>
3 ;;; Copyright © 2018 Clément Lassieur <clement@lassieur.org>
4 ;;; Copyright © 2018 Christopher Baines <mail@cbaines.net>
6 ;;; This file is part of GNU Guix.
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.
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.
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/>.
21 (define-module (gnu services cgit)
22 #:use-module (gnu packages admin)
23 #:use-module (gnu packages version-control)
24 #:use-module (gnu services base)
25 #:use-module (gnu services configuration)
26 #:use-module (gnu services shepherd)
27 #:use-module (gnu services web)
28 #:use-module (gnu services)
29 #:use-module (gnu system shadow)
30 #:use-module (guix gexp)
31 #:use-module (guix packages)
32 #:use-module (guix records)
33 #:use-module (guix store)
34 #:use-module (ice-9 match)
35 #:use-module (srfi srfi-1)
36 #:use-module (srfi srfi-26)
37 #:export (repository-cgit-configuration
39 %cgit-configuration-nginx
40 cgit-configuration-nginx-config
41 opaque-cgit-configuration
46 ;;; This module provides a service definition for the Cgit a web frontend for
47 ;;; Git repositories written in C.
49 ;;; Note: fields of <cgit-configuration> and <repository-cgit-configuration>
50 ;;; should be specified in the specific order.
54 (define %cgit-configuration-nginx
55 (nginx-server-configuration
59 (nginx-location-configuration
61 (body '("fastcgi_param SCRIPT_FILENAME $document_root/lib/cgit/cgit.cgi;"
62 "fastcgi_param PATH_INFO $uri;"
63 "fastcgi_param QUERY_STRING $args;"
64 "fastcgi_param HTTP_HOST $server_name;"
65 "fastcgi_pass 127.0.0.1:9000;")))))
66 (try-files (list "$uri" "@cgit"))
69 (ssl-certificate-key #f)))
73 ;;; Serialize <cgit-configuration>
76 (define (uglify-field-name field-name)
77 (string-delete #\? (symbol->string field-name)))
79 (define (serialize-field field-name val)
80 #~(format #f "~a=~a\n" #$(uglify-field-name field-name) #$val))
82 (define (serialize-string field-name val)
83 (if (and (string? val) (string=? val ""))
85 (serialize-field field-name val)))
87 (define (serialize-list field-name val)
88 (if (null? val) "" (serialize-field field-name (string-join val))))
90 (define robots-list? list?)
92 (define (serialize-robots-list field-name val)
93 (if (null? val) "" (serialize-field field-name (string-join val ", "))))
95 (define (integer? val)
98 (define (serialize-integer field-name val)
99 (serialize-field field-name (number->string val)))
101 (define (serialize-boolean field-name val)
102 (serialize-integer field-name (if val 1 0)))
104 (define (serialize-repository-cgit-configuration x)
105 (serialize-configuration x repository-cgit-configuration-fields))
107 (define (repository-cgit-configuration-list? val)
110 (define (serialize-repository-cgit-configuration-list field-name val)
112 #$@(map serialize-repository-cgit-configuration val)))
114 (define (file-object? val)
115 (or (file-like? val) (string? val)))
116 (define (serialize-file-object field-name val)
117 (serialize-string field-name val))
119 (define (project-list? val)
125 ;;; Serialize <nginx-server-configuration>
128 (define (nginx-server-configuration-list? val)
129 (and (list? val) (and-map nginx-server-configuration? val)))
131 (define (serialize-nginx-server-configuration-list field-name val)
136 ;;; Serialize <repository-cgit-configuration>
139 (define (serialize-repo-field field-name val)
140 #~(format #f "repo.~a=~a\n" #$(uglify-field-name field-name) #$val))
142 (define (serialize-repo-list field-name val)
143 (if (null? val) "" (serialize-repo-field field-name (string-join val))))
145 (define repo-boolean? boolean?)
147 (define (serialize-repo-integer field-name val)
148 (serialize-repo-field field-name (number->string val)))
150 (define (serialize-repo-boolean field-name val)
151 (serialize-repo-integer field-name (if val 1 0)))
152 (define-maybe repo-boolean)
154 (define repo-list? list?)
156 (define repo-string? string?)
158 (define (serialize-repo-string field-name val)
159 (if (string=? val "") "" (serialize-repo-field field-name val)))
161 (define repo-file-object? file-object?)
162 (define serialize-repo-file-object serialize-repo-string)
164 (define module-link-path? list?)
166 (define (serialize-module-link-path field-name val)
170 (format #f "repo.module-link.~a=~a\n" path text)))))
172 (define (serialize-project-list _ val)
176 (if (file-object? val)
178 (plain-file "project-list" (string-join val "\n"))))))
180 (define (serialize-extra-options extra-options)
181 (string-join extra-options "\n" 'suffix))
183 (define repository-directory? string?)
185 (define (serialize-repository-directory _ val)
186 (if (string=? val "") "" (format #f "scan-path=~a\n" val)))
188 (define mimetype-alist? list?)
190 (define (serialize-mimetype-alist field-name val)
191 (format #f "# Mimetypes\n~a"
194 ((extension mimetype)
195 (format #f "mimetype.~a=~a"
196 (symbol->string extension) mimetype)))
199 (define-configuration repository-cgit-configuration
202 "A mask of snapshot formats for this repo that cgit generates links for,
203 restricted by the global @code{snapshots} setting.")
205 (repo-file-object "")
206 "Override the default @code{source-filter}.")
209 "The relative URL used to access the repository.")
211 (repo-file-object "")
212 "Override the default @code{about-filter}.")
215 "Flag which, when set to @samp{age}, enables date ordering in the branch
216 ref list, and when set to @samp{name} enables ordering by branch name.")
219 "A list of URLs which can be used to clone repo.")
221 (repo-file-object "")
222 "Override the default @code{commit-filter}.")
225 "Flag which, when set to @samp{date}, enables strict date ordering in the
226 commit log, and when set to @samp{topo} enables strict topological ordering.")
229 "The name of the default branch for this repository. If no such branch
230 exists in the repository, the first branch name (when sorted) is used as
231 default instead. By default branch pointed to by HEAD, or \"master\" if there
232 is no suitable HEAD.")
235 "The value to show as repository description.")
238 "The value to show as repository homepage.")
240 (repo-file-object "")
241 "Override the default @code{email-filter}.")
242 (enable-commit-graph?
243 (maybe-repo-boolean 'disabled)
244 "A flag which can be used to disable the global setting
245 @code{enable-commit-graph?}.")
246 (enable-log-filecount?
247 (maybe-repo-boolean 'disabled)
248 "A flag which can be used to disable the global setting
249 @code{enable-log-filecount?}.")
250 (enable-log-linecount?
251 (maybe-repo-boolean 'disabled)
252 "A flag which can be used to disable the global setting
253 @code{enable-log-linecount?}.")
254 (enable-remote-branches?
255 (maybe-repo-boolean 'disabled)
256 "Flag which, when set to @code{#t}, will make cgit display remote
257 branches in the summary and refs views.")
258 (enable-subject-links?
259 (maybe-repo-boolean 'disabled)
260 "A flag which can be used to override the global setting
261 @code{enable-subject-links?}.")
262 (enable-html-serving?
263 (maybe-repo-boolean 'disabled)
264 "A flag which can be used to override the global setting
265 @code{enable-html-serving?}.")
268 "Flag which, when set to @code{#t}, hides the repository from the
272 "Flag which, when set to @samp{#t}, ignores the repository.")
274 (repo-file-object "")
275 "URL which specifies the source of an image which will be used as a
276 logo on this repo’s pages.")
279 "URL loaded when clicking on the cgit logo image.")
281 (repo-file-object "")
282 "Override the default @code{owner-filter}.")
285 "Text which will be used as the formatstring for a hyperlink when a
286 submodule is printed in a directory listing. The arguments for the
287 formatstring are the path and SHA1 of the submodule commit.")
289 (module-link-path '())
290 "Text which will be used as the formatstring for a hyperlink when a
291 submodule with the specified subdirectory path is printed in a directory
295 "Override the default maximum statistics period.")
298 "The value to show as repository name.")
301 "A value used to identify the owner of the repository.")
304 "An absolute path to the repository directory.")
307 "A path (relative to repo) which specifies a file to include verbatim
308 as the \"About\" page for this repo.")
311 "The name of the current repository section - all repositories defined
312 after this option will inherit the current section name.")
315 "Extra options will be appended to cgitrc file."))
317 ;; Generate a <cgit-configuration> record, which may include a list of
318 ;; <repository-cgit-configuration>, <nginx-server-configuration>, <package>.
319 (define-configuration cgit-configuration
324 (nginx-server-configuration-list (list %cgit-configuration-nginx))
325 "NGINX configuration.")
328 "Specifies a command which will be invoked to format the content of about
329 pages (both top-level and for each repository).")
332 "Specifies a path, relative to each repository path, which can be used to
333 specify the date and time of the youngest commit in the repository.")
336 "Specifies a command that will be invoked for authenticating repository
340 "Flag which, when set to @samp{age}, enables date ordering in the branch
341 ref list, and when set @samp{name} enables ordering by branch name.")
343 (string "/var/cache/cgit")
344 "Path used to store the cgit cache entries.")
347 "Number which specifies the time-to-live, in minutes, for the cached
348 version of repository pages accessed with a fixed SHA1.")
351 "Number which specifies the time-to-live, in minutes, for the cached
352 version of repository pages accessed without a fixed SHA1.")
355 "Number which specifies the time-to-live, in minutes, for the cached
356 version of the repository summary page.")
359 "Number which specifies the time-to-live, in minutes, for the cached
360 version of the repository index page.")
363 "Number which specifies the time-to-live, in minutes, for the result of
364 scanning a path for Git repositories.")
367 "Number which specifies the time-to-live, in minutes, for the cached
368 version of the repository about page.")
371 "Number which specifies the time-to-live, in minutes, for the cached
372 version of snapshots.")
375 "The maximum number of entries in the cgit cache. When set to
376 @samp{0}, caching is disabled.")
377 (case-sensitive-sort?
379 "Sort items in the repo list case sensitively.")
382 "List of common prefixes which, when combined with a repository URL,
383 generates valid clone URLs for the repository.")
386 "List of @code{clone-url} templates.")
389 "Command which will be invoked to format commit messages.")
392 "Flag which, when set to @samp{date}, enables strict date ordering in the
393 commit log, and when set to @samp{topo} enables strict topological
396 (file-object "/share/cgit/cgit.css")
397 "URL which specifies the css document to include in all cgit pages.")
400 "Specifies a command which will be invoked to format names and email
401 address of committers, authors, and taggers, as represented in various
402 places throughout the cgit interface.")
405 "Flag which, when set to @samp{#t}, will make cgit generate a HTML
406 fragment suitable for embedding in other HTML pages.")
407 (enable-commit-graph?
409 "Flag which, when set to @samp{#t}, will make cgit print an ASCII-art
410 commit history graph to the left of the commit messages in the
411 repository log page.")
412 (enable-filter-overrides?
414 "Flag which, when set to @samp{#t}, allows all filter settings to be
415 overridden in repository-specific cgitrc files.")
416 (enable-follow-links?
418 "Flag which, when set to @samp{#t}, allows users to follow a file in the
422 "If set to @samp{#t}, cgit will act as an dumb HTTP endpoint for Git
426 "Flag which, when set to @samp{#t}, will make cgit generate extra links
427 \"summary\", \"commit\", \"tree\" for each repo in the repository index.")
430 "Flag which, when set to @samp{#t}, will make cgit display the owner of
431 each repo in the repository index.")
432 (enable-log-filecount?
434 "Flag which, when set to @samp{#t}, will make cgit print the number of
435 modified files for each commit on the repository log page.")
436 (enable-log-linecount?
438 "Flag which, when set to @samp{#t}, will make cgit print the number of
439 added and removed lines for each commit on the repository log page.")
440 (enable-remote-branches?
442 "Flag which, when set to @code{#t}, will make cgit display remote
443 branches in the summary and refs views.")
444 (enable-subject-links?
446 "Flag which, when set to @code{1}, will make cgit use the subject of
447 the parent commit as link text when generating links to parent commits
449 (enable-html-serving?
451 "Flag which, when set to @samp{#t}, will make cgit use the subject of the
452 parent commit as link text when generating links to parent commits in
454 (enable-tree-linenumbers?
456 "Flag which, when set to @samp{#t}, will make cgit generate linenumber
457 links for plaintext blobs printed in the tree view.")
460 "Flag which, when set to @samp{#f}, will allow cgit to use Git config to
461 set any repo specific settings.")
463 (file-object "/favicon.ico")
464 "URL used as link to a shortcut icon for cgit.")
467 "The content of the file specified with this option will be included
468 verbatim at the bottom of all pages (i.e. it replaces the standard
469 \"generated by...\" message).")
472 "The content of the file specified with this option will be included
473 verbatim in the HTML HEAD section on all pages.")
476 "The content of the file specified with this option will be included
477 verbatim at the top of all pages.")
480 "Name of a configfile to include before the rest of the current config-
484 "The content of the file specified with this option will be included
485 verbatim above the repository index.")
488 "The content of the file specified with this option will be included
489 verbatim below the heading on the repository index page.")
492 "Flag which, if set to @samp{#t}, makes cgit print commit and tag times
493 in the servers timezone.")
495 (file-object "/share/cgit/cgit.png")
496 "URL which specifies the source of an image which will be used as a logo
500 "URL loaded when clicking on the cgit logo image.")
503 "Command which will be invoked to format the Owner column of the main
507 "Number of items to display in atom feeds view.")
510 "Number of entries to list per page in \"log\" view.")
513 "Number of commit message characters to display in \"log\" view.")
516 "Specifies the number of entries to list per page on the repository index
520 "Specifies the maximum number of repo description characters to display
521 on the repository index page.")
524 "Specifies the maximum size of a blob to display HTML for in KBytes.")
527 "Maximum statistics period. Valid values are @samp{week},@samp{month},
528 @samp{quarter} and @samp{year}.")
530 (mimetype-alist '((gif "image/gif")
534 (pdf "application/pdf")
536 (svg "image/svg+xml")))
537 "Mimetype for the specified filename extension.")
540 "Specifies the file to use for automatic mimetype lookup.")
543 "Text which will be used as the formatstring for a hyperlink when a
544 submodule is printed in a directory listing.")
547 "If set to the value @samp{#t} caching will be disabled.")
550 "If set to @samp{#t} showing full author email addresses will be
554 "Flag which, when set to @samp{#t}, will make cgit omit the standard
555 header on all pages.")
558 "A list of subdirectories inside of @code{repository-directory}, relative
559 to it, that should loaded as Git repositories. An empty list means that all
560 subdirectories will be loaded.")
563 "Text which will be used as default value for @code{cgit-repo-readme}.")
566 "If set to @code{#t} and @code{repository-directory} is enabled, if any
567 repositories are found with a suffix of @code{.git}, this suffix will be
568 removed for the URL and name.")
571 "Maximum number of files to consider when detecting renames.")
574 "The way in which repositories in each section are sorted.")
576 (robots-list (list "noindex" "nofollow"))
577 "Text used as content for the @code{robots} meta-tag.")
579 (string "a fast webinterface for the git dscm")
580 "Text printed below the heading on the repository index page.")
583 "The content of the file specified with this option will be included
584 verbatim below thef \"about\" link on the repository index page.")
587 "Text printed as heading on the repository index page.")
590 "If set to @samp{#t} and repository-directory is enabled,
591 repository-directory will recurse into directories whose name starts with a
592 period. Otherwise, repository-directory will stay away from such directories,
593 considered as \"hidden\". Note that this does not apply to the \".git\"
594 directory in non-bare repos.")
597 "Text which specifies the default set of snapshot formats that cgit
598 generates links for.")
599 (repository-directory
600 (repository-directory "/srv/git")
601 "Name of the directory to scan for repositories (represents
605 "The name of the current repository section - all repositories defined
606 after this option will inherit the current section name.")
609 "Flag which, when set to @samp{1}, will sort the sections on the repository
613 "A number which, if defined prior to repository-directory, specifies how
614 many path elements from each repo path to use as a default section name.")
617 "If set to @samp{#t} shows side-by-side diffs instead of unidiffs per
621 "Specifies a command which will be invoked to format plaintext blobs in the
625 "Specifies the number of branches to display in the repository \"summary\"
629 "Specifies the number of log entries to display in the repository
633 "Specifies the number of tags to display in the repository \"summary\"
637 "Filename which, if specified, needs to be present within the repository
638 for cgit to allow access to that repository.")
641 "URL which, if specified, will be used as root for all cgit links.")
643 (repository-cgit-configuration-list '())
644 "A list of @dfn{cgit-repo} records to use with config.")
647 "Extra options will be appended to cgitrc file."))
649 ;; This distinguishes fields whose order matters, and makes sure further
650 ;; changes won't inadvertently change the order.
651 (define (serialize-cgit-configuration config)
652 (define (rest? field)
653 (not (memq (configuration-field-name field)
659 #$(let ((rest (filter rest? cgit-configuration-fields)))
660 (serialize-configuration config rest))
661 #$(serialize-project-list
663 (cgit-configuration-project-list config))
664 #$(serialize-extra-options
665 (cgit-configuration-extra-options config))
666 #$(serialize-repository-directory
667 'repository-directory
668 (cgit-configuration-repository-directory config))
669 #$(serialize-repository-cgit-configuration-list
671 (cgit-configuration-repositories config))))
673 (define-configuration opaque-cgit-configuration
678 (string (configuration-missing-field 'opaque-cgit-configuration 'cgitrc))
679 "The contents of the @code{cgitrc} to use.")
681 (string "/var/cache/cgit")
682 "Path used to store the cgit cache entries.")
684 (nginx-server-configuration-list (list %cgit-configuration-nginx))
685 "NGINX configuration."))
687 (define (cgit-activation config)
688 "Return the activation gexp for CONFIG."
689 (let* ((opaque-config? (opaque-cgit-configuration? config))
692 (opaque-cgit-configuration-cgitrc config)
693 (serialize-cgit-configuration config))))
695 (use-modules (guix build utils))
696 (mkdir-p #$(if opaque-config?
697 (opaque-cgit-configuration-cache-root config)
698 (cgit-configuration-cache-root config)))
699 (copy-file #$(mixed-text-file "cgitrc" config-str)
702 (define (cgit-configuration-nginx-config config)
703 (if (opaque-cgit-configuration? config)
704 (opaque-cgit-configuration-nginx config)
705 (cgit-configuration-nginx config)))
707 (define cgit-service-type
711 (list (service-extension activation-service-type
713 (service-extension nginx-service-type
714 cgit-configuration-nginx-config)
716 ;; Make sure fcgiwrap is instantiated.
717 (service-extension fcgiwrap-service-type
719 (default-value (cgit-configuration))
721 "Run the cgit web interface, which allows users to browse Git
724 (define (generate-cgit-documentation)
725 (generate-documentation
726 `((cgit-configuration
727 ,cgit-configuration-fields
728 (repositories repository-cgit-configuration))
729 (repository-cgit-configuration
730 ,repository-cgit-configuration-fields))
731 'cgit-configuration))