diff --git a/nixos/modules/services/databases/postgresql.md b/nixos/modules/services/databases/postgresql.md index 6b95478c70f2..af10dace3086 100644 --- a/nixos/modules/services/databases/postgresql.md +++ b/nixos/modules/services/databases/postgresql.md @@ -354,6 +354,60 @@ self: super: { } ``` +You can add a custom PostgreSQL extension to an environment by calling `postgresqlPackages.callPackage` on the derivation. +In addition to the correct `postgresql` package, `callPackage` will provide additional functions to build those extensions: + +- `postgresqlBuildExtension`, extending `mkDerivation` for C and SQL extensions. +- `buildPgrxExtension`, extending `mkDerivation` for pgrx (Rust) extensions. +- `postgresqlTestExtension`, a helper to test these extensions on a PostgreSQL instance. + +We can define our extension as such: + +```nix +# my-extension.nix +{ + postgresql, + postgresqlBuildExtension, + # other regular mkDerivation arguments + fetchFromGitHub, +}: +postgresqlBuildExtension (finalAttrs: { + pname = "myext"; + # ... + src = fetchFromGitHub { + # ... + }; + meta = { + platforms = postgresql.meta.platforms; + # other meta properties + }; +}) +``` + +We can then build it with `callPackage`, for instance in a NixOS config: + +```nix +{ + services.postgresql.extensions = + ps: with ps; [ + pg_repack + postgis + (ps.callPackage ./my-extension.nix { }) + ]; +} +``` +Or to include it in a shell environment: + +```nix +{ + postgresql_custom = self.postgresql_17.withPackages (ps: [ + ps.pg_repack + ps.postgis + (ps.callPackage ./my-extension.nix { }) + ]); +} +``` + ## Procedural Languages {#module-services-postgres-pls} PostgreSQL ships the additional procedural languages PL/Perl, PL/Python and PL/Tcl as extensions. diff --git a/pkgs/servers/sql/postgresql/ext.nix b/pkgs/servers/sql/postgresql/ext.nix index 0fe5c4b5e734..1be386255117 100644 --- a/pkgs/servers/sql/postgresql/ext.nix +++ b/pkgs/servers/sql/postgresql/ext.nix @@ -7,6 +7,8 @@ in directory = ./ext; }) // { + inherit (super) callPackage; + timescaledb-apache = super.callPackage ./ext/timescaledb.nix { enableUnfree = false; }; } // lib.optionalAttrs (!self.perlSupport) {