-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Simplify the Nix derivation as much as possible. (#144)
### What The Nix derivation code we have is quite sophisticated, and does things we don't need. I want to clean it up so we can reuse it as much as possible in other repositories to take advantage of the caching (now we have caching working well) and the small Docker image size. ### How I have tried to simplify it in a couple of ways: 1. Extracting out the clever Rust cross-compilation stuff into its own file which doesn't need to be touched very often. 2. Merging what's left of _cargo-build.nix_ and _ndc-agent.nix_.
- Loading branch information
1 parent
627b896
commit d780eaf
Showing
6 changed files
with
124 additions
and
188 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
# This is a function that returns a derivation for the compiled Rust project. | ||
{ rust }: | ||
let | ||
inherit (rust) pkgs; | ||
|
||
buildArgs = { | ||
pname = "ndc-postgres"; | ||
|
||
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 | ||
|| rust.craneLib.filterCargoSources path type; | ||
in | ||
pkgs.lib.cleanSourceWith { src = rust.craneLib.path ./..; filter = isSourceFile; }; | ||
|
||
buildInputs = [ | ||
pkgs.openssl | ||
] ++ pkgs.lib.optionals pkgs.stdenv.hostPlatform.isDarwin [ | ||
pkgs.libiconv | ||
pkgs.darwin.apple_sdk.frameworks.Security | ||
pkgs.darwin.apple_sdk.frameworks.SystemConfiguration | ||
]; | ||
|
||
nativeBuildInputs = [ | ||
pkgs.pkg-config # required for non-static builds | ||
pkgs.protobuf # required by opentelemetry-proto, a dependency of axum-tracing-opentelemetry | ||
]; | ||
}; | ||
|
||
# Build the dependencies first. | ||
cargoArtifacts = rust.craneLib.buildDepsOnly buildArgs; | ||
|
||
# Then build the crate. | ||
crate = rust.craneLib.buildPackage | ||
(buildArgs // { | ||
inherit cargoArtifacts; | ||
doCheck = false; | ||
}); | ||
in | ||
# Override the derivation to add cross-compilation and static linking environment variables. | ||
crate.overrideAttrs (previous: rust.buildEnv // { | ||
# We also have to override the `cargoArtifacts` derivation with the same changes. | ||
cargoArtifacts = previous.cargoArtifacts.overrideAttrs (previous: rust.buildEnv); | ||
}) |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
# 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 pkgs rustToolchain craneLib buildEnv; } |