summary refs log tree commit diff
diff options
context:
space:
mode:
authorJörg Thalheim <joerg@thalheim.io>2021-06-22 09:07:50 +0200
committerJörg Thalheim <joerg@thalheim.io>2021-06-26 11:59:50 +0200
commit34d1c55580cbf76bee01beef023b0d12d3f385cf (patch)
treed144a08c39856f4f0707fc156f303538d3bf7db4
parent00ead2addd89654e843a5eb977ecb089044a2245 (diff)
downloadnixpkgs-34d1c55580cbf76bee01beef023b0d12d3f385cf.tar
nixpkgs-34d1c55580cbf76bee01beef023b0d12d3f385cf.tar.gz
nixpkgs-34d1c55580cbf76bee01beef023b0d12d3f385cf.tar.bz2
nixpkgs-34d1c55580cbf76bee01beef023b0d12d3f385cf.tar.lz
nixpkgs-34d1c55580cbf76bee01beef023b0d12d3f385cf.tar.xz
nixpkgs-34d1c55580cbf76bee01beef023b0d12d3f385cf.tar.zst
nixpkgs-34d1c55580cbf76bee01beef023b0d12d3f385cf.zip
nixos/go-neb: secret support
-rw-r--r--nixos/modules/services/networking/go-neb.nix34
-rw-r--r--nixos/tests/go-neb.nix12
2 files changed, 36 insertions, 10 deletions
diff --git a/nixos/modules/services/networking/go-neb.nix b/nixos/modules/services/networking/go-neb.nix
index 991ae38f30a..765834fad83 100644
--- a/nixos/modules/services/networking/go-neb.nix
+++ b/nixos/modules/services/networking/go-neb.nix
@@ -5,7 +5,8 @@ with lib;
 let
   cfg = config.services.go-neb;
 
-  configFile = pkgs.writeText "config.yml" (builtins.toJSON cfg.config);
+  settingsFormat = pkgs.formats.yaml {};
+  configFile = settingsFormat.generate "config.yaml" cfg.config;
 in {
   options.services.go-neb = {
     enable = mkEnableOption "Extensible matrix bot written in Go";
@@ -16,13 +17,26 @@ in {
       default = ":4050";
     };
 
+    secretFile = mkOption {
+      type = types.nullOr types.path;
+      default = null;
+      example = "/run/keys/go-neb.env";
+      description = ''
+        Environment variables from this file will be interpolated into the
+        final config file using envsubst with this syntax: <literal>$ENVIRONMENT</literal>
+        or <literal>''${VARIABLE}</literal>.
+        The file should contain lines formatted as <literal>SECRET_VAR=SECRET_VALUE</literal>.
+        This is useful to avoid putting secrets into the nix store.
+      '';
+    };
+
     baseUrl = mkOption {
       type = types.str;
       description = "Public-facing endpoint that can receive webhooks.";
     };
 
     config = mkOption {
-      type = types.uniq types.attrs;
+      inherit (settingsFormat) type;
       description = ''
         Your <filename>config.yaml</filename> as a Nix attribute set.
         See <link xlink:href="https://github.com/matrix-org/go-neb/blob/master/config.sample.yaml">config.sample.yaml</link>
@@ -32,18 +46,30 @@ in {
   };
 
   config = mkIf cfg.enable {
-    systemd.services.go-neb = {
+    systemd.services.go-neb = let
+      finalConfigFile = if cfg.secretFile == null then configFile else "/var/run/go-neb/config.yaml";
+    in {
       description = "Extensible matrix bot written in Go";
       after = [ "network.target" ];
       wantedBy = [ "multi-user.target" ];
       environment = {
         BASE_URL = cfg.baseUrl;
         BIND_ADDRESS = cfg.bindAddress;
-        CONFIG_FILE = configFile;
+        CONFIG_FILE = finalConfigFile;
       };
 
       serviceConfig = {
+        ExecStartPre = lib.optional (cfg.secretFile != null)
+          (pkgs.writeShellScript "pre-start" ''
+            umask 077
+            export $(xargs < ${cfg.secretFile})
+            ${pkgs.envsubst}/bin/envsubst -i "${configFile}" > ${finalConfigFile}
+            chown go-neb ${finalConfigFile}
+          '');
+        PermissionsStartOnly = true;
+        RuntimeDirectory = "go-neb";
         ExecStart = "${pkgs.go-neb}/bin/go-neb";
+        User = "go-neb";
         DynamicUser = true;
       };
     };
diff --git a/nixos/tests/go-neb.nix b/nixos/tests/go-neb.nix
index f8801ff68d6..4bd03dcf3c6 100644
--- a/nixos/tests/go-neb.nix
+++ b/nixos/tests/go-neb.nix
@@ -10,10 +10,11 @@ import ./make-test-python.nix ({ pkgs, ... }:
       services.go-neb = {
         enable = true;
         baseUrl = "http://localhost";
+        secretFile = pkgs.writeText "secrets" "ACCESS_TOKEN=changeme";
         config = {
           clients = [ {
             UserId = "@test:localhost";
-            AccessToken = "changeme";
+            AccessToken = "$ACCESS_TOKEN";
             HomeServerUrl = "http://localhost";
             Sync = false;
             AutoJoinRooms = false;
@@ -33,11 +34,10 @@ import ./make-test-python.nix ({ pkgs, ... }:
   testScript = ''
     start_all()
     server.wait_for_unit("go-neb.service")
-    server.wait_until_succeeds(
-        "curl -fL http://localhost:4050/services/hooks/d2lraXBlZGlhX3NlcnZpY2U"
-    )
-    server.wait_until_succeeds(
-        "journalctl -eu go-neb -o cat | grep -q service_id=wikipedia_service"
+    server.wait_until_succeeds("curl -fL http://localhost:4050/services/hooks/d2lraXBlZGlhX3NlcnZpY2U")
+    server.succeed(
+        "journalctl -eu go-neb -o cat | grep -q service_id=wikipedia_service",
+        "grep -q changeme /var/run/go-neb/config.yaml",
     )
   '';