Skip to content

Commit

Permalink
refactor(flake): separate package into file, and remove flake-utils
Browse files Browse the repository at this point in the history
  • Loading branch information
Ovyerus committed Jul 6, 2024
1 parent 8f599ea commit 1ce5a97
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 97 deletions.
55 changes: 9 additions & 46 deletions flake.lock

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

72 changes: 21 additions & 51 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

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

crane = {
url = "github:ipetkov/crane";
Expand All @@ -13,63 +12,34 @@
rust-overlay = {
url = "github:oxalica/rust-overlay";
inputs.nixpkgs.follows = "nixpkgs";
inputs.flake-utils.follows = "flake-utils";
};
};

outputs = {
nixpkgs,
rust-overlay,
flake-utils,
crane,
...
}:
flake-utils.lib.eachDefaultSystem (
system: let
# TODO: musl https://github.com/ipetkov/crane/blob/master/examples/cross-musl/flake.nix
pkgs = import nixpkgs {
inherit system;
overlays = [(import rust-overlay)];
};

# TODO: cross compilation
craneLib = crane.lib.${system};
# TODO: lock
rust = pkgs.rust-bin.stable.latest.default.override {
extensions = ["rust-src"];
};

stdenv =
if pkgs.stdenv.isLinux
then pkgs.stdenv
else pkgs.clangStdenv;

commonArgs = {
src = craneLib.cleanCargoSource (craneLib.path ./.);

buildInputs = with pkgs;
[]
++ (lib.optional stdenv.isDarwin [libiconvReal]);

nativeBuildInputs = with pkgs;
[pkg-config]
++ (lib.optional stdenv.isLinux [openssl.dev])
++ (lib.optional stdenv.isDarwin (with darwin.apple_sdk; [
frameworks.AppKit
frameworks.CoreFoundation
]));
};

artifacts = craneLib.buildDepsOnly commonArgs;

bandsnatch = craneLib.buildPackage (commonArgs // {inherit artifacts;});
in {
packages.default = bandsnatch;
devShells.default = with pkgs;
mkShell {
nativeBuildInputs = commonArgs.nativeBuildInputs;
buildInputs = [rust] ++ commonArgs.buildInputs;
};
}
}: 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);
in
pkgs.callPackage ./package.nix {inherit craneLib;};
in {
packages = defaultForSystems mkBandsnatch;

devShells = defaultForSystems (
pkgs: pkgs.mkShell {inputsFrom = [(mkBandsnatch pkgs)];}
);
};
}
19 changes: 19 additions & 0 deletions package.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
craneLib,
darwin,
lib,
libiconvReal,
openssl,
pkg-config,
stdenv,
}: let
src = craneLib.cleanCargoSource (craneLib.path ./.);
buildInputs = lib.optionals stdenv.isDarwin [libiconvReal];
nativeBuildInputs =
[pkg-config openssl.dev]
++ lib.optionals stdenv.isDarwin (with darwin.apple_sdk.frameworks; [AppKit CoreFoundation]);
in
craneLib.buildPackage {
inherit src buildInputs nativeBuildInputs;
# strictDeps = true;
}

0 comments on commit 1ce5a97

Please sign in to comment.