diff --git a/flake.lock b/flake.lock index 3ee7c2d7f..2d05388c6 100644 --- a/flake.lock +++ b/flake.lock @@ -121,12 +121,47 @@ "type": "github" } }, + "nixpkgs_4": { + "locked": { + "lastModified": 1728538411, + "narHash": "sha256-f0SBJz1eZ2yOuKUr5CA9BHULGXVSn6miBuUWdTyhUhU=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "b69de56fac8c2b6f8fd27f2eca01dcda8e0a4221", + "type": "github" + }, + "original": { + "owner": "NixOS", + "ref": "nixpkgs-unstable", + "repo": "nixpkgs", + "type": "github" + } + }, "root": { "inputs": { "flake-utils": "flake-utils", "nix-editor": "nix-editor", "nix2container": "nix2container", - "nixpkgs": "nixpkgs_3" + "nixpkgs": "nixpkgs_3", + "rust-overlay": "rust-overlay" + } + }, + "rust-overlay": { + "inputs": { + "nixpkgs": "nixpkgs_4" + }, + "locked": { + "lastModified": 1729045942, + "narHash": "sha256-HjmK0x5Zm2TK2vFpC7XBM2e3EDNVnAIuEoU2FkeN8xw=", + "owner": "oxalica", + "repo": "rust-overlay", + "rev": "9de3cea452d2401d6f93c06ad985178a4e11d1fc", + "type": "github" + }, + "original": { + "owner": "oxalica", + "repo": "rust-overlay", + "type": "github" } }, "systems": { diff --git a/flake.nix b/flake.nix index 15f1a585a..f34dcb8fb 100644 --- a/flake.nix +++ b/flake.nix @@ -6,9 +6,10 @@ flake-utils.url = "github:numtide/flake-utils"; nix2container.url = "github:nlewo/nix2container"; nix-editor.url = "github:snowfallorg/nix-editor"; + rust-overlay.url = "github:oxalica/rust-overlay"; }; - outputs = { self, nixpkgs, flake-utils, nix2container, nix-editor, ...}: + outputs = { self, nixpkgs, flake-utils, nix2container, nix-editor, rust-overlay, ...}: let gitRev = "vcs=${self.shortRev or "dirty"}+${builtins.substring 0 8 (self.lastModifiedDate or self.lastModified or "19700101")}"; @@ -53,10 +54,41 @@ }; inherit system; overlays = [ - # NOTE (aseipp): add any needed overlays here. in theory we could + # NOTE add any needed overlays here. in theory we could # pull them from the overlays/ directory automatically, but we don't # want to have an arbitrary order, since it might matter. being # explicit is better. + (import rust-overlay) + (final: prev: { + cargo-pgrx = final.callPackage ./nix/cargo-pgrx/default.nix { + inherit (final) lib; + inherit (final) darwin; + inherit (final) fetchCrate; + inherit (final) openssl; + inherit (final) pkg-config; + inherit (final) makeRustPlatform; + inherit (final) stdenv; + inherit (final) rust-bin; + }; + + buildPgrxExtension = final.callPackage ./nix/cargo-pgrx/buildPgrxExtension.nix { + inherit (final) cargo-pgrx; + inherit (final) lib; + inherit (final) Security; + inherit (final) pkg-config; + inherit (final) makeRustPlatform; + inherit (final) stdenv; + inherit (final) writeShellScriptBin; + }; + + buildPgrxExtension_0_11_3 = prev.buildPgrxExtension.override { + cargo-pgrx = final.cargo-pgrx.cargo-pgrx_0_11_3; + }; + + buildPgrxExtension_0_12_6 = prev.buildPgrxExtension.override { + cargo-pgrx = final.cargo-pgrx.cargo-pgrx_0_12_6; + }; + }) (final: prev: { postgresql = final.callPackage ./nix/postgresql/default.nix { inherit (final) lib; @@ -66,7 +98,6 @@ inherit (final) callPackage; }; }) - (import ./nix/overlays/cargo-pgrx-0-11-3.nix) ]; }; sfcgal = pkgs.callPackage ./nix/ext/sfcgal/sfcgal.nix { }; @@ -310,6 +341,8 @@ in postgresVersions //{ supabase-groonga = supabase-groonga; + cargo-pgrx_0_11_3 = pkgs.cargo-pgrx.cargo-pgrx_0_11_3; + cargo-pgrx_0_12_6 = pkgs.cargo-pgrx.cargo-pgrx_0_12_6; # PostgreSQL versions. psql_15 = makePostgres "15"; #psql_orioledb_16 = makeOrioleDbPostgres "16_23" postgresql_orioledb_16; @@ -566,10 +599,7 @@ packages = flake-utils.lib.flattenTree basePackages // { # Any extra packages we might want to include in our package # set can go here. - inherit (pkgs) - # NOTE: comes from our cargo-pgrx-0-11-3.nix overlay - cargo-pgrx_0_11_3; - + inherit (pkgs); }; # The list of exported 'checks' that are run with every run of 'nix @@ -603,7 +633,21 @@ # ambient $PATH environment when you run 'nix develop'. This is useful # for development and puts many convenient devtools instantly within # reach. - devShells.default = pkgs.mkShell { + + devShells = let + mkCargoPgrxDevShell = { pgrxVersion, rustVersion }: pkgs.mkShell { + packages = with pkgs; [ + basePackages."cargo-pgrx_${pgrxVersion}" + (rust-bin.stable.${rustVersion}.default.override { + extensions = [ "rust-src" ]; + }) + ]; + shellHook = '' + export HISTFILE=.history + ''; + }; + in { + default = pkgs.mkShell { packages = with pkgs; [ coreutils just @@ -626,6 +670,15 @@ export HISTFILE=.history ''; }; - } - ); + cargo-pgrx_0_11_3 = mkCargoPgrxDevShell { + pgrxVersion = "0_11_3"; + rustVersion = "1.80.0"; + }; + cargo-pgrx_0_12_6 = mkCargoPgrxDevShell { + pgrxVersion = "0_12_6"; + rustVersion = "1.80.0"; + }; + }; + } + ); } diff --git a/nix/cargo-pgrx/buildPgrxExtension.nix b/nix/cargo-pgrx/buildPgrxExtension.nix new file mode 100644 index 000000000..89293ab62 --- /dev/null +++ b/nix/cargo-pgrx/buildPgrxExtension.nix @@ -0,0 +1,161 @@ +# preBuildAndTest and some small other bits +# taken from https://github.com/tcdi/pgrx/blob/v0.9.4/nix/extension.nix +# (but now heavily modified) +# which uses MIT License with the following license file +# +# MIT License +# +# Portions Copyright 2019-2021 ZomboDB, LLC. +# Portions Copyright 2021-2022 Technology Concepts & Design, Inc. . +# All rights reserved. +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in all +# copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +# SOFTWARE. + +{ lib +, cargo-pgrx +, pkg-config +, rustPlatform +, stdenv +, Security +, writeShellScriptBin +}: + +# The idea behind: Use it mostly like rustPlatform.buildRustPackage and so +# we hand most of the arguments down. +# +# Additional arguments are: +# - `postgresql` postgresql package of the version of postgresql this extension should be build for. +# Needs to be the build platform variant. +# - `useFakeRustfmt` Whether to use a noop fake command as rustfmt. cargo-pgrx tries to call rustfmt. +# If the generated rust bindings aren't needed to use the extension, its a +# unnecessary and heavy dependency. If you set this to true, you also +# have to add `rustfmt` to `nativeBuildInputs`. + +{ buildAndTestSubdir ? null +, buildType ? "release" +, buildFeatures ? [ ] +, cargoBuildFlags ? [ ] +, postgresql +# cargo-pgrx calls rustfmt on generated bindings, this is not strictly necessary, so we avoid the +# dependency here. Set to false and provide rustfmt in nativeBuildInputs, if you need it, e.g. +# if you include the generated code in the output via postInstall. +, useFakeRustfmt ? true +, usePgTestCheckFeature ? true +, ... +} @ args: +let + rustfmtInNativeBuildInputs = lib.lists.any (dep: lib.getName dep == "rustfmt") (args.nativeBuildInputs or []); +in + +assert lib.asserts.assertMsg ((args.installPhase or "") == "") + "buildPgrxExtensions overwrites the installPhase, so providing one does nothing"; +assert lib.asserts.assertMsg ((args.buildPhase or "") == "") + "buildPgrxExtensions overwrites the buildPhase, so providing one does nothing"; +assert lib.asserts.assertMsg (useFakeRustfmt -> !rustfmtInNativeBuildInputs) + "The parameter useFakeRustfmt is set to true, but rustfmt is included in nativeBuildInputs. Either set useFakeRustfmt to false or remove rustfmt from nativeBuildInputs."; +assert lib.asserts.assertMsg (!useFakeRustfmt -> rustfmtInNativeBuildInputs) + "The parameter useFakeRustfmt is set to false, but rustfmt is not included in nativeBuildInputs. Either set useFakeRustfmt to true or add rustfmt from nativeBuildInputs."; + +let + fakeRustfmt = writeShellScriptBin "rustfmt" '' + exit 0 + ''; + maybeDebugFlag = lib.optionalString (buildType != "release") "--debug"; + maybeEnterBuildAndTestSubdir = lib.optionalString (buildAndTestSubdir != null) '' + export CARGO_TARGET_DIR="$(pwd)/target" + pushd "${buildAndTestSubdir}" + ''; + maybeLeaveBuildAndTestSubdir = lib.optionalString (buildAndTestSubdir != null) "popd"; + + pgrxPostgresMajor = lib.versions.major postgresql.version; + preBuildAndTest = '' + export PGRX_HOME=$(mktemp -d) + export PGDATA="$PGRX_HOME/data-${pgrxPostgresMajor}/" + cargo-pgrx pgrx init "--pg${pgrxPostgresMajor}" ${lib.getDev postgresql}/bin/pg_config + echo "unix_socket_directories = '$(mktemp -d)'" > "$PGDATA/postgresql.conf" + + # This is primarily for Mac or other Nix systems that don't use the nixbld user. + export USER="$(whoami)" + pg_ctl start + createuser -h localhost --superuser --createdb "$USER" || true + pg_ctl stop + ''; + + argsForBuildRustPackage = builtins.removeAttrs args [ "postgresql" "useFakeRustfmt" "usePgTestCheckFeature" ]; + + # so we don't accidentally `(rustPlatform.buildRustPackage argsForBuildRustPackage) // { ... }` because + # we forgot parentheses + finalArgs = argsForBuildRustPackage // { + buildInputs = (args.buildInputs or [ ]) ++ lib.optionals stdenv.hostPlatform.isDarwin [ Security ]; + + nativeBuildInputs = (args.nativeBuildInputs or [ ]) ++ [ + cargo-pgrx + postgresql + pkg-config + rustPlatform.bindgenHook + ] ++ lib.optionals useFakeRustfmt [ fakeRustfmt ]; + + buildPhase = '' + runHook preBuild + + echo "Executing cargo-pgrx buildPhase" + ${preBuildAndTest} + ${maybeEnterBuildAndTestSubdir} + + PGRX_BUILD_FLAGS="--frozen -j $NIX_BUILD_CORES ${builtins.concatStringsSep " " cargoBuildFlags}" \ + ${lib.optionalString stdenv.hostPlatform.isDarwin ''RUSTFLAGS="''${RUSTFLAGS:+''${RUSTFLAGS} }-Clink-args=-Wl,-undefined,dynamic_lookup"''} \ + cargo pgrx package \ + --pg-config ${lib.getDev postgresql}/bin/pg_config \ + ${maybeDebugFlag} \ + --features "${builtins.concatStringsSep " " buildFeatures}" \ + --out-dir "$out" + + ${maybeLeaveBuildAndTestSubdir} + + runHook postBuild + ''; + + preCheck = preBuildAndTest + args.preCheck or ""; + + installPhase = '' + runHook preInstall + + echo "Executing buildPgrxExtension install" + + ${maybeEnterBuildAndTestSubdir} + + cargo-pgrx pgrx stop all + + mv $out/${postgresql}/* $out + rm -rf $out/nix + + ${maybeLeaveBuildAndTestSubdir} + + runHook postInstall + ''; + + PGRX_PG_SYS_SKIP_BINDING_REWRITE = "1"; + CARGO_BUILD_INCREMENTAL = "false"; + RUST_BACKTRACE = "full"; + + checkNoDefaultFeatures = true; + checkFeatures = (args.checkFeatures or [ ]) ++ (lib.optionals usePgTestCheckFeature [ "pg_test" ]) ++ [ "pg${pgrxPostgresMajor}" ]; + }; +in +rustPlatform.buildRustPackage finalArgs diff --git a/nix/cargo-pgrx/default.nix b/nix/cargo-pgrx/default.nix new file mode 100644 index 000000000..2d5545927 --- /dev/null +++ b/nix/cargo-pgrx/default.nix @@ -0,0 +1,70 @@ +{ lib +, darwin +, fetchCrate +, openssl +, pkg-config +, makeRustPlatform +, stdenv +, rust-bin +}: +let + rustVersion = "1.76.0"; + rustPlatform = makeRustPlatform { + cargo = rust-bin.stable.${rustVersion}.default; + rustc = rust-bin.stable.${rustVersion}.default; + }; + generic = + { version + , hash + , cargoHash + }: + rustPlatform.buildRustPackage rec { + pname = "cargo-pgrx"; + inherit version; + src = fetchCrate { + inherit version pname hash; + }; + inherit cargoHash; + nativeBuildInputs = lib.optionals stdenv.hostPlatform.isLinux [ + pkg-config + ]; + buildInputs = lib.optionals stdenv.hostPlatform.isLinux [ + openssl + ] ++ lib.optionals stdenv.hostPlatform.isDarwin [ + darwin.apple_sdk.frameworks.Security + ]; + + OPENSSL_DIR = "${openssl.dev}"; + OPENSSL_INCLUDE_DIR = "${openssl.dev}/include"; + OPENSSL_LIB_DIR = "${openssl.out}/lib"; + PKG_CONFIG_PATH = "${openssl.dev}/lib/pkgconfig"; + preCheck = '' + export PGRX_HOME=$(mktemp -d) + ''; + checkFlags = [ + # requires pgrx to be properly initialized with cargo pgrx init + "--skip=command::schema::tests::test_parse_managed_postmasters" + ]; + meta = with lib; { + description = "Build Postgres Extensions with Rust"; + homepage = "https://github.com/pgcentralfoundation/pgrx"; + changelog = "https://github.com/pgcentralfoundation/pgrx/releases/tag/v${version}"; + license = licenses.mit; + maintainers = with maintainers; [ happysalada ]; + mainProgram = "cargo-pgrx"; + }; + }; +in +{ + cargo-pgrx_0_11_3 = generic { + version = "0.11.3"; + hash = "sha256-UHIfwOdXoJvR4Svha6ud0FxahP1wPwUtviUwUnTmLXU="; + cargoHash = "sha256-j4HnD8Zt9uhlV5N7ldIy9564o9qFEqs5KfXHmnQ1WEw="; + }; + cargo-pgrx_0_12_6 = generic { + version = "0.12.6"; + hash = "sha256-7aQkrApALZe6EoQGVShGBj0UIATnfOy2DytFj9IWdEA="; + cargoHash = "sha256-Di4UldQwAt3xVyvgQT1gUhdvYUVp7n/a72pnX45kP0w="; + }; + inherit rustPlatform; +} diff --git a/nix/ext/pg_graphql.nix b/nix/ext/pg_graphql.nix index 08451e3cd..1a900a821 100644 --- a/nix/ext/pg_graphql.nix +++ b/nix/ext/pg_graphql.nix @@ -1,5 +1,8 @@ -{ lib, stdenv, fetchFromGitHub, postgresql, buildPgrxExtension_0_11_3, cargo }: - +{ lib, stdenv, fetchFromGitHub, postgresql, buildPgrxExtension_0_11_3, cargo, rust-bin }: +let + rustVersion = "1.76.0"; + cargo = rust-bin.stable.${rustVersion}.default; +in buildPgrxExtension_0_11_3 rec { pname = "pg_graphql"; version = "1.5.7"; diff --git a/nix/ext/pg_jsonschema.nix b/nix/ext/pg_jsonschema.nix index 6606d3aaa..f79efcd95 100644 --- a/nix/ext/pg_jsonschema.nix +++ b/nix/ext/pg_jsonschema.nix @@ -1,5 +1,8 @@ -{ lib, stdenv, fetchFromGitHub, postgresql, buildPgrxExtension_0_11_3, cargo }: - +{ lib, stdenv, fetchFromGitHub, postgresql, buildPgrxExtension_0_11_3, cargo, rust-bin }: +let + rustVersion = "1.76.0"; + cargo = rust-bin.stable.${rustVersion}.default; +in buildPgrxExtension_0_11_3 rec { pname = "pg_jsonschema"; version = "0.3.1"; diff --git a/nix/ext/wrappers/default.nix b/nix/ext/wrappers/default.nix index 98d6a7cbc..d44af6b50 100644 --- a/nix/ext/wrappers/default.nix +++ b/nix/ext/wrappers/default.nix @@ -8,8 +8,12 @@ , cargo , darwin , jq +, rust-bin }: - +let + rustVersion = "1.76.0"; + cargo = rust-bin.stable.${rustVersion}.default; +in buildPgrxExtension_0_11_3 rec { pname = "supabase-wrappers"; version = "0.4.2";