|
| 1 | +# Adding a new extension package |
| 2 | + |
| 3 | + |
| 4 | +## Pre-packaging steps |
| 5 | +1. Make sure you have nix installed [Nix installer](https://github.com/DeterminateSystems/nix-installer) |
| 6 | +2. Create a branch off of `develop` |
| 7 | + |
| 8 | + |
| 9 | +## C/C++ postgres extensions |
| 10 | + |
| 11 | +If you are creating a C/C++ extension, the pattern found in https://github.com/supabase/postgres/blob/develop/nix/ext/pgvector.nix will work well. |
| 12 | + |
| 13 | +``` |
| 14 | +{ lib, stdenv, fetchFromGitHub, postgresql }: |
| 15 | +
|
| 16 | +stdenv.mkDerivation rec { |
| 17 | + pname = "pgvector"; |
| 18 | + version = "0.7.4"; |
| 19 | +
|
| 20 | + buildInputs = [ postgresql ]; |
| 21 | +
|
| 22 | + src = fetchFromGitHub { |
| 23 | + owner = "pgvector"; |
| 24 | + repo = pname; |
| 25 | + rev = "refs/tags/v${version}"; |
| 26 | + hash = "sha256-qwPaguQUdDHV8q6GDneLq5MuhVroPizpbqt7f08gKJI="; |
| 27 | + }; |
| 28 | +
|
| 29 | + installPhase = '' |
| 30 | + mkdir -p $out/{lib,share/postgresql/extension} |
| 31 | +
|
| 32 | + cp *.so $out/lib |
| 33 | + cp sql/*.sql $out/share/postgresql/extension |
| 34 | + cp *.control $out/share/postgresql/extension |
| 35 | + ''; |
| 36 | +
|
| 37 | + meta = with lib; { |
| 38 | + description = "Open-source vector similarity search for Postgres"; |
| 39 | + homepage = "https://github.com/${src.owner}/${src.repo}"; |
| 40 | + maintainers = with maintainers; [ olirice ]; |
| 41 | + platforms = postgresql.meta.platforms; |
| 42 | + license = licenses.postgresql; |
| 43 | + }; |
| 44 | +} |
| 45 | +``` |
| 46 | + |
| 47 | +This uses `stdenv.mkDerivation` which is a general nix builder for C and C++ projects (and others). It can auto detect the Makefile, and attempt to use it. ***It's a good practice to not have steps in the Makefile of your project that try to deal with OS specific system paths, or make calls out to the internet, as Nix cannot use these steps to build your project.*** |
| 48 | + |
| 49 | +Your build should produce all of the sql and control files needed for the install phase. |
| 50 | + |
| 51 | +1. Once you have created this file, you can add it to `nix/ext/<yourname>.nix` and edit `flake.nix` and add it to the `ourExtensions` list. |
| 52 | +2. `git add .` as nix uses git to track changes |
| 53 | +3. In your package file, temporarily empty the `hash = "sha256<...>=";` to `hash = "";` and save and `git add .` |
| 54 | +4. Run `nix build .#psql_15/exts/<yourname>` to try to trigger a build, nix will print the calculated sha256 value that you can add back the the `hash` variable, save the file again, and re-run `nix build .#psql_15/exts/<yourname>`. |
| 55 | +5. Add any needed migrations into the `supabase/postgres` migrations directory. |
| 56 | +6. You can then run tests locally to verify that the update of the package succeeded. |
| 57 | +7. Now it's ready for PR review! |
| 58 | + |
| 59 | +## Extensions written in Rust that use `buildPgrxExtension` builder |
| 60 | + |
| 61 | +Extensions like: |
| 62 | + |
| 63 | +* https://github.com/supabase/postgres/blob/develop/nix/ext/wrappers/default.nix |
| 64 | +* https://github.com/supabase/postgres/blob/develop/nix/ext/pg_graphql.nix |
| 65 | +* https://github.com/supabase/postgres/blob/develop/nix/ext/pg_jsonschema.nix |
| 66 | + |
| 67 | +Are written in Rust, built with `cargo`, and need to use https://github.com/pgcentralfoundation/pgrx to build the extension. |
| 68 | + |
| 69 | +We in turn have a special nix package `builder` which is sourced from `nixpkgs` and called `buildPgrxExtension` |
| 70 | + |
| 71 | +A simple example is found in `pg_jsonschema` |
| 72 | + |
| 73 | + |
| 74 | +``` |
| 75 | +{ lib, stdenv, fetchFromGitHub, postgresql, buildPgrxExtension_0_11_3, cargo }: |
| 76 | +
|
| 77 | +buildPgrxExtension_0_11_3 rec { |
| 78 | + pname = "pg_jsonschema"; |
| 79 | + version = "0.3.1"; |
| 80 | + inherit postgresql; |
| 81 | +
|
| 82 | + src = fetchFromGitHub { |
| 83 | + owner = "supabase"; |
| 84 | + repo = pname; |
| 85 | + rev = "v${version}"; |
| 86 | + hash = "sha256-YdKpOEiDIz60xE7C+EzpYjBcH0HabnDbtZl23CYls6g="; |
| 87 | + }; |
| 88 | +
|
| 89 | + nativeBuildInputs = [ cargo ]; |
| 90 | + buildInputs = [ postgresql ]; |
| 91 | + # update the following array when the pg_jsonschema version is updated |
| 92 | + # required to ensure that extensions update scripts from previous versions are generated |
| 93 | +
|
| 94 | + previousVersions = ["0.3.0" "0.2.0" "0.1.4" "0.1.4" "0.1.2" "0.1.1" "0.1.0"]; |
| 95 | + CARGO="${cargo}/bin/cargo"; |
| 96 | + env = lib.optionalAttrs stdenv.isDarwin { |
| 97 | + POSTGRES_LIB = "${postgresql}/lib"; |
| 98 | + RUSTFLAGS = "-C link-arg=-undefined -C link-arg=dynamic_lookup"; |
| 99 | + }; |
| 100 | + cargoHash = "sha256-VcS+efMDppofuFW2zNrhhsbC28By3lYekDFquHPta2g="; |
| 101 | +
|
| 102 | + # FIXME (aseipp): testsuite tries to write files into /nix/store; we'll have |
| 103 | + # to fix this a bit later. |
| 104 | + doCheck = false; |
| 105 | +
|
| 106 | + preBuild = '' |
| 107 | + echo "Processing git tags..." |
| 108 | + echo '${builtins.concatStringsSep "," previousVersions}' | sed 's/,/\n/g' > git_tags.txt |
| 109 | + ''; |
| 110 | +
|
| 111 | + postInstall = '' |
| 112 | + echo "Creating SQL files for previous versions..." |
| 113 | + current_version="${version}" |
| 114 | + sql_file="$out/share/postgresql/extension/pg_jsonschema--$current_version.sql" |
| 115 | + |
| 116 | + if [ -f "$sql_file" ]; then |
| 117 | + while read -r previous_version; do |
| 118 | + if [ "$(printf '%s\n' "$previous_version" "$current_version" | sort -V | head -n1)" = "$previous_version" ] && [ "$previous_version" != "$current_version" ]; then |
| 119 | + new_file="$out/share/postgresql/extension/pg_jsonschema--$previous_version--$current_version.sql" |
| 120 | + echo "Creating $new_file" |
| 121 | + cp "$sql_file" "$new_file" |
| 122 | + fi |
| 123 | + done < git_tags.txt |
| 124 | + else |
| 125 | + echo "Warning: $sql_file not found" |
| 126 | + fi |
| 127 | + rm git_tags.txt |
| 128 | + ''; |
| 129 | +
|
| 130 | +
|
| 131 | + meta = with lib; { |
| 132 | + description = "JSON Schema Validation for PostgreSQL"; |
| 133 | + homepage = "https://github.com/supabase/${pname}"; |
| 134 | + maintainers = with maintainers; [ samrose ]; |
| 135 | + platforms = postgresql.meta.platforms; |
| 136 | + license = licenses.postgresql; |
| 137 | + }; |
| 138 | +} |
| 139 | +``` |
| 140 | + |
| 141 | +Here we have built support in our overlay to specify and pin the version of `buildPgrxExtension` to a specific version (in this case `buildPgrxExtension_0_11_3`). This is currently the only version we can support, but this can be extended in our overlay https://github.com/supabase/postgres/blob/develop/nix/overlays/cargo-pgrx-0-11-3.nix to support other versions. |
| 142 | + |
| 143 | +A few things about `buildPgrxExtension_x`: |
| 144 | + |
| 145 | +* It doesn't support `buildPhase`, `installPhase` and those are implemented directly in the builder already |
| 146 | +* It mostly just allows `cargo build` to do it's thing, but you may need to set env vars for the build process as seen above |
| 147 | +* It caclulates a special `cargoHash` that will be generated after the first in `src` is generated, when running `nix build .#psql_15/exts/<yourname>` to build the extension |
| 148 | + |
| 149 | + |
| 150 | +## Post Nix derivation release steps |
| 151 | + |
| 152 | + |
| 153 | +1. You can add and run tests as described in https://github.com/supabase/postgres/blob/develop/nix/docs/adding-tests.md |
| 154 | +2. You may need to add tests to our test.yml gh action workflow as well. |
| 155 | +3. You can add the package and name and version to `ansible/vars.yml` it is not necessary to add the sha256 hash here, as the package is already built and cached in our release process before these vars are ever run. |
| 156 | +4. to check that all your files will land in the overall build correctly, you can run `nix profile install .#psql_15/bin` on your machine, and check in `~/.nix-profile/bin, ~/.nix-profile/lib, ~/.nix-profile/share/postgresql/*` and you should see your lib, .control and sql files there. |
| 157 | +5. You can also run `nix run .#start-server 15` and in a new terminal window run `nix run .#star-client-and-migrate 15` and try to `CREATE EXTENSION <yourname>` and work with it there |
| 158 | +6. Check that your extension works with the `pg_upgrade` process (TODO documentation forthcoming) |
| 159 | +7. Now you are ready to PR the extension |
| 160 | +8. From here, the release process should typically take care of the rest. |
0 commit comments