diff --git a/flake.lock b/flake.lock new file mode 100644 index 0000000..2ce998c --- /dev/null +++ b/flake.lock @@ -0,0 +1,48 @@ +{ + "nodes": { + "nixpkgs": { + "locked": { + "lastModified": 1733212471, + "narHash": "sha256-M1+uCoV5igihRfcUKrr1riygbe73/dzNnzPsmaLCmpo=", + "owner": "nixos", + "repo": "nixpkgs", + "rev": "55d15ad12a74eb7d4646254e13638ad0c4128776", + "type": "github" + }, + "original": { + "owner": "nixos", + "ref": "nixos-unstable", + "repo": "nixpkgs", + "type": "github" + } + }, + "root": { + "inputs": { + "nixpkgs": "nixpkgs", + "rust-overlay": "rust-overlay" + } + }, + "rust-overlay": { + "inputs": { + "nixpkgs": [ + "nixpkgs" + ] + }, + "locked": { + "lastModified": 1733366051, + "narHash": "sha256-Zlas3LFqrW8bVVrZYgkzS4VNkZgtZ/hsbYhO0GtKLys=", + "owner": "oxalica", + "repo": "rust-overlay", + "rev": "ba5ed0362eaae83fe8925a2d5cfcf356ff22f70f", + "type": "github" + }, + "original": { + "owner": "oxalica", + "repo": "rust-overlay", + "type": "github" + } + } + }, + "root": "root", + "version": 7 +} diff --git a/flake.nix b/flake.nix new file mode 100644 index 0000000..4bf11dd --- /dev/null +++ b/flake.nix @@ -0,0 +1,35 @@ +{ + inputs = { + nixpkgs.url = "github:nixos/nixpkgs?ref=nixos-unstable"; + + rust-overlay.url = "github:oxalica/rust-overlay"; + rust-overlay.inputs.nixpkgs.follows = "nixpkgs"; + }; + + outputs = { self, nixpkgs, rust-overlay }: let + systems = [ + "x86_64-linux" + "x86_64-darwin" + "aarch64-linux" + "aarch64-darwin" + "armv7l-linux" + ]; + + forAllSystems = f: nixpkgs.lib.genAttrs systems (system: let + pkgs = import nixpkgs { + inherit system; + overlays = [ + (import rust-overlay) + ]; + }; + + in f system pkgs); + + inherit (nixpkgs) lib; + in { + devShells = forAllSystems (system: pkgs: { + rust = pkgs.callPackage ./generic/rust/shell.nix { }; + rust-nightly = pkgs.callPackage ./generic/rust/shell.nix { rust-channel = "nightly"; }; + }); + }; +} diff --git a/generic/rust/shell.nix b/generic/rust/shell.nix new file mode 100644 index 0000000..73cfad6 --- /dev/null +++ b/generic/rust/shell.nix @@ -0,0 +1,69 @@ +{ + lib +, stdenv +, mkShell + +, rust-channel ? "stable" +, rust-bin +, rust-toolchain ? rust-bin.${rust-channel}.latest.default.override { + extensions = [ "rust-src" "rust-analyzer" "rust-std" ]; + } + +, enableSccache ? true +, pkgsBuildBuild + +# Rust tools +, cargo-deny +, cargo-insta +, cargo-nextest + +# Commonly used libraries +, pkg-config +, openssl +, zstd +, systemdLibs +}: +mkShell { + packages = [ + rust-toolchain + + cargo-deny + cargo-insta + cargo-nextest + + pkg-config + ]; + + buildInputs = [ + openssl + zstd + systemdLibs + ]; + + env = { + RUST_SRC_PATH = "${rust-toolchain}/lib/rustlib/src/rust/library"; + } // (lib.optionalAttrs (enableSccache) { + RUSTC_WRAPPER = "${pkgsBuildBuild.sccache}/bin/sccache"; + }); + + # https://hoverbear.org/blog/rust-bindgen-in-nix/ + shellHook = let + clangArgs = lib.concatStringsSep " " [ + "-idirafter ${stdenv.cc.cc}/lib/clang/${lib.getVersion stdenv.cc.cc}/include" + ]; + gccArgs = lib.concatStringsSep " " [ + "-isystem ${stdenv.cc.cc}/include/c++/${lib.getVersion stdenv.cc.cc}" + "-isystem ${stdenv.cc.cc}/include/c++/${lib.getVersion stdenv.cc.cc}/${stdenv.hostPlatform.config}" + "-idirafter ${stdenv.cc.cc}/lib/gcc/${stdenv.hostPlatform.config}/${lib.getVersion stdenv.cc.cc}/include" + ]; + in '' + export BINDGEN_EXTRA_CLANG_ARGS="${lib.concatStringsSep " " [ + "$(< ${stdenv.cc}/nix-support/libc-crt1-cflags)" + "$(< ${stdenv.cc}/nix-support/libc-cflags)" + "$(< ${stdenv.cc}/nix-support/cc-cflags)" + "$(< ${stdenv.cc}/nix-support/libcxx-cxxflags)" + "${lib.optionalString stdenv.cc.isClang clangArgs}" + "${lib.optionalString stdenv.cc.isGNU gccArgs}" + ]}" + ''; +}