1 { config, lib, pkgs, ... }:
4 inherit (lib) mkBefore mkDefault mkEnableOption mkPackageOption
5 mkIf mkOption mkRemovedOptionModule types;
6 inherit (lib) concatStringsSep literalExpression mapAttrsToList;
7 inherit (lib) optional optionalAttrs optionalString;
9 cfg = config.services.redmine;
10 format = pkgs.formats.yaml {};
11 bundle = "${cfg.package}/share/redmine/bin/bundle";
15 adapter = cfg.database.type;
16 database = if cfg.database.type == "sqlite3" then "${cfg.stateDir}/database.sqlite3" else cfg.database.name;
17 } // optionalAttrs (cfg.database.type != "sqlite3") {
18 host = if (cfg.database.type == "postgresql" && cfg.database.socket != null) then cfg.database.socket else cfg.database.host;
19 port = cfg.database.port;
20 username = cfg.database.user;
21 } // optionalAttrs (cfg.database.type != "sqlite3" && cfg.database.passwordFile != null) {
22 password = "#dbpass#";
23 } // optionalAttrs (cfg.database.type == "mysql2" && cfg.database.socket != null) {
24 socket = cfg.database.socket;
28 databaseYml = format.generate "database.yml" databaseSettings;
30 configurationYml = format.generate "configuration.yml" cfg.settings;
31 additionalEnvironment = pkgs.writeText "additional_environment.rb" cfg.extraEnv;
33 unpackTheme = unpack "theme";
34 unpackPlugin = unpack "plugin";
35 unpack = id: (name: source:
36 pkgs.stdenv.mkDerivation {
37 name = "redmine-${id}-${name}";
38 nativeBuildInputs = [ pkgs.unzip ];
46 mysqlLocal = cfg.database.createLocally && cfg.database.type == "mysql2";
47 pgsqlLocal = cfg.database.createLocally && cfg.database.type == "postgresql";
52 (mkRemovedOptionModule [ "services" "redmine" "extraConfig" ] "Use services.redmine.settings instead.")
53 (mkRemovedOptionModule [ "services" "redmine" "database" "password" ] "Use services.redmine.database.passwordFile instead.")
59 enable = mkEnableOption "Redmine, a project management web application";
61 package = mkPackageOption pkgs "redmine" {
62 example = "redmine.override { ruby = pkgs.ruby_3_2; }";
68 description = "User under which Redmine is ran.";
74 description = "Group under which Redmine is ran.";
80 description = "Port on which Redmine is ran.";
85 default = "/var/lib/redmine";
86 description = "The state directory, logs and plugins are stored here.";
93 Redmine configuration ({file}`configuration.yml`). Refer to
94 <https://guides.rubyonrails.org/action_mailer_basics.html#action-mailer-configuration>
97 example = literalExpression ''
100 delivery_method = "smtp";
102 address = "mail.example.com";
110 extraEnv = mkOption {
114 Extra configuration in additional_environment.rb.
116 See <https://svn.redmine.org/redmine/trunk/config/additional_environment.rb.example>
120 config.logger.level = Logger::DEBUG
125 type = types.attrsOf types.path;
127 description = "Set of themes.";
128 example = literalExpression ''
130 dkuk-redmine_alex_skin = builtins.fetchurl {
131 url = "https://bitbucket.org/dkuk/redmine_alex_skin/get/1842ef675ef3.zip";
132 sha256 = "0hrin9lzyi50k4w2bd2b30vrf1i4fi1c0gyas5801wn8i7kpm9yl";
139 type = types.attrsOf types.path;
141 description = "Set of plugins.";
142 example = literalExpression ''
144 redmine_env_auth = builtins.fetchurl {
145 url = "https://github.com/Intera/redmine_env_auth/archive/0.6.zip";
146 sha256 = "0yyr1yjd8gvvh832wdc8m3xfnhhxzk2pk3gm2psg5w9jdvd6skak";
154 type = types.enum [ "mysql2" "postgresql" "sqlite3" ];
155 example = "postgresql";
157 description = "Database engine to use.";
162 default = "localhost";
163 description = "Database host address.";
168 default = if cfg.database.type == "postgresql" then 5432 else 3306;
169 defaultText = literalExpression "3306";
170 description = "Database host port.";
176 description = "Database name.";
182 description = "Database user.";
185 passwordFile = mkOption {
186 type = types.nullOr types.path;
188 example = "/run/keys/redmine-dbpassword";
190 A file containing the password corresponding to
191 {option}`database.user`.
196 type = types.nullOr types.path;
198 if mysqlLocal then "/run/mysqld/mysqld.sock"
199 else if pgsqlLocal then "/run/postgresql"
201 defaultText = literalExpression "/run/mysqld/mysqld.sock";
202 example = "/run/mysqld/mysqld.sock";
203 description = "Path to the unix socket file to use for authentication.";
206 createLocally = mkOption {
209 description = "Create the database and database user locally.";
214 subversion = mkOption {
217 description = "Subversion integration.";
220 mercurial = mkOption {
223 description = "Mercurial integration.";
229 description = "git integration.";
235 description = "cvs integration.";
241 description = "bazaar integration.";
244 imagemagick = mkOption {
247 description = "Allows exporting Gant diagrams as PNG.";
250 ghostscript = mkOption {
253 description = "Allows exporting Gant diagrams as PDF.";
256 minimagick_font_path = mkOption {
259 description = "MiniMagick font path";
260 example = "/run/current-system/sw/share/X11/fonts/LiberationSans-Regular.ttf";
267 config = mkIf cfg.enable {
270 { assertion = cfg.database.type != "sqlite3" -> cfg.database.passwordFile != null || cfg.database.socket != null;
271 message = "one of services.redmine.database.socket or services.redmine.database.passwordFile must be set";
273 { assertion = cfg.database.createLocally -> cfg.database.user == cfg.user;
274 message = "services.redmine.database.user must be set to ${cfg.user} if services.redmine.database.createLocally is set true";
276 { assertion = pgsqlLocal -> cfg.database.user == cfg.database.name;
277 message = "services.redmine.database.user and services.redmine.database.name must be the same when using a local postgresql database";
279 { assertion = (cfg.database.createLocally && cfg.database.type != "sqlite3") -> cfg.database.socket != null;
280 message = "services.redmine.database.socket must be set if services.redmine.database.createLocally is set to true and no sqlite database is used";
282 { assertion = cfg.database.createLocally -> cfg.database.host == "localhost";
283 message = "services.redmine.database.host must be set to localhost if services.redmine.database.createLocally is set to true";
285 { assertion = cfg.components.imagemagick -> cfg.components.minimagick_font_path != "";
286 message = "services.redmine.components.minimagick_font_path must be configured with a path to a font file if services.redmine.components.imagemagick is set to true.";
290 services.redmine.settings = {
292 scm_subversion_command = optionalString cfg.components.subversion "${pkgs.subversion}/bin/svn";
293 scm_mercurial_command = optionalString cfg.components.mercurial "${pkgs.mercurial}/bin/hg";
294 scm_git_command = optionalString cfg.components.git "${pkgs.git}/bin/git";
295 scm_cvs_command = optionalString cfg.components.cvs "${pkgs.cvs}/bin/cvs";
296 scm_bazaar_command = optionalString cfg.components.breezy "${pkgs.breezy}/bin/bzr";
297 imagemagick_convert_command = optionalString cfg.components.imagemagick "${pkgs.imagemagick}/bin/convert";
298 gs_command = optionalString cfg.components.ghostscript "${pkgs.ghostscript}/bin/gs";
299 minimagick_font_path = "${cfg.components.minimagick_font_path}";
303 services.redmine.extraEnv = mkBefore ''
304 config.logger = Logger.new("${cfg.stateDir}/log/production.log", 14, 1048576)
305 config.logger.level = Logger::INFO
308 services.mysql = mkIf mysqlLocal {
310 package = mkDefault pkgs.mariadb;
311 ensureDatabases = [ cfg.database.name ];
313 { name = cfg.database.user;
314 ensurePermissions = { "${cfg.database.name}.*" = "ALL PRIVILEGES"; };
319 services.postgresql = mkIf pgsqlLocal {
321 ensureDatabases = [ cfg.database.name ];
323 { name = cfg.database.user;
324 ensureDBOwnership = true;
329 # create symlinks for the basic directory layout the redmine package expects
330 systemd.tmpfiles.rules = [
331 "d '${cfg.stateDir}' 0750 ${cfg.user} ${cfg.group} - -"
332 "d '${cfg.stateDir}/cache' 0750 ${cfg.user} ${cfg.group} - -"
333 "d '${cfg.stateDir}/config' 0750 ${cfg.user} ${cfg.group} - -"
334 "d '${cfg.stateDir}/files' 0750 ${cfg.user} ${cfg.group} - -"
335 "d '${cfg.stateDir}/log' 0750 ${cfg.user} ${cfg.group} - -"
336 "d '${cfg.stateDir}/plugins' 0750 ${cfg.user} ${cfg.group} - -"
337 "d '${cfg.stateDir}/public' 0750 ${cfg.user} ${cfg.group} - -"
338 "d '${cfg.stateDir}/public/plugin_assets' 0750 ${cfg.user} ${cfg.group} - -"
339 "d '${cfg.stateDir}/public/themes' 0750 ${cfg.user} ${cfg.group} - -"
340 "d '${cfg.stateDir}/tmp' 0750 ${cfg.user} ${cfg.group} - -"
342 "d /run/redmine - - - - -"
343 "d /run/redmine/public - - - - -"
344 "L+ /run/redmine/config - - - - ${cfg.stateDir}/config"
345 "L+ /run/redmine/files - - - - ${cfg.stateDir}/files"
346 "L+ /run/redmine/log - - - - ${cfg.stateDir}/log"
347 "L+ /run/redmine/plugins - - - - ${cfg.stateDir}/plugins"
348 "L+ /run/redmine/public/plugin_assets - - - - ${cfg.stateDir}/public/plugin_assets"
349 "L+ /run/redmine/public/themes - - - - ${cfg.stateDir}/public/themes"
350 "L+ /run/redmine/tmp - - - - ${cfg.stateDir}/tmp"
353 systemd.services.redmine = {
354 after = [ "network.target" ] ++ optional mysqlLocal "mysql.service" ++ optional pgsqlLocal "postgresql.service";
355 wantedBy = [ "multi-user.target" ];
356 environment.RAILS_ENV = "production";
357 environment.RAILS_CACHE = "${cfg.stateDir}/cache";
358 environment.REDMINE_LANG = "en";
359 environment.SCHEMA = "${cfg.stateDir}/cache/schema.db";
362 ++ optional cfg.components.subversion subversion
363 ++ optional cfg.components.mercurial mercurial
364 ++ optional cfg.components.git git
365 ++ optional cfg.components.cvs cvs
366 ++ optional cfg.components.breezy breezy
367 ++ optional cfg.components.imagemagick imagemagick
368 ++ optional cfg.components.ghostscript ghostscript;
371 rm -rf "${cfg.stateDir}/plugins/"*
372 rm -rf "${cfg.stateDir}/public/themes/"*
374 # start with a fresh config directory
375 # the config directory is copied instead of linked as some mutable data is stored in there
376 find "${cfg.stateDir}/config" ! -name "secret_token.rb" -type f -exec rm -f {} +
377 cp -r ${cfg.package}/share/redmine/config.dist/* "${cfg.stateDir}/config/"
379 chmod -R u+w "${cfg.stateDir}/config"
381 # link in the application configuration
382 ln -fs ${configurationYml} "${cfg.stateDir}/config/configuration.yml"
384 # link in the additional environment configuration
385 ln -fs ${additionalEnvironment} "${cfg.stateDir}/config/additional_environment.rb"
388 # link in all user specified themes
389 for theme in ${concatStringsSep " " (mapAttrsToList unpackTheme cfg.themes)}; do
390 ln -fs $theme/* "${cfg.stateDir}/public/themes"
393 # link in redmine provided themes
394 ln -sf ${cfg.package}/share/redmine/public/themes.dist/* "${cfg.stateDir}/public/themes/"
397 # link in all user specified plugins
398 for plugin in ${concatStringsSep " " (mapAttrsToList unpackPlugin cfg.plugins)}; do
399 ln -fs $plugin/* "${cfg.stateDir}/plugins/''${plugin##*-redmine-plugin-}"
403 # handle database.passwordFile & permissions
404 cp -f ${databaseYml} "${cfg.stateDir}/config/database.yml"
406 ${optionalString ((cfg.database.type != "sqlite3") && (cfg.database.passwordFile != null)) ''
407 DBPASS="$(head -n1 ${cfg.database.passwordFile})"
408 sed -e "s,#dbpass#,$DBPASS,g" -i "${cfg.stateDir}/config/database.yml"
411 chmod 440 "${cfg.stateDir}/config/database.yml"
414 # generate a secret token if required
415 if ! test -e "${cfg.stateDir}/config/initializers/secret_token.rb"; then
416 ${bundle} exec rake generate_secret_token
417 chmod 440 "${cfg.stateDir}/config/initializers/secret_token.rb"
420 # execute redmine required commands prior to starting the application
421 ${bundle} exec rake db:migrate
422 ${bundle} exec rake redmine:plugins:migrate
423 ${bundle} exec rake redmine:load_default_data
431 WorkingDirectory = "${cfg.package}/share/redmine";
432 ExecStart="${bundle} exec rails server -u webrick -e production -p ${toString cfg.port} -P '${cfg.stateDir}/redmine.pid'";
437 users.users = optionalAttrs (cfg.user == "redmine") {
441 uid = config.ids.uids.redmine;
445 users.groups = optionalAttrs (cfg.group == "redmine") {
446 redmine.gid = config.ids.gids.redmine;
451 meta.maintainers = with lib.maintainers; [ felixsinger ];