Skip to content

Commit

Permalink
Use nix stuff from ndc-postgres
Browse files Browse the repository at this point in the history
  • Loading branch information
danieljharvey committed Nov 15, 2023
1 parent 26b01db commit 1cd19d1
Show file tree
Hide file tree
Showing 6 changed files with 151 additions and 258 deletions.
105 changes: 38 additions & 67 deletions flake.nix
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
description = "SQLServer data connector";
description = "PostgreSQL data connector";

inputs = {
nixpkgs.url = github:NixOS/nixpkgs/nixos-unstable;
Expand Down Expand Up @@ -27,75 +27,46 @@
overlays = [ rust-overlay.overlays.default ];
};

# Edit ./nix/ndc-agent.nix to adjust library and buildtime
# dependencies or other build configuration for sqlserver-agent
crateExpression = import ./nix/ndc-agent.nix;

# cockroachExpression = import ./nix/cockroach-agent.nix;
cargoBuild = import ./nix/cargo-build.nix;

# create binaries for a given NDC
make-binaries = (binary-name: {
inherit binary-name;
rust = import ./nix/rust.nix {
inherit nixpkgs rust-overlay crane localSystem;
};
in
{
packages = {
# a binary for whichever is the local computer
local-system = cargoBuild {
inherit binary-name crateExpression nixpkgs crane rust-overlay localSystem;
};
default = rust.callPackage ./nix/app.nix { };

# cross compiler an x86_64 linux binary
x86_64-linux = cargoBuild {
inherit binary-name crateExpression nixpkgs crane rust-overlay localSystem;
x86_64-linux = (import ./nix/rust.nix {
inherit nixpkgs rust-overlay crane localSystem;
crossSystem = "x86_64-linux";
};
}).callPackage ./nix/app.nix
{ };
# cross compile a aarch64 linux binary
aarch64-linux = cargoBuild {
inherit binary-name crateExpression nixpkgs crane rust-overlay localSystem;
aarch64-linux = (import ./nix/rust.nix {
inherit nixpkgs rust-overlay crane localSystem;
crossSystem = "aarch64-linux";
}).callPackage ./nix/app.nix
{ };

# docker for local system
docker = pkgs.callPackage ./nix/docker.nix {
package = self.packages.${localSystem}.default;
image-name = "ghcr.io/hasura/ndc-sqlserver";
tag = "dev";
};
# docker for x86_64-linux
docker-x86_64-linux = pkgs.callPackage ./nix/docker.nix {
package = self.packages.${localSystem}.x86_64-linux;
architecture = "amd64";
image-name = "ghcr.io/hasura/ndc-sqlserver-x86_64";
};
# docker for aarch64-linux
docker-aarch64-linux = pkgs.callPackage ./nix/docker.nix {
package = self.packages.${localSystem}.aarch64-linux;
architecture = "arm64";
image-name = "ghcr.io/hasura/ndc-sqlserver-aarch64";
};
});

# given the binaries, return the flake targets that build Docker etc
make-packages =
(ndc-binaries:
let name = ndc-binaries.binary-name; in {
# binary compiled on local system
"${name}" = ndc-binaries.local-system;
# binary compiled for x86_64-linux
"${name}-x86_64-linux" = ndc-binaries.x86_64-linux;
# binary compiled for aarch64-linux
"${name}-aarch64-linux" = ndc-binaries.aarch64-linux;
# docker for local system
"${name}-docker" = pkgs.callPackage ./nix/docker.nix {
ndc-agent = ndc-binaries.local-system;
binary-name = name;
image-name = "ghcr.io/hasura/${name}";
tag = "dev";
};
# docker for x86_64-linux
"${name}-docker-x86_64-linux" = pkgs.callPackage ./nix/docker.nix {
ndc-agent = ndc-binaries.x86_64-linux;
architecture = "amd64";
binary-name = name;
image-name = "ghcr.io/hasura/${name}-x86_64";
};
# docker for aarch64-linux
"${name}-docker-aarch64-linux" = pkgs.callPackage ./nix/docker.nix {
ndc-agent = ndc-binaries.aarch64-linux;
architecture = "arm64";
binary-name = name;
image-name = "ghcr.io/hasura/${name}-aarch64";
};
});

sqlserver-binaries = make-binaries "ndc-sqlserver";

inherit (sqlserver-binaries.local-system) cargoArtifacts rustToolchain craneLib buildArgs;

in
{
packages = builtins.foldl' (x: y: x // y) { } [
(make-packages sqlserver-binaries)
] // {
default = sqlserver-binaries.local-system;

publish-docker-image = pkgs.writeShellApplication {
name = "publish-docker-image";
Expand All @@ -106,7 +77,7 @@

checks = {
# Build the crate as part of `nix flake check`
ndc-sqlserver = sqlserver-binaries.local-system;
ndc-sqlserver = self.packages.${localSystem}.default;
};

formatter = pkgs.nixpkgs-fmt;
Expand All @@ -131,14 +102,14 @@
pkgs.pkg-config
pkgs.rnix-lsp
pkgs.skopeo
pkgs.nodePackages.prettier
rustToolchain
rust.rustToolchain
] ++ (
pkgs.lib.optionals
pkgs.stdenv.isLinux
[
pkgs.heaptrack
pkgs.linuxPackages_latest.perf
pkgs.mold-wrapped
pkgs.valgrind
]
);
Expand Down
49 changes: 49 additions & 0 deletions nix/app.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# This is a function that returns a derivation for the compiled Rust project.
{ craneLib
, lib
, stdenv
, openssl
, libiconv
, pkg-config
, protobuf
, darwin
}:
let
buildArgs = {
pname = "ndc-sqlserver";

src =
let
isJsonFile = path: _type: builtins.match ".*json" path != null;
isSqlFile = path: _type: builtins.match ".*sql" path != null;
isSourceFile = path: type:
isJsonFile path type
|| isSqlFile path type
|| craneLib.filterCargoSources path type;
in
lib.cleanSourceWith { src = craneLib.path ./..; filter = isSourceFile; };

buildInputs = [
openssl
] ++ lib.optionals stdenv.hostPlatform.isDarwin [
libiconv
darwin.apple_sdk.frameworks.Security
darwin.apple_sdk.frameworks.SystemConfiguration
];

nativeBuildInputs = [
pkg-config # required for non-static builds
protobuf # required by opentelemetry-proto, a dependency of axum-tracing-opentelemetry
];
};

# Build the dependencies first.
cargoArtifacts = craneLib.buildDepsOnly buildArgs;
in
# Then build the crate.
craneLib.buildPackage
(buildArgs // {
inherit cargoArtifacts;
doCheck = false;
cargoExtraArgs = "--locked --bin ${buildArgs.pname}";
})
105 changes: 0 additions & 105 deletions nix/cargo-build.nix

This file was deleted.

11 changes: 5 additions & 6 deletions nix/docker.nix
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
# This is a function that returns a derivation for a docker image.
{ ndc-agent
, binary-name
, dockerTools
{ dockerTools
, lib
, architecture ? null
, package
, image-name
, architecture ? null
, tag ? null # defaults to the output hash
, extraConfig ? { } # see config options at: https://github.com/moby/moby/blob/master/image/spec/v1.2.md#image-json-field-descriptions
}:
Expand All @@ -13,10 +12,10 @@ let
args = {
name = image-name;
created = "now";
contents = [ ndc-agent ];
contents = [ package ];
config = {
Entrypoint = [
"/bin/${binary-name}"
"/bin/${package.pname}"
];
ExposedPorts = { "8100/tcp" = { }; };
} // extraConfig;
Expand Down
Loading

0 comments on commit 1cd19d1

Please sign in to comment.