@@ -5,7 +5,7 @@ with lib;
55let
66 cfg = config.services.redmine;
77
8− bundle = "${pkgs.redmine}/share/redmine/bin/bundle";
8+ bundle = "${cfg.package}/share/redmine/bin/bundle";
99
1010 databaseYml = pkgs.writeText "database.yml" ''
1111 production:
@@ -15,6 +15,7 @@ let
1515 port: ${toString cfg.database.port}
1616 username: ${cfg.database.user}
1717 password: #dbpass#
18+ ${optionalString (cfg.database.socket != null) "socket: ${cfg.database.socket}"}
1819 '';
1920
2021 configurationYml = pkgs.writeText "configuration.yml" ''
@@ -29,6 +30,19 @@ let
2930 ${cfg.extraConfig}
3031 '';
3132
33+ unpackTheme = unpack "theme";
34+ unpackPlugin = unpack "plugin";
35+ unpack = id: (name: source:
36+ pkgs.stdenv.mkDerivation {
37+ name = "redmine-${id}-${name}";
38+ buildInputs = [ pkgs.unzip ];
39+ buildCommand = ''
40+ mkdir -p $out
41+ cd $out
42+ unpackFile ${source}
43+ '';
44+ });
45+
3246in
3347
3448{
@@ -40,6 +54,14 @@ in
4054 description = "Enable the Redmine service.";
4155 };
4256
57+ package = mkOption {
58+ type = types.package;
59+ default = pkgs.redmine;
60+ defaultText = "pkgs.redmine";
61+ description = "Which Redmine package to use.";
62+ example = "pkgs.redmine.override { ruby = pkgs.ruby_2_3; }";
63+ };
64+
4365 user = mkOption {
4466 type = types.str;
4567 default = "redmine";
@@ -52,6 +74,12 @@ in
5274 description = "Group under which Redmine is ran.";
5375 };
5476
77+ port = mkOption {
78+ type = types.int;
79+ default = 3000;
80+ description = "Port on which Redmine is ran.";
81+ };
82+
5583 stateDir = mkOption {
5684 type = types.str;
5785 default = "/var/lib/redmine";
@@ -66,6 +94,41 @@ in
6694
6795 See https://guides.rubyonrails.org/action_mailer_basics.html#action-mailer-configuration
6896 '';
97+ example = literalExample ''
98+ email_delivery:
99+ delivery_method: smtp
100+ smtp_settings:
101+ address: mail.example.com
102+ port: 25
103+ '';
104+ };
105+
106+ themes = mkOption {
107+ type = types.attrsOf types.path;
108+ default = {};
109+ description = "Set of themes.";
110+ example = literalExample ''
111+ {
112+ dkuk-redmine_alex_skin = builtins.fetchurl {
113+ url = https://bitbucket.org/dkuk/redmine_alex_skin/get/1842ef675ef3.zip;
114+ sha256 = "0hrin9lzyi50k4w2bd2b30vrf1i4fi1c0gyas5801wn8i7kpm9yl";
115+ };
116+ }
117+ '';
118+ };
119+
120+ plugins = mkOption {
121+ type = types.attrsOf types.path;
122+ default = {};
123+ description = "Set of plugins.";
124+ example = literalExample ''
125+ {
126+ redmine_env_auth = builtins.fetchurl {
127+ url = https://github.com/Intera/redmine_env_auth/archive/0.6.zip;
128+ sha256 = "0yyr1yjd8gvvh832wdc8m3xfnhhxzk2pk3gm2psg5w9jdvd6skak";
129+ };
130+ }
131+ '';
69132 };
70133
71134 database = {
@@ -78,7 +141,7 @@ in
78141
79142 host = mkOption {
80143 type = types.str;
81− default = "127.0.0.1";
144+ default = (if cfg.database.socket != null then "localhost" else "127.0.0.1");
82145 description = "Database host address.";
83146 };
84147
@@ -119,6 +182,13 @@ in
119182 <option>database.user</option>.
120183 '';
121184 };
185+
186+ socket = mkOption {
187+ type = types.nullOr types.path;
188+ default = null;
189+ example = "/run/mysqld/mysqld.sock";
190+ description = "Path to the unix socket file to use for authentication.";
191+ };
122192 };
123193 };
124194 };
@@ -126,17 +196,20 @@ in
126196 config = mkIf cfg.enable {
127197
128198 assertions = [
129− { assertion = cfg.database.passwordFile != null || cfg.database.password != "";
130− message = "either services.redmine.database.passwordFile or services.redmine.database.password must be set";
199+ { assertion = cfg.database.passwordFile != null || cfg.database.password != "" || cfg.database.socket != null;
200+ message = "one of services.redmine.database.socket, services.redmine.database.passwordFile, or services.redmine.database.password must be set";
201+ }
202+ { assertion = cfg.database.socket != null -> (cfg.database.type == "mysql2");
203+ message = "Socket authentication is only available for the mysql2 database type";
131204 }
132205 ];
133206
134− environment.systemPackages = [ pkgs.redmine ];
207+ environment.systemPackages = [ cfg.package ];
135208
136209 systemd.services.redmine = {
137210 after = [ "network.target" (if cfg.database.type == "mysql2" then "mysql.service" else "postgresql.service") ];
138211 wantedBy = [ "multi-user.target" ];
139− environment.HOME = "${pkgs.redmine}/share/redmine";
212+ environment.HOME = "${cfg.package}/share/redmine";
140213 environment.RAILS_ENV = "production";
141214 environment.RAILS_CACHE = "${cfg.stateDir}/cache";
142215 environment.REDMINE_LANG = "en";
@@ -151,43 +224,80 @@ in
151224 subversion
152225 ];
153226 preStart = ''
154− # start with a fresh config directory every time
155− rm -rf ${cfg.stateDir}/config
156− cp -r ${pkgs.redmine}/share/redmine/config.dist ${cfg.stateDir}/config
227+ # ensure cache directory exists for db:migrate command
228+ mkdir -p ${cfg.stateDir}/cache
157229
158− # create the basic state directory layout pkgs.redmine expects
159− mkdir -p /run/redmine
230+ # create the basic directory layout the redmine package expects
231+ mkdir -p /run/redmine/public
160232
161233 for i in config files log plugins tmp; do
162234 mkdir -p ${cfg.stateDir}/$i
163− ln -fs ${cfg.stateDir}/$i /run/redmine/$i
235+ ln -fs ${cfg.stateDir}/$i /run/redmine/
164236 done
165237
166− # ensure cache directory exists for db:migrate command
167− mkdir -p ${cfg.stateDir}/cache
238+ for i in plugin_assets themes; do
239+ mkdir -p ${cfg.stateDir}/public/$i
240+ ln -fs ${cfg.stateDir}/public/$i /run/redmine/public/
241+ done
242+
243+
244+ # start with a fresh config directory
245+ # the config directory is copied instead of linked as some mutable data is stored in there
246+ rm -rf ${cfg.stateDir}/config/*
247+ cp -r ${cfg.package}/share/redmine/config.dist/* ${cfg.stateDir}/config/
168248
169249 # link in the application configuration
170250 ln -fs ${configurationYml} ${cfg.stateDir}/config/configuration.yml
171251
252+
253+ # link in all user specified themes
254+ rm -rf ${cfg.stateDir}/public/themes/*
255+ for theme in ${concatStringsSep " " (mapAttrsToList unpackTheme cfg.themes)}; do
256+ ln -fs $theme/* ${cfg.stateDir}/public/themes/
257+ done
258+
259+ # link in redmine provided themes
260+ ln -sf ${cfg.package}/share/redmine/public/themes.dist/* ${cfg.stateDir}/public/themes/
261+
262+
263+ # link in all user specified plugins
264+ rm -rf ${cfg.stateDir}/plugins/*
265+ for plugin in ${concatStringsSep " " (mapAttrsToList unpackPlugin cfg.plugins)}; do
266+ ln -fs $plugin/* ${cfg.stateDir}/plugins/''${plugin##*-redmine-plugin-}
267+ done
268+
269+
270+ # ensure correct permissions for most files
172271 chmod -R ug+rwX,o-rwx+x ${cfg.stateDir}/
173272
174− # handle database.passwordFile
273+
274+ # handle database.passwordFile & permissions
175275 DBPASS=$(head -n1 ${cfg.database.passwordFile})
176276 cp -f ${databaseYml} ${cfg.stateDir}/config/database.yml
177277 sed -e "s,#dbpass#,$DBPASS,g" -i ${cfg.stateDir}/config/database.yml
178278 chmod 440 ${cfg.stateDir}/config/database.yml
179279
280+
180281 # generate a secret token if required
181282 if ! test -e "${cfg.stateDir}/config/initializers/secret_token.rb"; then
182283 ${bundle} exec rake generate_secret_token
183284 chmod 440 ${cfg.stateDir}/config/initializers/secret_token.rb
184285 fi
185286
287+
186288 # ensure everything is owned by ${cfg.user}
187289 chown -R ${cfg.user}:${cfg.group} ${cfg.stateDir}
188290
189− ${bundle} exec rake db:migrate
190− ${bundle} exec rake redmine:load_default_data
291+
292+ # execute redmine required commands prior to starting the application
293+ # NOTE: su required in case using mysql socket authentication
294+ /run/wrappers/bin/su -s ${pkgs.bash}/bin/bash -m -l redmine -c '${bundle} exec rake db:migrate'
295+ /run/wrappers/bin/su -s ${pkgs.bash}/bin/bash -m -l redmine -c '${bundle} exec rake redmine:load_default_data'
296+
297+
298+ # log files don't exist until after first command has been executed
299+ # correct ownership of files generated by calling exec rake ...
300+ chown -R ${cfg.user}:${cfg.group} ${cfg.stateDir}/log
191301 '';
192302
193303 serviceConfig = {
@@ -196,13 +306,13 @@ in
196306 User = cfg.user;
197307 Group = cfg.group;
198308 TimeoutSec = "300";
199− WorkingDirectory = "${pkgs.redmine}/share/redmine";
200− ExecStart="${bundle} exec rails server webrick -e production -P ${cfg.stateDir}/redmine.pid";
309+ WorkingDirectory = "${cfg.package}/share/redmine";
310+ ExecStart="${bundle} exec rails server webrick -e production -p ${toString cfg.port} -P ${cfg.stateDir}/redmine.pid";
201311 };
202312
203313 };
204314
205− users.extraUsers = optionalAttrs (cfg.user == "redmine") (singleton
315+ users.users = optionalAttrs (cfg.user == "redmine") (singleton
206316 { name = "redmine";
207317 group = cfg.group;
208318 home = cfg.stateDir;
@@ -210,7 +320,7 @@ in
210320 uid = config.ids.uids.redmine;
211321 });
212322
213− users.extraGroups = optionalAttrs (cfg.group == "redmine") (singleton
323+ users.groups = optionalAttrs (cfg.group == "redmine") (singleton
214324 { name = "redmine";
215325 gid = config.ids.gids.redmine;
216326 });