From e702468f6b73154b712b4ebdf5bc1410e6198eaf Mon Sep 17 00:00:00 2001 From: Aaron Andersen Date: Sat, 22 Jun 2019 16:03:42 -0400 Subject: [PATCH] nixos/redmine: add database.createLocally option --- nixos/modules/services/misc/redmine.nix | 57 +++++++++++++++++++++---- nixos/tests/redmine.nix | 24 +---------- 2 files changed, 51 insertions(+), 30 deletions(-) diff --git a/nixos/modules/services/misc/redmine.nix b/nixos/modules/services/misc/redmine.nix index 91ddf2c3edf3..b0cc11e7c8b6 100644 --- a/nixos/modules/services/misc/redmine.nix +++ b/nixos/modules/services/misc/redmine.nix @@ -11,11 +11,11 @@ let production: adapter: ${cfg.database.type} database: ${cfg.database.name} - host: ${cfg.database.host} + host: ${if (cfg.database.type == "postgresql" && cfg.database.socket != null) then cfg.database.socket else cfg.database.host} port: ${toString cfg.database.port} username: ${cfg.database.user} password: #dbpass# - ${optionalString (cfg.database.socket != null) "socket: ${cfg.database.socket}"} + ${optionalString (cfg.database.type == "mysql2" && cfg.database.socket != null) "socket: ${cfg.database.socket}"} ''; configurationYml = pkgs.writeText "configuration.yml" '' @@ -50,6 +50,9 @@ let ''; }); + mysqlLocal = cfg.database.createLocally && cfg.database.type == "mysql2"; + pgsqlLocal = cfg.database.createLocally && cfg.database.type == "postgresql"; + in { @@ -169,13 +172,14 @@ in host = mkOption { type = types.str; - default = (if cfg.database.socket != null then "localhost" else "127.0.0.1"); + default = "localhost"; description = "Database host address."; }; port = mkOption { type = types.int; - default = 3306; + default = if cfg.database.type == "postgresql" then 5432 else 3306; + defaultText = "3306"; description = "Database host port."; }; @@ -213,10 +217,20 @@ in socket = mkOption { type = types.nullOr types.path; - default = null; + default = + if mysqlLocal then "/run/mysqld/mysqld.sock" + else if pgsqlLocal then "/run/postgresql" + else null; + defaultText = "/run/mysqld/mysqld.sock"; example = "/run/mysqld/mysqld.sock"; description = "Path to the unix socket file to use for authentication."; }; + + createLocally = mkOption { + type = types.bool; + default = true; + description = "Create the database and database user locally."; + }; }; }; }; @@ -227,11 +241,38 @@ in { assertion = cfg.database.passwordFile != null || cfg.database.password != "" || cfg.database.socket != null; message = "one of services.redmine.database.socket, services.redmine.database.passwordFile, or services.redmine.database.password must be set"; } - { assertion = cfg.database.socket != null -> (cfg.database.type == "mysql2"); - message = "Socket authentication is only available for the mysql2 database type"; + { assertion = cfg.database.createLocally -> cfg.database.user == cfg.user; + message = "services.redmine.database.user must be set to ${cfg.user} if services.redmine.database.createLocally is set true"; + } + { assertion = cfg.database.createLocally -> cfg.database.socket != null; + message = "services.redmine.database.socket must be set if services.redmine.database.createLocally is set to true"; + } + { assertion = cfg.database.createLocally -> cfg.database.host == "localhost"; + message = "services.redmine.database.host must be set to localhost if services.redmine.database.createLocally is set to true"; } ]; + services.mysql = mkIf mysqlLocal { + enable = true; + package = mkDefault pkgs.mariadb; + ensureDatabases = [ cfg.database.name ]; + ensureUsers = [ + { name = cfg.database.user; + ensurePermissions = { "${cfg.database.name}.*" = "ALL PRIVILEGES"; }; + } + ]; + }; + + services.postgresql = mkIf pgsqlLocal { + enable = true; + ensureDatabases = [ cfg.database.name ]; + ensureUsers = [ + { name = cfg.database.user; + ensurePermissions = { "DATABASE ${cfg.database.name}" = "ALL PRIVILEGES"; }; + } + ]; + }; + environment.systemPackages = [ cfg.package ]; # create symlinks for the basic directory layout the redmine package expects @@ -259,7 +300,7 @@ in ]; systemd.services.redmine = { - after = [ "network.target" (if cfg.database.type == "mysql2" then "mysql.service" else "postgresql.service") ]; + after = [ "network.target" ] ++ optional mysqlLocal "mysql.service" ++ optional pgsqlLocal "postgresql.service"; wantedBy = [ "multi-user.target" ]; environment.RAILS_ENV = "production"; environment.RAILS_CACHE = "${cfg.stateDir}/cache"; diff --git a/nixos/tests/redmine.nix b/nixos/tests/redmine.nix index cbdb5c8d2954..2d4df288b055 100644 --- a/nixos/tests/redmine.nix +++ b/nixos/tests/redmine.nix @@ -10,19 +10,9 @@ let mysqlTest = package: makeTest { machine = { config, pkgs, ... }: - { services.mysql.enable = true; - services.mysql.package = pkgs.mariadb; - services.mysql.ensureDatabases = [ "redmine" ]; - services.mysql.ensureUsers = [ - { name = "redmine"; - ensurePermissions = { "redmine.*" = "ALL PRIVILEGES"; }; - } - ]; - - services.redmine.enable = true; + { services.redmine.enable = true; services.redmine.package = package; services.redmine.database.type = "mysql2"; - services.redmine.database.socket = "/run/mysqld/mysqld.sock"; services.redmine.plugins = { redmine_env_auth = pkgs.fetchurl { url = https://github.com/Intera/redmine_env_auth/archive/0.7.zip; @@ -48,19 +38,9 @@ let pgsqlTest = package: makeTest { machine = { config, pkgs, ... }: - { services.postgresql.enable = true; - services.postgresql.ensureDatabases = [ "redmine" ]; - services.postgresql.ensureUsers = [ - { name = "redmine"; - ensurePermissions = { "DATABASE redmine" = "ALL PRIVILEGES"; }; - } - ]; - - services.redmine.enable = true; + { services.redmine.enable = true; services.redmine.package = package; services.redmine.database.type = "postgresql"; - services.redmine.database.host = ""; - services.redmine.database.port = 5432; services.redmine.plugins = { redmine_env_auth = pkgs.fetchurl { url = https://github.com/Intera/redmine_env_auth/archive/0.7.zip;