summary refs log tree commit diff
path: root/nixos/modules/services/web-apps/mattermost.nix
diff options
context:
space:
mode:
authorMorgan Jones <me@numin.it>2021-04-11 19:29:29 -0600
committerRaphael Megzari <raphael@megzari.com>2021-12-31 23:49:00 -0500
commit174b3404063c5821a1852435a0113a792beb7bb4 (patch)
treea7ee3260eb51cee15b6735bb19438c3a0ecf8ab0 /nixos/modules/services/web-apps/mattermost.nix
parentf5901b85d106fbd1a868bb956490c5da77345c1b (diff)
downloadnixpkgs-174b3404063c5821a1852435a0113a792beb7bb4.tar
nixpkgs-174b3404063c5821a1852435a0113a792beb7bb4.tar.gz
nixpkgs-174b3404063c5821a1852435a0113a792beb7bb4.tar.bz2
nixpkgs-174b3404063c5821a1852435a0113a792beb7bb4.tar.lz
nixpkgs-174b3404063c5821a1852435a0113a792beb7bb4.tar.xz
nixpkgs-174b3404063c5821a1852435a0113a792beb7bb4.tar.zst
nixpkgs-174b3404063c5821a1852435a0113a792beb7bb4.zip
nixos/mattermost: add preferNixConfig option and tests
One use case for Mattermost configuration is doing a "mostly
mutable" configuration where NixOS module options take priority
over Mattermost's config JSON.

Add a preferNixConfig option that prefers configured Nix options
over what's configured in Mattermost config if mutableConfig is set.

Remove the reliance on readFile (it's flake incompatible) and use
jq instead.

Merge Mattermost configs together on Mattermost startup, depending
on configured module options.

Write tests for mutable, mostly mutable, and immutable configurations.
Diffstat (limited to 'nixos/modules/services/web-apps/mattermost.nix')
-rw-r--r--nixos/modules/services/web-apps/mattermost.nix42
1 files changed, 26 insertions, 16 deletions
diff --git a/nixos/modules/services/web-apps/mattermost.nix b/nixos/modules/services/web-apps/mattermost.nix
index f5c2c356afc..a3127aed692 100644
--- a/nixos/modules/services/web-apps/mattermost.nix
+++ b/nixos/modules/services/web-apps/mattermost.nix
@@ -6,23 +6,18 @@ let
 
   cfg = config.services.mattermost;
 
-  defaultConfig = builtins.fromJSON (builtins.replaceStrings [ "\\u0026" ] [ "&" ]
-    (readFile "${pkgs.mattermost}/config/config.json")
-  );
-
   database = "postgres://${cfg.localDatabaseUser}:${cfg.localDatabasePassword}@localhost:5432/${cfg.localDatabaseName}?sslmode=disable&connect_timeout=10";
 
-  mattermostConf = foldl recursiveUpdate defaultConfig
-    [ { ServiceSettings.SiteURL = cfg.siteUrl;
-        ServiceSettings.ListenAddress = cfg.listenAddress;
-        TeamSettings.SiteName = cfg.siteName;
-        SqlSettings.DriverName = "postgres";
-        SqlSettings.DataSource = database;
-      }
-      cfg.extraConfig
-    ];
+  mattermostConf = recursiveUpdate
+    { ServiceSettings.SiteURL = cfg.siteUrl;
+      ServiceSettings.ListenAddress = cfg.listenAddress;
+      TeamSettings.SiteName = cfg.siteName;
+      SqlSettings.DriverName = "postgres";
+      SqlSettings.DataSource = database;
+    }
+    cfg.extraConfig;
 
-  mattermostConfJSON = pkgs.writeText "mattermost-config-raw.json" (builtins.toJSON mattermostConf);
+  mattermostConfJSON = pkgs.writeText "mattermost-config.json" (builtins.toJSON mattermostConf);
 
 in
 
@@ -77,6 +72,16 @@ in
         '';
       };
 
+      preferNixConfig = mkOption {
+        type = types.bool;
+        default = false;
+        description = ''
+          If both mutableConfig and this option are set, the Nix configuration
+          will take precedence over any settings configured in the server
+          console.
+        '';
+      };
+
       extraConfig = mkOption {
         type = types.attrs;
         default = { };
@@ -180,14 +185,19 @@ in
           ln -sf ${pkgs.mattermost}/{bin,fonts,i18n,templates,client} ${cfg.statePath}
         '' + lib.optionalString (!cfg.mutableConfig) ''
           rm -f ${cfg.statePath}/config/config.json
-          cp ${mattermostConfJSON} ${cfg.statePath}/config/config.json
+          ${pkgs.jq}/bin/jq -s '.[0] * .[1]' ${pkgs.mattermost}/config/config.json ${mattermostConfJSON} > ${cfg.statePath}/config/config.json
           ${pkgs.mattermost}/bin/mattermost config migrate ${cfg.statePath}/config/config.json ${database}
         '' + lib.optionalString cfg.mutableConfig ''
           if ! test -e "${cfg.statePath}/config/.initial-created"; then
             rm -f ${cfg.statePath}/config/config.json
-            cp ${mattermostConfJSON} ${cfg.statePath}/config/config.json
+            ${pkgs.jq}/bin/jq -s '.[0] * .[1]' ${pkgs.mattermost}/config/config.json ${mattermostConfJSON} > ${cfg.statePath}/config/config.json
             touch ${cfg.statePath}/config/.initial-created
           fi
+        '' + lib.optionalString (cfg.mutableConfig && cfg.preferNixConfig) ''
+          newConfig="$(${pkgs.jq}/bin/jq -s '.[0] * .[1]' ${cfg.statePath}/config/config.json ${mattermostConfJSON})"
+
+          rm -f ${cfg.statePath}/config/config.json
+          echo "$newConfig" > ${cfg.statePath}/config/config.json
         '' + lib.optionalString cfg.localDatabaseCreate ''
           if ! test -e "${cfg.statePath}/.db-created"; then
             ${pkgs.sudo}/bin/sudo -u ${config.services.postgresql.superUser} \