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 = "IP address Redmine should bind to.";
86 description = "Port on which Redmine is ran.";
91 default = "/var/lib/redmine";
92 description = "The state directory, logs and plugins are stored here.";
99 Redmine configuration ({file}`configuration.yml`). Refer to
100 <https://guides.rubyonrails.org/action_mailer_basics.html#action-mailer-configuration>
103 example = literalExpression ''
106 delivery_method = "smtp";
108 address = "mail.example.com";
116 extraEnv = mkOption {
120 Extra configuration in additional_environment.rb.
122 See <https://svn.redmine.org/redmine/trunk/config/additional_environment.rb.example>
126 config.logger.level = Logger::DEBUG
131 type = types.attrsOf types.path;
133 description = "Set of themes.";
134 example = literalExpression ''
136 dkuk-redmine_alex_skin = builtins.fetchurl {
137 url = "https://bitbucket.org/dkuk/redmine_alex_skin/get/1842ef675ef3.zip";
138 sha256 = "0hrin9lzyi50k4w2bd2b30vrf1i4fi1c0gyas5801wn8i7kpm9yl";
145 type = types.attrsOf types.path;
147 description = "Set of plugins.";
148 example = literalExpression ''
150 redmine_env_auth = builtins.fetchurl {
151 url = "https://github.com/Intera/redmine_env_auth/archive/0.6.zip";
152 sha256 = "0yyr1yjd8gvvh832wdc8m3xfnhhxzk2pk3gm2psg5w9jdvd6skak";
160 type = types.enum [ "mysql2" "postgresql" "sqlite3" ];
161 example = "postgresql";
163 description = "Database engine to use.";
168 default = "localhost";
169 description = "Database host address.";
174 default = if cfg.database.type == "postgresql" then 5432 else 3306;
175 defaultText = literalExpression "3306";
176 description = "Database host port.";
182 description = "Database name.";
188 description = "Database user.";
191 passwordFile = mkOption {
192 type = types.nullOr types.path;
194 example = "/run/keys/redmine-dbpassword";
196 A file containing the password corresponding to
197 {option}`database.user`.
202 type = types.nullOr types.path;
204 if mysqlLocal then "/run/mysqld/mysqld.sock"
205 else if pgsqlLocal then "/run/postgresql"
207 defaultText = literalExpression "/run/mysqld/mysqld.sock";
208 example = "/run/mysqld/mysqld.sock";
209 description = "Path to the unix socket file to use for authentication.";
212 createLocally = mkOption {
215 description = "Create the database and database user locally.";
220 subversion = mkOption {
223 description = "Subversion integration.";
226 mercurial = mkOption {
229 description = "Mercurial integration.";
235 description = "git integration.";
241 description = "cvs integration.";
247 description = "bazaar integration.";
250 imagemagick = mkOption {
253 description = "Allows exporting Gant diagrams as PNG.";
256 ghostscript = mkOption {
259 description = "Allows exporting Gant diagrams as PDF.";
262 minimagick_font_path = mkOption {
265 description = "MiniMagick font path";
266 example = "/run/current-system/sw/share/X11/fonts/LiberationSans-Regular.ttf";
273 config = mkIf cfg.enable {
276 { assertion = cfg.database.type != "sqlite3" -> cfg.database.passwordFile != null || cfg.database.socket != null;
277 message = "one of services.redmine.database.socket or services.redmine.database.passwordFile must be set";
279 { assertion = cfg.database.createLocally -> cfg.database.user == cfg.user;
280 message = "services.redmine.database.user must be set to ${cfg.user} if services.redmine.database.createLocally is set true";
282 { assertion = pgsqlLocal -> cfg.database.user == cfg.database.name;
283 message = "services.redmine.database.user and services.redmine.database.name must be the same when using a local postgresql database";
285 { assertion = (cfg.database.createLocally && cfg.database.type != "sqlite3") -> cfg.database.socket != null;
286 message = "services.redmine.database.socket must be set if services.redmine.database.createLocally is set to true and no sqlite database is used";
288 { assertion = cfg.database.createLocally -> cfg.database.host == "localhost";
289 message = "services.redmine.database.host must be set to localhost if services.redmine.database.createLocally is set to true";
291 { assertion = cfg.components.imagemagick -> cfg.components.minimagick_font_path != "";
292 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.";
296 services.redmine.settings = {
298 scm_subversion_command = optionalString cfg.components.subversion "${pkgs.subversion}/bin/svn";
299 scm_mercurial_command = optionalString cfg.components.mercurial "${pkgs.mercurial}/bin/hg";
300 scm_git_command = optionalString cfg.components.git "${pkgs.git}/bin/git";
301 scm_cvs_command = optionalString cfg.components.cvs "${pkgs.cvs}/bin/cvs";
302 scm_bazaar_command = optionalString cfg.components.breezy "${pkgs.breezy}/bin/bzr";
303 imagemagick_convert_command = optionalString cfg.components.imagemagick "${pkgs.imagemagick}/bin/convert";
304 gs_command = optionalString cfg.components.ghostscript "${pkgs.ghostscript}/bin/gs";
305 minimagick_font_path = "${cfg.components.minimagick_font_path}";
309 services.redmine.extraEnv = mkBefore ''
310 config.logger = Logger.new("${cfg.stateDir}/log/production.log", 14, 1048576)
311 config.logger.level = Logger::INFO
314 services.mysql = mkIf mysqlLocal {
316 package = mkDefault pkgs.mariadb;
317 ensureDatabases = [ cfg.database.name ];
319 { name = cfg.database.user;
320 ensurePermissions = { "${cfg.database.name}.*" = "ALL PRIVILEGES"; };
325 services.postgresql = mkIf pgsqlLocal {
327 ensureDatabases = [ cfg.database.name ];
329 { name = cfg.database.user;
330 ensureDBOwnership = true;
335 # create symlinks for the basic directory layout the redmine package expects
336 systemd.tmpfiles.rules = [
337 "d '${cfg.stateDir}' 0750 ${cfg.user} ${cfg.group} - -"
338 "d '${cfg.stateDir}/cache' 0750 ${cfg.user} ${cfg.group} - -"
339 "d '${cfg.stateDir}/config' 0750 ${cfg.user} ${cfg.group} - -"
340 "d '${cfg.stateDir}/files' 0750 ${cfg.user} ${cfg.group} - -"
341 "d '${cfg.stateDir}/log' 0750 ${cfg.user} ${cfg.group} - -"
342 "d '${cfg.stateDir}/plugins' 0750 ${cfg.user} ${cfg.group} - -"
343 "d '${cfg.stateDir}/public' 0750 ${cfg.user} ${cfg.group} - -"
344 "d '${cfg.stateDir}/public/plugin_assets' 0750 ${cfg.user} ${cfg.group} - -"
345 "d '${cfg.stateDir}/public/themes' 0750 ${cfg.user} ${cfg.group} - -"
346 "d '${cfg.stateDir}/tmp' 0750 ${cfg.user} ${cfg.group} - -"
348 "d /run/redmine - - - - -"
349 "d /run/redmine/public - - - - -"
350 "L+ /run/redmine/config - - - - ${cfg.stateDir}/config"
351 "L+ /run/redmine/files - - - - ${cfg.stateDir}/files"
352 "L+ /run/redmine/log - - - - ${cfg.stateDir}/log"
353 "L+ /run/redmine/plugins - - - - ${cfg.stateDir}/plugins"
354 "L+ /run/redmine/public/plugin_assets - - - - ${cfg.stateDir}/public/plugin_assets"
355 "L+ /run/redmine/public/themes - - - - ${cfg.stateDir}/public/themes"
356 "L+ /run/redmine/tmp - - - - ${cfg.stateDir}/tmp"
359 systemd.services.redmine = {
360 after = [ "network.target" ] ++ optional mysqlLocal "mysql.service" ++ optional pgsqlLocal "postgresql.service";
361 wantedBy = [ "multi-user.target" ];
362 environment.RAILS_ENV = "production";
363 environment.RAILS_CACHE = "${cfg.stateDir}/cache";
364 environment.REDMINE_LANG = "en";
365 environment.SCHEMA = "${cfg.stateDir}/cache/schema.db";
368 ++ optional cfg.components.subversion subversion
369 ++ optional cfg.components.mercurial mercurial
370 ++ optional cfg.components.git git
371 ++ optional cfg.components.cvs cvs
372 ++ optional cfg.components.breezy breezy
373 ++ optional cfg.components.imagemagick imagemagick
374 ++ optional cfg.components.ghostscript ghostscript;
377 rm -rf "${cfg.stateDir}/plugins/"*
378 rm -rf "${cfg.stateDir}/public/themes/"*
380 # start with a fresh config directory
381 # the config directory is copied instead of linked as some mutable data is stored in there
382 find "${cfg.stateDir}/config" ! -name "secret_token.rb" -type f -exec rm -f {} +
383 cp -r ${cfg.package}/share/redmine/config.dist/* "${cfg.stateDir}/config/"
385 chmod -R u+w "${cfg.stateDir}/config"
387 # link in the application configuration
388 ln -fs ${configurationYml} "${cfg.stateDir}/config/configuration.yml"
390 # link in the additional environment configuration
391 ln -fs ${additionalEnvironment} "${cfg.stateDir}/config/additional_environment.rb"
394 # link in all user specified themes
395 for theme in ${concatStringsSep " " (mapAttrsToList unpackTheme cfg.themes)}; do
396 ln -fs $theme/* "${cfg.stateDir}/public/themes"
399 # link in redmine provided themes
400 ln -sf ${cfg.package}/share/redmine/public/themes.dist/* "${cfg.stateDir}/public/themes/"
403 # link in all user specified plugins
404 for plugin in ${concatStringsSep " " (mapAttrsToList unpackPlugin cfg.plugins)}; do
405 ln -fs $plugin/* "${cfg.stateDir}/plugins/''${plugin##*-redmine-plugin-}"
409 # handle database.passwordFile & permissions
410 cp -f ${databaseYml} "${cfg.stateDir}/config/database.yml"
412 ${optionalString ((cfg.database.type != "sqlite3") && (cfg.database.passwordFile != null)) ''
413 DBPASS="$(head -n1 ${cfg.database.passwordFile})"
414 sed -e "s,#dbpass#,$DBPASS,g" -i "${cfg.stateDir}/config/database.yml"
417 chmod 440 "${cfg.stateDir}/config/database.yml"
420 # generate a secret token if required
421 if ! test -e "${cfg.stateDir}/config/initializers/secret_token.rb"; then
422 ${bundle} exec rake generate_secret_token
423 chmod 440 "${cfg.stateDir}/config/initializers/secret_token.rb"
426 # execute redmine required commands prior to starting the application
427 ${bundle} exec rake db:migrate
428 ${bundle} exec rake redmine:plugins:migrate
429 ${bundle} exec rake redmine:load_default_data
437 WorkingDirectory = "${cfg.package}/share/redmine";
438 ExecStart="${bundle} exec rails server -u webrick -e production -b ${toString cfg.address} -p ${toString cfg.port} -P '${cfg.stateDir}/redmine.pid'";
439 AmbientCapabilities = "";
440 CapabilityBoundingSet = "";
441 LockPersonality = true;
442 MemoryDenyWriteExecute = true;
443 NoNewPrivileges = true;
444 PrivateDevices = true;
448 ProtectControlGroups = true;
450 ProtectHostname = true;
451 ProtectKernelLogs = true;
452 ProtectKernelModules = true;
453 ProtectKernelTunables = true;
454 ProtectProc = "noaccess";
455 ProtectSystem = "full";
457 RestrictAddressFamilies = [ "AF_UNIX" "AF_INET" ];
458 RestrictNamespaces = true;
459 RestrictRealtime = true;
460 RestrictSUIDSGID = true;
461 SystemCallArchitectures = "native";
467 users.users = optionalAttrs (cfg.user == "redmine") {
471 uid = config.ids.uids.redmine;
475 users.groups = optionalAttrs (cfg.group == "redmine") {
476 redmine.gid = config.ids.gids.redmine;
481 meta.maintainers = with lib.maintainers; [ felixsinger ];