@@ -0,0 +1,659 @@
1+{ config, lib, pkgs, ... }:
2+
3+with lib;
4+
5+let
6+
7+ cfg = config.services.gitit;
8+
9+ homeDir = "/var/lib/gitit";
10+
11+ gititShared = with cfg.haskellPackages; gitit + "/share/" + pkgs.stdenv.system + "-" + ghc.name + "/" + gitit.pname + "-" + gitit.version;
12+
13+ gititWithPkgs = hsPkgs: extras: hsPkgs.ghcWithPackages (self: with self; [ gitit ] ++ (extras self));
14+
15+ gititSh = hsPkgs: extras: with pkgs; let
16+ env = gititWithPkgs hsPkgs extras;
17+ in writeScript "gitit" ''
18+ #!${stdenv.shell}
19+ cd $HOME
20+ export PATH="${makeSearchPath "bin" (
21+ [ git curl ] ++ (if cfg.pdfExport == "yes" then [texLiveFull] else [])
22+ )}:$PATH";
23+ export NIX_GHC="${env}/bin/ghc"
24+ export NIX_GHCPKG="${env}/bin/ghc-pkg"
25+ export NIX_GHC_DOCDIR="${env}/share/doc/ghc/html"
26+ export NIX_GHC_LIBDIR=$( $NIX_GHC --print-libdir )
27+ ${env}/bin/gitit -f ${configFile}
28+ '';
29+
30+ gititOptions = let
31+
32+ yesNo = types.enum [ "yes" "no" ];
33+
34+ in {
35+
36+ enable = mkOption {
37+ type = types.bool;
38+ default = false;
39+ description = "Enable the gitit service.";
40+ };
41+
42+ haskellPackages = mkOption {
43+ default = pkgs.haskellPackages;
44+ defaultText = "pkgs.haskellPackages";
45+ example = literalExample "pkgs.haskell.packages.ghc784";
46+ description = "haskellPackages used to build gitit and plugins.";
47+ };
48+
49+ extraPackages = mkOption {
50+ default = self: [];
51+ example = literalExample ''
52+ haskellPackages: [
53+ haskellPackages.wreq
54+ ]
55+ '';
56+ description = ''
57+ Extra packages available to ghc when running gitit. The
58+ value must be a function which receives the attrset defined
59+ in <varname>haskellPackages</varname> as the sole argument.
60+ '';
61+ };
62+
63+ address = mkOption {
64+ type = types.str;
65+ default = "0.0.0.0";
66+ description = "IP address on which the web server will listen.";
67+ };
68+
69+ port = mkOption {
70+ type = types.int;
71+ default = 5001;
72+ description = "Port on which the web server will run.";
73+ };
74+
75+ wikiTitle = mkOption {
76+ type = types.str;
77+ default = "Gitit!";
78+ description = "The wiki title.";
79+ };
80+
81+ repositoryType = mkOption {
82+ type = types.enum ["git" "darcs" "mercurial"];
83+ default = "git";
84+ description = "Specifies the type of repository used for wiki content.";
85+ };
86+
87+ repositoryPath = mkOption {
88+ type = types.path;
89+ default = homeDir + "/wiki";
90+ description = ''
91+ Specifies the path of the repository directory. If it does not
92+ exist, gitit will create it on startup.
93+ '';
94+ };
95+
96+ requireAuthentication = mkOption {
97+ type = types.enum [ "none" "modify" "read" ];
98+ default = "modify";
99+ description = ''
100+ If 'none', login is never required, and pages can be edited
101+ anonymously. If 'modify', login is required to modify the wiki
102+ (edit, add, delete pages, upload files). If 'read', login is
103+ required to see any wiki pages.
104+ '';
105+ };
106+
107+ authenticationMethod = mkOption {
108+ type = types.enum [ "form" "http" "generic"];
109+ default = "form";
110+ description = ''
111+ 'form' means that users will be logged in and registered using forms
112+ in the gitit web interface. 'http' means that gitit will assume that
113+ HTTP authentication is in place and take the logged in username from
114+ the "Authorization" field of the HTTP request header (in addition,
115+ the login/logout and registration links will be suppressed).
116+ 'generic' means that gitit will assume that some form of
117+ authentication is in place that directly sets REMOTE_USER to the name
118+ of the authenticated user (e.g. mod_auth_cas on apache). 'rpx' means
119+ that gitit will attempt to log in through https://rpxnow.com. This
120+ requires that 'rpx-domain', 'rpx-key', and 'base-url' be set below,
121+ and that 'curl' be in the system path.
122+ '';
123+ };
124+
125+ userFile = mkOption {
126+ type = types.path;
127+ default = homeDir + "/gitit-users";
128+ description = ''
129+ Specifies the path of the file containing user login information. If
130+ it does not exist, gitit will create it (with an empty user list).
131+ This file is not used if 'http' is selected for
132+ authentication-method.
133+ '';
134+ };
135+
136+ sessionTimeout = mkOption {
137+ type = types.int;
138+ default = 60;
139+ description = ''
140+ Number of minutes of inactivity before a session expires.
141+ '';
142+ };
143+
144+ staticDir = mkOption {
145+ type = types.path;
146+ default = gititShared + "/data/static";
147+ description = ''
148+ Specifies the path of the static directory (containing javascript,
149+ css, and images). If it does not exist, gitit will create it and
150+ populate it with required scripts, stylesheets, and images.
151+ '';
152+ };
153+
154+ defaultPageType = mkOption {
155+ type = types.enum [ "markdown" "rst" "latex" "html" "markdown+lhs" "rst+lhs" "latex+lhs" ];
156+ default = "markdown";
157+ description = ''
158+ Specifies the type of markup used to interpret pages in the wiki.
159+ Possible values are markdown, rst, latex, html, markdown+lhs,
160+ rst+lhs, and latex+lhs. (the +lhs variants treat the input as
161+ literate Haskell. See pandoc's documentation for more details.) If
162+ Markdown is selected, pandoc's syntax extensions (for footnotes,
163+ delimited code blocks, etc.) will be enabled. Note that pandoc's
164+ restructuredtext parser is not complete, so some pages may not be
165+ rendered correctly if rst is selected. The same goes for latex and
166+ html.
167+ '';
168+ };
169+
170+ math = mkOption {
171+ type = types.enum [ "mathml" "raw" "mathjax" "jsmath" "google" ];
172+ default = "mathml";
173+ description = ''
174+ Specifies how LaTeX math is to be displayed. Possible values are
175+ mathml, raw, mathjax, jsmath, and google. If mathml is selected,
176+ gitit will convert LaTeX math to MathML and link in a script,
177+ MathMLinHTML.js, that allows the MathML to be seen in Gecko browsers,
178+ IE + mathplayer, and Opera. In other browsers you may get a jumble of
179+ characters. If raw is selected, the LaTeX math will be displayed as
180+ raw LaTeX math. If mathjax is selected, gitit will link to the
181+ remote mathjax script. If jsMath is selected, gitit will link to the
182+ script /js/jsMath/easy/load.js, and will assume that jsMath has been
183+ installed into the js/jsMath directory. This is the most portable
184+ solution. If google is selected, the google chart API is called to
185+ render the formula as an image. This requires a connection to google,
186+ and might raise a technical or a privacy problem.
187+ '';
188+ };
189+
190+ mathJaxScript = mkOption {
191+ type = types.str;
192+ default = "https://d3eoax9i5htok0.cloudfront.net/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML";
193+ description = ''
194+ Specifies the path to MathJax rendering script. You might want to
195+ use your own MathJax script to render formulas without Internet
196+ connection or if you want to use some special LaTeX packages. Note:
197+ path specified there cannot be an absolute path to a script on your
198+ hdd, instead you should run your (local if you wish) HTTP server
199+ which will serve the MathJax.js script. You can easily (in four lines
200+ of code) serve MathJax.js using
201+ http://happstack.com/docs/crashcourse/FileServing.html Do not forget
202+ the "http://" prefix (e.g. http://localhost:1234/MathJax.js).
203+ '';
204+ };
205+
206+ showLhsBirdTracks = mkOption {
207+ type = yesNo;
208+ default = "no";
209+ description = ''
210+ Specifies whether to show Haskell code blocks in "bird style", with
211+ "> " at the beginning of each line.
212+ '';
213+ };
214+
215+ templatesDir = mkOption {
216+ type = types.path;
217+ default = gititShared + "/data/templates";
218+ description = ''
219+ Specifies the path of the directory containing page templates. If it
220+ does not exist, gitit will create it with default templates. Users
221+ may wish to edit the templates to customize the appearance of their
222+ wiki. The template files are HStringTemplate templates. Variables to
223+ be interpolated appear between $\'s. Literal $\'s must be
224+ backslash-escaped.
225+ '';
226+ };
227+
228+ logFile = mkOption {
229+ type = types.path;
230+ default = homeDir + "/gitit.log";
231+ description = ''
232+ Specifies the path of gitit's log file. If it does not exist, gitit
233+ will create it. The log is in Apache combined log format.
234+ '';
235+ };
236+
237+ logLevel = mkOption {
238+ type = types.enum [ "DEBUG" "INFO" "NOTICE" "WARNING" "ERROR" "CRITICAL" "ALERT" "EMERGENCY" ];
239+ default = "ERROR";
240+ description = ''
241+ Determines how much information is logged. Possible values (from
242+ most to least verbose) are DEBUG, INFO, NOTICE, WARNING, ERROR,
243+ CRITICAL, ALERT, EMERGENCY.
244+ '';
245+ };
246+
247+ frontPage = mkOption {
248+ type = types.str;
249+ default = "Front Page";
250+ description = ''
251+ Specifies which wiki page is to be used as the wiki's front page.
252+ Gitit creates a default front page on startup, if one does not exist
253+ already.
254+ '';
255+ };
256+
257+ noDelete = mkOption {
258+ type = types.str;
259+ default = "Front Page, Help";
260+ description = ''
261+ Specifies pages that cannot be deleted through the web interface.
262+ (They can still be deleted directly using git or darcs.) A
263+ comma-separated list of page names. Leave blank to allow every page
264+ to be deleted.
265+ '';
266+ };
267+
268+ noEdit = mkOption {
269+ type = types.str;
270+ default = "Help";
271+ description = ''
272+ Specifies pages that cannot be edited through the web interface.
273+ Leave blank to allow every page to be edited.
274+ '';
275+ };
276+
277+ defaultSummary = mkOption {
278+ type = types.str;
279+ default = "";
280+ description = ''
281+ Specifies text to be used in the change description if the author
282+ leaves the "description" field blank. If default-summary is blank
283+ (the default), the author will be required to fill in the description
284+ field.
285+ '';
286+ };
287+
288+ tableOfContents = mkOption {
289+ type = yesNo;
290+ default = "yes";
291+ description = ''
292+ Specifies whether to print a tables of contents (with links to
293+ sections) on each wiki page.
294+ '';
295+ };
296+
297+ plugins = mkOption {
298+ type = types.path;
299+ default = gititShared + "/plugins/Dot.hs";
300+ description = ''
301+ Specifies a list of plugins to load. Plugins may be specified either
302+ by their path or by their module name. If the plugin name starts
303+ with Gitit.Plugin., gitit will assume that the plugin is an installed
304+ module and will not try to find a source file.
305+ Examples:
306+ plugins: plugins/DotPlugin.hs, CapitalizeEmphasisPlugin.hs
307+ plugins: plugins/DotPlugin
308+ plugins: Gitit.Plugin.InterwikiLinks
309+ '';
310+ };
311+
312+ useCache = mkOption {
313+ type = yesNo;
314+ default = "no";
315+ description = ''
316+ Specifies whether to cache rendered pages. Note that if use-feed is
317+ selected, feeds will be cached regardless of the value of use-cache.
318+ '';
319+ };
320+
321+ cacheDir = mkOption {
322+ type = types.path;
323+ default = homeDir + "/cache";
324+ description = "Path where rendered pages will be cached.";
325+ };
326+
327+ maxUploadSize = mkOption {
328+ type = types.str;
329+ default = "1000K";
330+ description = ''
331+ Specifies an upper limit on the size (in bytes) of files uploaded
332+ through the wiki's web interface. To disable uploads, set this to
333+ 0K. This will result in the uploads link disappearing and the
334+ _upload url becoming inactive.
335+ '';
336+ };
337+
338+ maxPageSize = mkOption {
339+ type = types.str;
340+ default = "1000K";
341+ description = "Specifies an upper limit on the size (in bytes) of pages.";
342+ };
343+
344+ debugMode = mkOption {
345+ type = yesNo;
346+ default = "no";
347+ description = "Causes debug information to be logged while gitit is running.";
348+ };
349+
350+ compressResponses = mkOption {
351+ type = yesNo;
352+ default = "yes";
353+ description = "Specifies whether HTTP responses should be compressed.";
354+ };
355+
356+ mimeTypesFile = mkOption {
357+ type = types.path;
358+ default = "/etc/mime/types.info";
359+ description = ''
360+ Specifies the path of a file containing mime type mappings. Each
361+ line of the file should contain two fields, separated by whitespace.
362+ The first field is the mime type, the second is a file extension.
363+ For example:
364+ video/x-ms-wmx wmx
365+ If the file is not found, some simple defaults will be used.
366+ '';
367+ };
368+
369+ useReCaptcha = mkOption {
370+ type = yesNo;
371+ default = "no";
372+ description = ''
373+ If "yes", causes gitit to use the reCAPTCHA service
374+ (http://recaptcha.net) to prevent bots from creating accounts.
375+ '';
376+ };
377+
378+ reCaptchaPrivateKey = mkOption {
379+ type = with types; nullOr str;
380+ default = null;
381+ description = ''
382+ Specifies the private key for the reCAPTCHA service. To get
383+ these, you need to create an account at http://recaptcha.net.
384+ '';
385+ };
386+
387+ reCaptchaPublicKey = mkOption {
388+ type = with types; nullOr str;
389+ default = null;
390+ description = ''
391+ Specifies the public key for the reCAPTCHA service. To get
392+ these, you need to create an account at http://recaptcha.net.
393+ '';
394+ };
395+
396+ accessQuestion = mkOption {
397+ type = types.str;
398+ default = "What is the code given to you by Ms. X?";
399+ description = ''
400+ Specifies a question that users must answer when they attempt to
401+ create an account
402+ '';
403+ };
404+
405+ accessQuestionAnswers = mkOption {
406+ type = types.str;
407+ default = "RED DOG, red dog";
408+ description = ''
409+ Specifies a question that users must answer when they attempt to
410+ create an account, along with a comma-separated list of acceptable
411+ answers. This can be used to institute a rudimentary password for
412+ signing up as a user on the wiki, or as an alternative to reCAPTCHA.
413+ Example:
414+ access-question: What is the code given to you by Ms. X?
415+ access-question-answers: RED DOG, red dog
416+ '';
417+ };
418+
419+ rpxDomain = mkOption {
420+ type = with types; nullOr str;
421+ default = null;
422+ description = ''
423+ Specifies the domain and key of your RPX account. The domain is just
424+ the prefix of the complete RPX domain, so if your full domain is
425+ 'https://foo.rpxnow.com/', use 'foo' as the value of rpx-domain.
426+ '';
427+ };
428+
429+ rpxKey = mkOption {
430+ type = with types; nullOr str;
431+ default = null;
432+ description = "RPX account access key.";
433+ };
434+
435+ mailCommand = mkOption {
436+ type = types.str;
437+ default = "sendmail %s";
438+ description = ''
439+ Specifies the command to use to send notification emails. '%s' will
440+ be replaced by the destination email address. The body of the
441+ message will be read from stdin. If this field is left blank,
442+ password reset will not be offered.
443+ '';
444+ };
445+
446+ resetPasswordMessage = mkOption {
447+ type = types.lines;
448+ default = ''
449+ > From: gitit@$hostname$
450+ > To: $useremail$
451+ > Subject: Wiki password reset
452+ >
453+ > Hello $username$,
454+ >
455+ > To reset your password, please follow the link below:
456+ > http://$hostname$:$port$$resetlink$
457+ >
458+ > Regards
459+ '';
460+ description = ''
461+ Gives the text of the message that will be sent to the user should
462+ she want to reset her password, or change other registration info.
463+ The lines must be indented, and must begin with '>'. The initial
464+ spaces and '> ' will be stripped off. $username$ will be replaced by
465+ the user's username, $useremail$ by her email address, $hostname$ by
466+ the hostname on which the wiki is running (as returned by the
467+ hostname system call), $port$ by the port on which the wiki is
468+ running, and $resetlink$ by the relative path of a reset link derived
469+ from the user's existing hashed password. If your gitit wiki is being
470+ proxied to a location other than the root path of $port$, you should
471+ change the link to reflect this: for example, to
472+ http://$hostname$/path/to/wiki$resetlink$ or
473+ http://gitit.$hostname$$resetlink$
474+ '';
475+ };
476+
477+ useFeed = mkOption {
478+ type = yesNo;
479+ default = "no";
480+ description = ''
481+ Specifies whether an ATOM feed should be enabled (for the site and
482+ for individual pages).
483+ '';
484+ };
485+
486+ baseUrl = mkOption {
487+ type = with types; nullOr str;
488+ default = null;
489+ description = ''
490+ The base URL of the wiki, to be used in constructing feed IDs and RPX
491+ token_urls. Set this if use-feed is 'yes' or authentication-method
492+ is 'rpx'.
493+ '';
494+ };
495+
496+ absoluteUrls = mkOption {
497+ type = yesNo;
498+ default = "no";
499+ description = ''
500+ Make wikilinks absolute with respect to the base-url. So, for
501+ example, in a wiki served at the base URL '/wiki', on a page
502+ Sub/Page, the wikilink '[Cactus]()' will produce a link to
503+ '/wiki/Cactus' if absolute-urls is 'yes', and a relative link to
504+ 'Cactus' (referring to '/wiki/Sub/Cactus') if absolute-urls is 'no'.
505+ '';
506+ };
507+
508+ feedDays = mkOption {
509+ type = types.int;
510+ default = 14;
511+ description = "Number of days to be included in feeds.";
512+ };
513+
514+ feedRefreshTime = mkOption {
515+ type = types.int;
516+ default = 60;
517+ description = "Number of minutes to cache feeds before refreshing.";
518+ };
519+
520+ pdfExport = mkOption {
521+ type = yesNo;
522+ default = "no";
523+ description = ''
524+ If yes, PDF will appear in export options. PDF will be created using
525+ pdflatex, which must be installed and in the path. Note that PDF
526+ exports create significant additional server load.
527+ '';
528+ };
529+
530+ pandocUserData = mkOption {
531+ type = with types; nullOr path;
532+ default = null;
533+ description = ''
534+ If a directory is specified, this will be searched for pandoc
535+ customizations. These can include a templates/ directory for custom
536+ templates for various export formats, an S5 directory for custom S5
537+ styles, and a reference.odt for ODT exports. If no directory is
538+ specified, $HOME/.pandoc will be searched. See pandoc's README for
539+ more information.
540+ '';
541+ };
542+
543+ xssSanitize = mkOption {
544+ type = yesNo;
545+ default = "yes";
546+ description = ''
547+ If yes, all HTML (including that produced by pandoc) is filtered
548+ through xss-sanitize. Set to no only if you trust all of your users.
549+ '';
550+ };
551+ };
552+
553+ configFile = pkgs.writeText "gitit.conf" ''
554+ address: ${cfg.address}
555+ port: ${toString cfg.port}
556+ wiki-title: ${cfg.wikiTitle}
557+ repository-type: ${cfg.repositoryType}
558+ repository-path: ${cfg.repositoryPath}
559+ require-authentication: ${cfg.requireAuthentication}
560+ authentication-method: ${cfg.authenticationMethod}
561+ user-file: ${cfg.userFile}
562+ session-timeout: ${toString cfg.sessionTimeout}
563+ static-dir: ${cfg.staticDir}
564+ default-page-type: ${cfg.defaultPageType}
565+ math: ${cfg.math}
566+ mathjax-script: ${cfg.mathJaxScript}
567+ show-lhs-bird-tracks: ${cfg.showLhsBirdTracks}
568+ templates-dir: ${cfg.templatesDir}
569+ log-file: ${cfg.logFile}
570+ log-level: ${cfg.logLevel}
571+ front-page: ${cfg.frontPage}
572+ no-delete: ${cfg.noDelete}
573+ no-edit: ${cfg.noEdit}
574+ default-summary: ${cfg.defaultSummary}
575+ table-of-contents: ${cfg.tableOfContents}
576+ plugins: ${cfg.plugins}
577+ use-cache: ${cfg.useCache}
578+ cache-dir: ${cfg.cacheDir}
579+ max-upload-size: ${cfg.maxUploadSize}
580+ max-page-size: ${cfg.maxPageSize}
581+ debug-mode: ${cfg.debugMode}
582+ compress-responses: ${cfg.compressResponses}
583+ mime-types-file: ${cfg.mimeTypesFile}
584+ use-recaptcha: ${cfg.useReCaptcha}
585+ recaptcha-private-key: ${toString cfg.reCaptchaPrivateKey}
586+ recaptcha-public-key: ${toString cfg.reCaptchaPublicKey}
587+ access-question: ${cfg.accessQuestion}
588+ access-question-answers: ${cfg.accessQuestionAnswers}
589+ rpx-domain: ${toString cfg.rpxDomain}
590+ rpx-key: ${toString cfg.rpxKey}
591+ mail-command: ${cfg.mailCommand}
592+ reset-password-message: ${cfg.resetPasswordMessage}
593+ use-feed: ${cfg.useFeed}
594+ base-url: ${toString cfg.baseUrl}
595+ absolute-urls: ${cfg.absoluteUrls}
596+ feed-days: ${toString cfg.feedDays}
597+ feed-refresh-time: ${toString cfg.feedRefreshTime}
598+ pdf-export: ${cfg.pdfExport}
599+ pandoc-user-data: ${toString cfg.pandocUserData}
600+ xss-sanitize: ${cfg.xssSanitize}
601+ '';
602+
603+in
604+
605+{
606+
607+ options.services.gitit = gititOptions;
608+
609+ config = mkIf cfg.enable {
610+
611+ users.extraUsers.gitit = {
612+ group = config.users.extraGroups.gitit.name;
613+ description = "Gitit user";
614+ home = homeDir;
615+ createHome = true;
616+ uid = config.ids.uids.gitit;
617+ };
618+
619+ users.extraGroups.gitit.gid = config.ids.gids.gitit;
620+
621+ systemd.services.gitit = let
622+ uid = toString config.ids.uids.gitit;
623+ gid = toString config.ids.gids.gitit;
624+ in {
625+ description = "Git and Pandoc Powered Wiki";
626+ after = [ "network.target" ];
627+ wantedBy = [ "multi-user.target" ];
628+
629+ preStart = with cfg; ''
630+ chown ${uid}:${gid} -R ${homeDir}
631+ for dir in ${repositoryPath} ${staticDir} ${templatesDir} ${cacheDir}
632+ do
633+ if [ ! -d $dir ]
634+ then
635+ mkdir -p $dir
636+ find $dir -type d -exec chmod 0750 {} +
637+ find $dir -type f -exec chmod 0640 {} +
638+ fi
639+ done
640+ cd ${repositoryPath}
641+ if [ ! -d .git ]
642+ then
643+ ${pkgs.git}/bin/git init
644+ ${pkgs.git}/bin/git config user.email "gitit@${config.networking.hostName}"
645+ ${pkgs.git}/bin/git config user.name "gitit"
646+ chown ${uid}:${gid} -R {repositoryPath}
647+ fi
648+ cd -
649+ '';
650+
651+ serviceConfig = {
652+ User = config.users.extraUsers.gitit.name;
653+ Group = config.users.extraGroups.gitit.name;
654+ ExecStart = with cfg; gititSh haskellPackages extraPackages;
655+ };
656+ };
657+ };
658+}
659+