From 18f9decf139fca00ae6a19697e4ee16d10363270 Mon Sep 17 00:00:00 2001 From: Shivaraj B H Date: Wed, 13 Mar 2024 00:40:13 +0530 Subject: [PATCH] refactor(postgres): replace string argument with attrset for `connectionURI` (#146) for justification, see: https://github.com/juspay/services-flake/pull/143#discussion_r1521290361 --- example/share-services/pgweb/flake.nix | 7 +------ example/simple/flake.nix | 2 +- nix/postgres/default.nix | 7 +++++-- 3 files changed, 7 insertions(+), 9 deletions(-) diff --git a/example/share-services/pgweb/flake.nix b/example/share-services/pgweb/flake.nix index ddee7a1..1f6320b 100644 --- a/example/share-services/pgweb/flake.nix +++ b/example/share-services/pgweb/flake.nix @@ -29,12 +29,7 @@ settings.processes.pgweb = { command = pkgs.pgweb; depends_on."northwind".condition = "process_healthy"; - environment.PGWEB_DATABASE_URL = - let - inherit (config.services.postgres.northwind) - listen_addresses port; - in - "postgres://$USER@${listen_addresses}:${builtins.toString port}/sample"; + environment.PGWEB_DATABASE_URL = config.services.postgres.northwind.connectionURI { dbName = "sample"; }; }; }; }; diff --git a/example/simple/flake.nix b/example/simple/flake.nix index 04fcdb3..9387f2e 100644 --- a/example/simple/flake.nix +++ b/example/simple/flake.nix @@ -45,7 +45,7 @@ pgcfg = config.services.postgres.pg1; in { - environment.PGWEB_DATABASE_URL = pgcfg.connectionURI dbName; + environment.PGWEB_DATABASE_URL = pgcfg.connectionURI { inherit dbName; }; command = pkgs.pgweb; depends_on."pg1".condition = "process_healthy"; }; diff --git a/nix/postgres/default.nix b/nix/postgres/default.nix index 5ce96c2..1f5dbec 100644 --- a/nix/postgres/default.nix +++ b/nix/postgres/default.nix @@ -60,8 +60,11 @@ in connectionURI = lib.mkOption { type = lib.types.functionTo lib.types.str; readOnly = true; - default = dbName: "postgres://${config.listen_addresses}:${builtins.toString config.port}/${dbName}"; - description = "A function that accepts database name and returns the [postgres connection URI](https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-CONNSTRING-URIS)"; + default = { dbName, ... }: "postgres://${config.listen_addresses}:${builtins.toString config.port}/${dbName}"; + description = '' + A function that accepts an attrset overriding the connection parameters + and returns the [postgres connection URI](https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-CONNSTRING-URIS) + ''; }; hbaConf =