8000 docs: updating for packaging and package updating (#1157) · cepro/postgres@7ea8fa8 · GitHub
[go: up one dir, main page]

Skip to content

Commit 7ea8fa8

Browse files
samrosesoedirgosteve-chavez
authored
docs: updating for packaging and package updating (supabase#1157)
* docs: updating for packaging and package updating * Update nix/docs/adding-new-package.md Co-authored-by: Bobbie Soedirgo <31685197+soedirgo@users.noreply.github.com> * Update nix/docs/adding-new-package.md Co-authored-by: Bobbie Soedirgo <31685197+soedirgo@users.noreply.github.com> * Update nix/docs/adding-new-package.md Co-authored-by: Bobbie Soedirgo <31685197+soedirgo@users.noreply.github.com> * Update nix/docs/update-extension.md Co-authored-by: Bobbie Soedirgo <31685197+soedirgo@users.noreply.github.com> * Update nix/docs/adding-new-package.md Co-authored-by: Steve Chavez <stevechavezast@gmail.com> --------- Co-authored-by: Sam Rose <samuel@supabase.io> Co-authored-by: Bobbie Soedirgo <31685197+soedirgo@users.noreply.github.com> Co-authored-by: Steve Chavez <stevechavezast@gmail.com>
1 parent 8091c42 commit 7ea8fa8

File tree

2 files changed

+170
-65
lines changed

2 files changed

+170
-65
lines changed

nix/docs/adding-new-package.md

Lines changed: 160 additions & 0 8000 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
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.

nix/docs/update-extension.md

Lines changed: 10 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,14 @@
1-
Extensions are managed in two places: upstream, and this repository. Therefore
2-
there are two update strategies available.
31

4-
## Updating extensions in this repository
2+
# Update an existing nix extension
53

6-
Assuming that you run `nix develop` to get a development shell, there is a tool
7-
named `nix-update` that is available:
84

9-
```
10-
austin@GANON:~/work/nix-postgres$ which nix-update
11-
/nix/store/2jyq6h0ln3f5vlgz2had80l2crdkjmdy-nix-update-0.19.2/bin/nix-update
12-
```
5+
1. Create a branch off of `develop`
6+
2. For instance, if we were updating https://github.com/supabase/postgres/blob/develop/nix/ext/supautils.nix we would:
7+
1. change the `version = "2.2.1";` to whatever our git tag release version is that we want to update to
8+
2. temporarily empty the `hash = "sha256-wSUEG0at00TPAoHv6+NMzuUE8mfW6fnHH0MNxvBdUiE=";` to `hash = "";` and save `supautils.nix` and `git add .`
9+
3. run `nix build .#psql_15/exts/supautils` or the name of the extension to update, 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/supautils.
10+
4. Add any needed migrations into the `supabase/postgres` migrations directory
11+
5. You can then run tests locally to verify that the update of the package succeeded.
12+
6. Now it's ready for PR review.
13+
1314

14-
Run something like this to update the extension `pg_foobar`:
15-
16-
```bash
17-
nix-update --flake psql_15/exts/pg_foobar
18-
git commit -asm "pg_foobar: update to latest release"
19-
```
20-
21-
It doesn't matter if you use `psql_15` or `psql_16` here, because `nix-update`
22-
will look at the _file_ that extension is defined in, in order to update the
23-
source code.
24-
25-
## Updating extensions upstream
26-
27-
You can use the same tool, `nix-update`, to do this. If you're sitting in the
28-
root of the nixpkgs repository, try this:
29-
30-
```
31-
nix run nixpkgs#nix-update -- postgresqlPackages.pg_foobar
32-
git commit -asm "pg_foobar: update to latest release"
33-
```
34-
35-
Because the tool may not be in your shell by default, we use `nix run` to run it
36-
for us.
37-
38-
The full list of available names to substitute for `pg_foobar` is available in
39-
the file
40-
[pkgs/servers/sql/postgresql/packages.nix](https://github.com/NixOS/nixpkgs/blob/master/pkgs/servers/sql/postgresql/packages.nix)
41-
42-
### Updating the Nixpkgs snapshot
43-
44-
Now that your change is merged upstream, you need to update the version of
45-
`nixpkgs` used in this repository:
46-
47-
- Check the `nixpkgs-unstable` branch:
48-
https://github.com/nixos/nixpkgs/tree/nixpkgs-unstable
49-
- Wait until your commit is fast-forwarded and part of that branch
50-
- Run `nix flake update`
51-
52-
## Release tags versus latest trunk
53-
54-
By default, `nix-update` will update an expression to the latest tagged release.
55-
No extra arguments are necessary. You can specify an exact release tag using the
56-
`--version=<xyz>` flag. Using the syntax `--version=branch` means "update to the
57-
latest version on the default branch."
58-
59-
<!-- ## Example PRs
60-
61-
- https://github.com/supabase/nix-postgres/pull/9 updates `pg_net` to the latest
62-
release
63-
- https://github.com/supabase/nix-postgres/pull/7 updates `pg_hashids` to the
64-
latest `trunk` tip
65-
-->
66-
## Other notes
67-
68-
See issue [#5](https://github.com/supabase/nix-postgres/issues/5) for more
69-
information about extension management.

0 commit comments

Comments
 (0)
0