Rust/flake.nix

186 lines
6.7 KiB
Nix
Raw Normal View History

2023-05-13 13:21:58 -04:00
{
2023-05-13 14:37:05 -04:00
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
utils.url = "github:numtide/flake-utils";
naersk = {
url = "github:nix-community/naersk";
inputs.nixpkgs.follows = "nixpkgs";
};
# Used for rust compiler
rust-overlay = {
url = "github:oxalica/rust-overlay";
inputs.nixpkgs.follows = "nixpkgs";
};
2023-05-13 15:11:02 -04:00
# Advisory db from rust-sec
advisory-db = {
url = "github:RustSec/advisory-db";
flake = false;
};
2023-05-13 14:37:05 -04:00
};
2023-05-13 13:21:58 -04:00
2023-05-13 14:37:05 -04:00
description = "Rust Toolchain and Utility Flake";
2023-05-13 13:21:58 -04:00
2023-05-13 14:37:05 -04:00
outputs = { self, nixpkgs, utils, ... }@inputs:
2023-05-13 15:11:02 -04:00
with builtins;
let
sources = fromJSON (readFile ./sources/sources.json);
rustPackageNames = attrNames sources ++ [ "cargo-release" "cargo-deny" ];
# Build the rust packages we'll be using
in (utils.lib.eachDefaultSystem (system: {
2023-05-13 14:37:05 -04:00
packages = let
pkgs = import nixpkgs {
inherit system;
overlays = [ (import inputs.rust-overlay) ];
};
rust = pkgs.rust-bin.stable.latest.default.override {
extensions = [ "llvm-tools-preview" ];
};
naersk-lib = inputs.naersk.lib."${system}".override {
rustc = rust;
cargo = rust;
};
2023-05-13 15:11:02 -04:00
in (mapAttrs (name: source:
2023-05-13 14:37:05 -04:00
naersk-lib.buildPackage {
pname = source.pname;
src = pkgs.fetchCrate source;
2023-05-16 16:20:23 -04:00
buildInputs = with pkgs;
[ pkg-config openssl ] ++ lib.optionals stdenv.isDarwin [
darwin.apple_sdk.frameworks.Security
pkgs.libiconv
darwin.apple_sdk.frameworks.SystemConfiguration
];
2023-05-13 15:11:02 -04:00
}) sources) //
# Packages that naersk has trouble building due to https://github.com/nix-community/naersk/issues/263
{
cargo-release = pkgs.cargo-release;
cargo-deny = pkgs.cargo-deny;
};
})) //
# Now provide our builder functions
{
# Build a rust flake with a single crate
single = { src, crateName, sharedDeps ? (system: [ ])
2023-10-21 18:37:11 -04:00
, sharedNativeDeps ? (system: [ ]), copyBins ? true, copyLibs ? false
, postInstall ? false, }:
2023-05-13 15:11:02 -04:00
utils.lib.eachDefaultSystem (system:
let
pkgs = import nixpkgs {
inherit system;
overlays = [ (import inputs.rust-overlay) ];
};
rust = pkgs.rust-bin.stable.latest.default.override {
extensions = [ "llvm-tools-preview" ];
};
naersk-lib = inputs.naersk.lib."${system}".override {
rustc = rust;
cargo = rust;
};
2023-06-08 21:31:56 -04:00
buildInputs = (sharedDeps pkgs) ++ (with pkgs;
2023-05-16 16:17:29 -04:00
[ openssl ] ++ lib.optionals stdenv.isDarwin [
darwin.apple_sdk.frameworks.Security
pkgs.libiconv
darwin.apple_sdk.frameworks.SystemConfiguration
]);
2023-06-08 21:31:56 -04:00
nativeBuildInputs = (sharedNativeDeps pkgs)
2023-07-21 22:47:17 -04:00
++ (with pkgs; [ pkg-config cmake ]) ++ (with pkgs;
lib.optionals stdenv.isDarwin [ fixDarwinDylibNames ]);
2023-05-13 15:11:02 -04:00
devBase = with pkgs;
[
# Build tools
rust-analyzer
gnuplot
# git tooling
gitFull
pre-commit
git-lfs
git-cliff
# Formatters
nixfmt
python311Packages.mdformat
] ++ buildInputs ++ nativeBuildInputs
# Linux specific tools
++ (lib.optionals pkgs.stdenv.isLinux [
2023-06-08 21:56:18 -04:00
# Profiling
perf-tools
]) ++ map (x: self.packages.${system}.${x}) rustPackageNames;
2023-05-13 15:11:02 -04:00
in rec {
# Main binary
packages.${crateName} = naersk-lib.buildPackage {
pname = "${crateName}";
2023-10-21 18:37:11 -04:00
inherit buildInputs nativeBuildInputs copyBins copyLibs
postInstall;
2023-05-13 15:11:02 -04:00
root = src;
};
# binary + tests
packages.tests.${crateName} = naersk-lib.buildPackage {
pname = "${crateName}";
2023-06-11 02:29:05 -04:00
inherit buildInputs nativeBuildInputs copyBins copyLibs;
2023-05-13 15:11:02 -04:00
root = src;
doCheck = true;
};
# Docs
packages.docs.${crateName} = naersk-lib.buildPackage {
pname = "${crateName}";
2023-06-11 02:29:05 -04:00
inherit buildInputs nativeBuildInputs copyBins copyLibs;
2023-05-13 15:11:02 -04:00
root = src;
dontBuild = true;
doDoc = true;
doDocFail = true;
};
# Set the default package to the main binary
defaultPackage = packages.${crateName};
# CI tasks
packages.lints = {
# lint formatting
format.${crateName} = with import nixpkgs { inherit system; };
stdenv.mkDerivation {
name = "format lint";
src = src;
2023-06-11 02:29:05 -04:00
inherit buildInputs copyBins copyLibs;
2023-05-13 15:11:02 -04:00
nativeBuildInputs = with pkgs;
[ rust-bin.stable.latest.default ] ++ nativeBuildInputs;
dontConfigure = true;
2023-05-13 15:11:02 -04:00
buildPhase = "cargo fmt -- --check";
installPhase = "mkdir -p $out; echo 'done'";
};
# audit against stored advisory db
audit.${crateName} = with import nixpkgs { inherit system; };
stdenv.mkDerivation {
name = "audit lint";
src = src;
2023-06-11 02:29:05 -04:00
inherit buildInputs copyBins copyLibs;
2023-05-13 15:11:02 -04:00
nativeBuildInputs = with pkgs;
[ rust-bin.stable.latest.default ] ++ nativeBuildInputs;
dontConfigure = true;
2023-05-13 15:11:02 -04:00
buildPhase = ''
export HOME=$TMP
mkdir -p ~/.cargo
cp -r ${advisory-db} ~/.cargo/advisory-db
cargo audit -n
'';
installPhase = "mkdir -p $out; echo 'done'";
};
# Clippy
clippy.${crateName} = naersk-lib.buildPackage {
pname = "${crateName}";
root = src;
2023-06-11 02:29:05 -04:00
inherit buildInputs nativeBuildInputs copyBins copyLibs;
2023-05-13 15:11:02 -04:00
cargoTestCommands = (old: [ "cargo $cargo_options clippy" ]);
doCheck = true;
dontBuild = true;
};
};
# Development environments
devShell = pkgs.mkShell {
inputsFrom = builtins.attrValues packages;
buildInputs = [ rust ] ++ devBase ++ buildInputs
++ nativeBuildInputs;
2023-05-13 15:11:02 -04:00
};
});
};
2023-05-13 13:21:58 -04:00
}