Skip to content

Commit

Permalink
wip: github actions - nix
Browse files Browse the repository at this point in the history
  • Loading branch information
pranshi06 committed Sep 19, 2024
1 parent 51e22f2 commit b1cf331
Show file tree
Hide file tree
Showing 5 changed files with 139 additions and 192 deletions.
56 changes: 56 additions & 0 deletions nix/app.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# This is a function that returns a derivation for the compiled Rust project.
{ craneLib
, lib
, hostPlatform
, openssl
, libiconv
, pkg-config
, protobuf
, darwin
}:
let
buildArgs = {
pname = "ndc-bigquery";
version = "0.1.0";

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; };

strictDeps = true;

# build-time inputs
nativeBuildInputs = [
openssl.dev # required to build Rust crates that can conduct TLS connections
pkg-config # required to find OpenSSL
];

# runtime inputs
buildInputs = [
openssl # required for TLS connections
protobuf # required by opentelemetry-proto, a dependency of axum-tracing-opentelemetry
] ++ lib.optionals hostPlatform.isDarwin [
# macOS-specific dependencies
libiconv
darwin.apple_sdk.frameworks.CoreFoundation
darwin.apple_sdk.frameworks.Security
darwin.apple_sdk.frameworks.SystemConfiguration
];
};

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

This file was deleted.

31 changes: 24 additions & 7 deletions nix/docker.nix
Original file line number Diff line number Diff line change
@@ -1,24 +1,41 @@
# 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:/moby/moby/blob/master/image/spec/v1.2.md#image-json-field-descriptions
}:

let
seconds = 1000 * 1000 * 1000; # nanoseconds in 1 second
args = {
name = image-name;
created = "now";
contents = [ ndc-agent ];
contents = [ package ];
config = {
Entrypoint = [
"/bin/${binary-name}"
"/bin/${package.pname}"
];
Cmd = [
"serve"
];
Env = [
''HASURA_CONFIGURATION_DIRECTORY=/etc/connector''
];
ExposedPorts = { "8100/tcp" = { }; };
ExposedPorts = { "8080/tcp" = { }; };
Healthcheck = {
Test = [
"CMD"
"/bin/${package.pname}"
"check-health"
];
StartInterval = 1 * seconds;
Interval = 5 * seconds;
Timeout = 10 * seconds;
Retries = 3;
};
} // extraConfig;
}
// lib.optionalAttrs (tag != null) {
Expand Down
80 changes: 0 additions & 80 deletions nix/ndc-agent.nix

This file was deleted.

59 changes: 59 additions & 0 deletions nix/rust.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# Sets up our Rust toolchain and Crane for cross-compilation.
# This is mostly a copy of the example provided at:
# https://crane.dev/examples/cross-rust-overlay.html
{ nixpkgs
, rust-overlay
, crane
, localSystem
, crossSystem ? localSystem
}:
let
pkgs = import nixpkgs {
inherit crossSystem localSystem;
overlays = [ rust-overlay.overlays.default ];
};

lib = pkgs.pkgsBuildHost.lib;

# Converts host system string for use in environment variable names
envCase = triple: lib.strings.toUpper (builtins.replaceStrings [ "-" ] [ "_" ] triple);

# `hostPlatform` is the cross-compilation output platform;
# `buildPlatform` is the platform we are compiling on
buildPlatform = pkgs.stdenv.buildPlatform;
hostPlatform = pkgs.stdenv.hostPlatform;

# When possibly cross-compiling we get several versions of nixpkgs of the
# form, `pkgs.pkgs<where it runs><platform it produces outputs for>`. We use
# `pkgs.pkgsBuildHost` to get packages that run at build time (so run on the
# build platform), and that produce outputs for the host platform which is the
# cross-compilation target.
rustBin = pkgs.pkgsBuildHost.rust-bin.fromRustupToolchainFile ../rust-toolchain.toml;
rustToolchain = rustBin.override { targets = [ hostPlatform.config ]; };
craneLib = (crane.mkLib pkgs).overrideToolchain rustToolchain;

buildEnv = {
CARGO_BUILD_TARGET = hostPlatform.config;
"CARGO_TARGET_${envCase hostPlatform.config}_LINKER" = "${pkgs.stdenv.cc.targetPrefix}cc";

# This environment variable may be necessary if any of your dependencies use
# a build-script which invokes the `cc` crate to build some other code. The
# `cc` crate should automatically pick up on our target-specific linker
# above, but this may be necessary if the build script needs to compile and
# run some extra code on the build system.
HOST_CC = "${pkgs.stdenv.cc.nativePrefix}cc";
};
in
{
inherit rustToolchain;

callPackage = (package: args:
# Call the package, providing `craneLib` as an extra.
let crate = pkgs.callPackage package (args // { inherit craneLib; });
in
# Override the derivation to add cross-compilation environment variables.
crate.overrideAttrs (previous: buildEnv // {
# We also have to override the `cargoArtifacts` derivation with the same changes.
cargoArtifacts = previous.cargoArtifacts.overrideAttrs (previous: buildEnv);
}));
}

0 comments on commit b1cf331

Please sign in to comment.