Skip to content

Instantly share code, notes, and snippets.

@reckenrode
Last active May 23, 2023 12:51
Show Gist options
  • Save reckenrode/44d1bb807effac7e453d02040483a53b to your computer and use it in GitHub Desktop.
Save reckenrode/44d1bb807effac7e453d02040483a53b to your computer and use it in GitHub Desktop.
flake.nix for fyrox/crane discussion
{
description = "Build a cargo project";
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
crane = {
url = "github:ipetkov/crane";
inputs.nixpkgs.follows = "nixpkgs";
};
fenix = {
url = "github:nix-community/fenix";
inputs.nixpkgs.follows = "nixpkgs";
inputs.rust-analyzer-src.follows = "";
};
flake-utils.url = "github:numtide/flake-utils";
advisory-db = {
url = "github:rustsec/advisory-db";
flake = false;
};
};
outputs = { self, nixpkgs, crane, fenix, flake-utils, advisory-db, ... }:
flake-utils.lib.eachDefaultSystem (system:
let
pkgs = import nixpkgs {
inherit system;
};
inherit (pkgs) lib;
sdkStdenv = pkgs.darwin.apple_sdk_11_0.stdenv.override { extraBuildInputs = [ pkgs.darwin.apple_sdk_11_0.frameworks.CoreFoundation ]; };
craneLib = crane.lib.${system}.overrideToolchain (pkgs.buildEnv {
name = "rust-11.0-sdk";
paths = [
(pkgs.rustc.override { stdenv = sdkStdenv; })
(pkgs.cargo.override { stdenv = sdkStdenv; })
];
});
src = craneLib.cleanCargoSource (craneLib.path ./.);
# Common arguments can be set here to avoid repeating them later
commonArgs = {
stdenv = sdkStdenv;
inherit src;
nativeBuildInputs = [ pkgs.darwin.apple_sdk_11_0.rustPlatform.bindgenHook ];
buildInputs = [
] ++ lib.optionals pkgs.stdenv.isDarwin (with pkgs.darwin.apple_sdk_11_0.frameworks; [
# Additional darwin specific inputs can be set here
pkgs.libiconv
AppKit
ApplicationServices
AudioUnit
AudioToolbox
CoreAudio
CoreFoundation
CoreGraphics
CoreServices
CoreVideo
Foundation
IOKit
OpenAL
OpenGL
QuartzCore
]);
};
craneLibLLvmTools = craneLib.overrideToolchain
(fenix.packages.${system}.complete.withComponents [
"cargo"
"llvm-tools"
"rustc"
]);
# Build *just* the cargo dependencies, so we can reuse
# all of that work (e.g. via cachix) when running in CI
cargoArtifacts = craneLib.buildDepsOnly commonArgs;
# Build the actual crate itself, reusing the dependency
# artifacts from above.
basic3d-crane-edit = craneLib.buildPackage (commonArgs // {
pname = "basic3d-edit";
version = "0.1.0";
inherit cargoArtifacts;
src = craneLib.cleanCargoSource (craneLib.path ./.);
cargoExtraArgs = "-p editor";
cargoVendorDir = craneLib.vendorCargoDeps { cargoLock = ./Cargo.lock; src = ./.; };
});
in
{
checks = {
# Build the crate as part of `nix flake check` for convenience
inherit basic3d-crane-edit;
# Run clippy (and deny all warnings) on the crate source,
# again, resuing the dependency artifacts from above.
#
# Note that this is done as a separate derivation so that
# we can block the CI if there are issues here, but not
# prevent downstream consumers from building our crate by itself.
my-crate-clippy = craneLib.cargoClippy (commonArgs // {
inherit cargoArtifacts;
cargoClippyExtraArgs = "--all-targets -- --deny warnings";
});
my-crate-doc = craneLib.cargoDoc (commonArgs // {
inherit cargoArtifacts;
});
# Check formatting
my-crate-fmt = craneLib.cargoFmt {
inherit src;
};
# Audit dependencies
my-crate-audit = craneLib.cargoAudit {
inherit src advisory-db;
};
# Run tests with cargo-nextest
# Consider setting `doCheck = false` on `my-crate` if you do not want
# the tests to run twice
my-crate-nextest = craneLib.cargoNextest (commonArgs // {
inherit cargoArtifacts;
partitions = 1;
partitionType = "count";
});
} // lib.optionalAttrs (system == "x86_64-linux") {
# NB: cargo-tarpaulin only supports x86_64 systems
# Check code coverage (note: this will not upload coverage anywhere)
my-crate-coverage = craneLib.cargoTarpaulin (commonArgs // {
inherit cargoArtifacts;
});
};
packages = {
default = basic3d-crane-edit;
my-crate-llvm-coverage = craneLibLLvmTools.cargoLlvmCov (commonArgs // {
inherit cargoArtifacts;
});
};
apps.default = flake-utils.lib.mkApp {
drv = basic3d-crane-edit;
};
devShells.default = pkgs.mkShell {
inputsFrom = builtins.attrValues self.checks.${system};
# Additional dev-shell environment variables can be set directly
# MY_CUSTOM_DEVELOPMENT_VAR = "something else";
# Extra inputs can be added here
nativeBuildInputs = with pkgs; [
cargo
rustc
];
};
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment