Skip to content

Commit

Permalink
refactor(flake): cross packages for each system
Browse files Browse the repository at this point in the history
  • Loading branch information
Ovyerus committed Jul 10, 2024
1 parent adf7cdf commit 6f6fe33
Show file tree
Hide file tree
Showing 2 changed files with 140 additions and 55 deletions.
96 changes: 69 additions & 27 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

99 changes: 71 additions & 28 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -3,43 +3,86 @@

inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";

crane = {
url = "github:ipetkov/crane";
inputs.nixpkgs.follows = "nixpkgs";
};

rust-overlay = {
url = "github:oxalica/rust-overlay";
inputs.nixpkgs.follows = "nixpkgs";
};
fenix.url = "github:nix-community/fenix";
naersk.url = "github:nix-community/naersk/master";
};

outputs = {
fenix,
nixpkgs,
rust-overlay,
crane,
naersk,
...
}: let
forSystems = fn:
nixpkgs.lib.genAttrs [
"aarch64-linux"
"aarch64-darwin"
"x86_64-darwin"
"x86_64-linux"
] (system: fn nixpkgs.legacyPackages.${system});
defaultForSystems = fn: forSystems (pkgs: {default = fn pkgs;});

mkBandsnatch = pkgs: let
rustBin = rust-overlay.lib.mkRustBin {} pkgs;
craneLib = (crane.mkLib pkgs).overrideToolchain (p: rustBin.stable.latest.default);
buildTargets = {
"x86_64-linux" = "x86_64-unknown-linux-musl";
"aarch64-linux" = "aarch64-unknown-linux-musl";
"x86_64-darwin" = "x86_64-apple-darwin";
"aarch64-darwin" = "aarch64-apple-darwin";
};

systems = builtins.attrNames buildTargets;

# forSystems [...system] (system: ...)
forSystems = systems: fn:
nixpkgs.lib.genAttrs systems (system: fn system);

# crossForSystems [...system] (hostSystem: targetSystem: ...)
crossForSystems = systems: fn:
forSystems systems (
hostSystem:
builtins.foldl'
(acc: targetSystem:
acc
// {
"cross-${targetSystem}" = fn hostSystem targetSystem;
})
{default = fn hostSystem hostSystem;}
systems
);

mkBandsnatch = hostSystem: targetSystem: let
rustTarget = buildTargets.${targetSystem};
pkgs = import nixpkgs {system = hostSystem;};
pkgsCross = import nixpkgs {
system = hostSystem;
crossSystem.config = rustTarget;
};
fenixPkgs = fenix.packages.${hostSystem};
toolchain = fenixPkgs.combine [
fenixPkgs.stable.rustc
fenixPkgs.stable.cargo
fenixPkgs.targets.${rustTarget}.stable.rust-std
];

naersk-lib = pkgs.callPackage naersk {
cargo = toolchain;
rustc = toolchain;
};
TARGET_CC = "${pkgsCross.stdenv.cc}/bin/${pkgsCross.stdenv.cc.targetPrefix}cc";
in
pkgs.callPackage ./package.nix {inherit craneLib;};
naersk-lib.buildPackage {
src = ./.;
strictDeps = true;
doCheck = false;

inherit TARGET_CC;

CARGO_BUILD_TARGET = rustTarget;
CARGO_BUILD_RUSTFLAGS = [
"-C"
"target-feature=+crt-static"
"-C"
"linker=${TARGET_CC}"
];
};
in {
packages = defaultForSystems mkBandsnatch;
packages = crossForSystems systems mkBandsnatch;

devShells = defaultForSystems (
pkgs: pkgs.mkShell {inputsFrom = [(mkBandsnatch pkgs)];}
devShells = forSystems systems (
system: let
pkgs = nixpkgs.legacyPackages.${system};
fenixPkgs = fenix.packages.${system};
in {default = pkgs.mkShell {nativeBuildInputs = [fenixPkgs.stable.toolchain];};}
);
};
}

0 comments on commit 6f6fe33

Please sign in to comment.