Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

postgres: add an extensions option #604

Merged
merged 2 commits into from
May 23, 2023
Merged

Conversation

sandydoo
Copy link
Member

@sandydoo sandydoo commented May 18, 2023

This is a convenience option to install a list of PostgreSQL extensions. The set of available extensions is provided as an argument.

{
  services.postgres.enable = true;
  services.postgres.extensions = extensions: [ extensions.postgis ];
}

You could already do this by modifying the package with withPackages and then overriding services.postgres.package. The new option has two advantages over the existing approach:

  1. It's easier to find in the documentation.

  2. Users unfamiliar with nixpkgs are unlikely to be aware of
    pkgs.postgresql.withPackages specifically, or the pattern in
    general.

The NixOS services calls this option extraPlugins. First of all, that's not what PostgreSQL calls them. Second, you have to manually source the extensions from pkgs.postgresql.pkgs and ensure that the PostgreSQL version matches whatever is specified in services.postgres.package.

The description for the option contains a list of available extensions generated from the current version of PostgreSQL.

Screenshot 2023-05-18 at 21 27 43

@sandydoo sandydoo added the enhancement New feature or request label May 18, 2023
This is a convenience option to install a list of PostgreSQL extensions.
The set of available extensions is provided as an argument.

The description for the option contains a list of available extensions
generated from the current version of PostgreSQL.

You could already do this by modifying the package with `withPackages`
and then overriding `services.postgres.package`. The new option has two
advantages over the existing approach:

1. It's easier to find in the documentation.

2. Users unfamiliar with nixpkgs are unlikely to be aware of
   `pkgs.postgresql.withPackages` specifically, or the pattern in
   general.

The NixOS services calls this option `extraPlugins`. First of all,
that's not what PostgreSQL calls them. Second, you have to manually
source the extensions from `pkgs.postgresql.pkgs` and ensure that the
PostgreSQL version matches whatever is specified in
`services.postgres.package`.
@sandydoo sandydoo force-pushed the feature/postgres-extensions branch from 0ca6dd6 to 7e04f5c Compare May 19, 2023 18:05
@cloudflare-pages
Copy link

cloudflare-pages bot commented May 19, 2023

Deploying with  Cloudflare Pages  Cloudflare Pages

Latest commit: 7e04f5c
Status: ✅  Deploy successful!
Preview URL: https://358926f2.devenv.pages.dev
Branch Preview URL: https://feature-postgres-extensions.devenv.pages.dev

View logs

@domenkozar domenkozar merged commit da2b0c6 into main May 23, 2023
90 checks passed
@thenonameguy
Copy link
Contributor

thenonameguy commented May 23, 2023

Great addition!

Just for fun I wrote an usage of this new option with some meta-programming to install all known working PG extensions for the selected cfg.package postgres version reliably.

{
  services.postgres = {
    enable = true;
    package = pkgs.postgresql_15;
    # install all extensions
    extensions =
      let
        filterBrokenAndNonHostPackages = hostPlatform: pkgs:
          lib.filterAttrs (name: pkg:
            !(pkg.meta ? broken && pkg.meta.broken) &&
            (pkg.meta ? platforms && lib.lists.elem hostPlatform pkg.meta.platforms)
          ) pkgs;
      in
        extensions: (lib.attrValues (filterBrokenAndNonHostPackages pkgs.system extensions));
  };
}

It might be worthwhile to use parts of the above code to only generate the extensions that are actually available into mkdocs, based on arch + postgres version used.

For an actually working version of above, I had to this on aarch64-darwin:

        extensions: (lib.attrValues
          (builtins.removeAttrs
            (filterBrokenAndNonHostPackages pkgs.system extensions)
            ["tds_fdw" "timescaledb_toolkit" "timescaldb-apache" "timescaledb"]));

@sandydoo
Copy link
Member Author

@thenonameguy, very nice! 🙌

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants