diff --git a/.gitignore b/.gitignore index 539c22c..b60beab 100644 --- a/.gitignore +++ b/.gitignore @@ -1,7 +1,8 @@ -/target +target/ .DS_Store test*.db* *.o local.db example +result .direnv/ diff --git a/build.sh b/build.sh index ef87193..87fab36 100755 --- a/build.sh +++ b/build.sh @@ -2,6 +2,11 @@ set -xe +cargo build --target x86_64-apple-darwin --features encryption --release cargo build --target aarch64-apple-darwin --features encryption --release cargo build --target aarch64-unknown-linux-gnu --features encryption --release cargo build --target x86_64-unknown-linux-gnu --features encryption --release +cargo build --target x86_64-pc-windows-gnu --features encryption --release + +RUSTFLAGS="-C target-feature=-crt-static" cargo build --target aarch64-unknown-linux-musl --features encryption --release +RUSTFLAGS="-C target-feature=-crt-static" cargo build --target x86_64-unknown-linux-musl --features encryption --release diff --git a/flake.lock b/flake.lock index ccc9bf2..2420cfc 100644 --- a/flake.lock +++ b/flake.lock @@ -1,15 +1,30 @@ { "nodes": { + "crane": { + "locked": { + "lastModified": 1734541973, + "narHash": "sha256-1wIgLmhvtfxbJVnhFHUYhPqL3gpLn5JhiS4maaD9RRk=", + "owner": "ipetkov", + "repo": "crane", + "rev": "fdd502f921936105869eba53db6593fc2a424c16", + "type": "github" + }, + "original": { + "owner": "ipetkov", + "repo": "crane", + "type": "github" + } + }, "flake-utils": { "inputs": { "systems": "systems" }, "locked": { - "lastModified": 1726560853, - "narHash": "sha256-X6rJYSESBVr3hBoH0WbKE5KvhPU5bloyZ2L4K60/fPQ=", + "lastModified": 1731533236, + "narHash": "sha256-l0KFg5HjrsfsO/JpG+r7fRrqm12kzFHyUHqHCVpMMbI=", "owner": "numtide", "repo": "flake-utils", - "rev": "c1dfcf08411b08f6b8615f7d8971a2bfa81d5e8a", + "rev": "11707dc2f618dd54ca8739b309ec4fc024de578b", "type": "github" }, "original": { @@ -20,11 +35,11 @@ }, "nixpkgs": { "locked": { - "lastModified": 1730785428, - "narHash": "sha256-Zwl8YgTVJTEum+L+0zVAWvXAGbWAuXHax3KzuejaDyo=", + "lastModified": 1734424634, + "narHash": "sha256-cHar1vqHOOyC7f1+tVycPoWTfKIaqkoe1Q6TnKzuti4=", "owner": "nixos", "repo": "nixpkgs", - "rev": "4aa36568d413aca0ea84a1684d2d46f55dbabad7", + "rev": "d3c42f187194c26d9f0309a8ecc469d6c878ce33", "type": "github" }, "original": { @@ -36,8 +51,30 @@ }, "root": { "inputs": { + "crane": "crane", "flake-utils": "flake-utils", - "nixpkgs": "nixpkgs" + "nixpkgs": "nixpkgs", + "rust-overlay": "rust-overlay" + } + }, + "rust-overlay": { + "inputs": { + "nixpkgs": [ + "nixpkgs" + ] + }, + "locked": { + "lastModified": 1734747996, + "narHash": "sha256-0DUuObdcPITVOMMymq2y6YlM++QEWXZO3cTm6RGYgL8=", + "owner": "oxalica", + "repo": "rust-overlay", + "rev": "f9086701f5f3d36b8e5f4a3b9c93579ebc2581e6", + "type": "github" + }, + "original": { + "owner": "oxalica", + "repo": "rust-overlay", + "type": "github" } }, "systems": { diff --git a/flake.nix b/flake.nix index 3a17bea..8abdfba 100644 --- a/flake.nix +++ b/flake.nix @@ -3,60 +3,57 @@ inputs = { nixpkgs.url = "github:nixos/nixpkgs?ref=nixos-unstable"; - flake-utils = { - url = "github:numtide/flake-utils"; + flake-utils.url = "github:numtide/flake-utils"; + crane.url = "github:ipetkov/crane"; + rust-overlay = { + url = "github:oxalica/rust-overlay"; + inputs.nixpkgs.follows = "nixpkgs"; }; }; - outputs = { self, nixpkgs, flake-utils }: - flake-utils.lib.eachDefaultSystem - (system: - let - pkgs = import nixpkgs { - inherit system; - config.allowUnsupportedSystem = true; - }; - in - { - formatter = pkgs.nixpkgs-fmt; - devShells.default = - with pkgs; - mkShell { - nativeBuildInputs = [ - pkg-config - rust-bindgen - cmake - ]; - - buildInputs = [ - ] ++ lib.optionals stdenv.isDarwin [ - iconv - darwin.apple_sdk.frameworks.Security - darwin.apple_sdk.frameworks.CoreFoundation - darwin.apple_sdk.frameworks.SystemConfiguration - darwin.apple_sdk.frameworks.CoreServices - ]; - - CARGO_TARGET_X86_64_APPLE_DARWIN_RUSTFLAGS= lib.lists.fold (a: b: "${a} ${b}") "" [ - "-Ctarget-feature=-crt-static" - "-Clink-arg=-target" - "-Clink-arg=x86_64-apple-darwin" - ]; - CC_x86_64_apple_darwin = - "${pkgs.pkgsCross.x86_64-darwin.stdenv.cc}/bin/x86_64-apple-darwin-clang"; - CARGO_TARGET_X86_64_APPLE_DARWIN_LINKER = - "${pkgs.pkgsCross.x86_64-darwin.stdenv.cc}/bin/x86_64-apple-darwin-clang"; - - CARGO_TARGET_X86_64_UNKNOWN_LINUX_MUSL_RUSTFLAGS="-Ctarget-feature=-crt-static"; - CC_x86_64_unknown_linux_gnu = - "${pkgs.pkgsCross.gnu64.stdenv.cc}/bin/x86_64-unknown-linux-gnu-gcc"; - CARGO_TARGET_X86_64_UNKNOWN_LINUX_GNU_LINKER = - "${pkgs.pkgsCross.gnu64.stdenv.cc}/bin/x86_64-unknown-linux-gnu-gcc"; - - CARGO_TARGET_AARCH64_UNKNOWN_LINUX_GNU_RUSTFLAGS="-Ctarget-feature=-crt-static"; - CC_aarch64_unknown_linux_gnu = - "${pkgs.pkgsCross.aarch64-multiplatform.stdenv.cc}/bin/aarch64-unknown-linux-gnu-gcc"; - CARGO_TARGET_AARCH64_UNKNOWN_LINUX_GNU_LINKER = - "${pkgs.pkgsCross.aarch64-multiplatform.stdenv.cc}/bin/aarch64-unknown-linux-gnu-gcc"; - }; - }); + outputs = { self, nixpkgs, flake-utils, crane, rust-overlay }: + flake-utils.lib.eachDefaultSystem (system: + let + pkgs = import nixpkgs { + inherit system; + overlays = [ (import rust-overlay) ]; + }; + toUpper = pkgs.lib.strings.toUpper; + rust = pkgs.rust-bin.fromRustupToolchainFile ./rust-toolchain.toml; + craneLib = pkgs.lib.debug.traveVal ((crane.mkLib pkgs).overrideToolchain rust); + cargofy = s: builtins.replaceStrings [ "-" ] [ "_" ] s; + env = config: cc: { + nativeBuildInputs = [ cc ]; + "CC_${cargofy config}" = "${cc.targetPrefix}cc"; + "CXX_${cargofy config}" = "${cc.targetPrefix}c++"; + "CARGO_TARGET_${toUpper (cargofy config)}_LINKER" = "${cc.targetPrefix}cc"; + }; + in + { + formatter = pkgs.nixpkgs-fmt; + packages.default = pkgs.callPackage ./package.nix { inherit craneLib; }; + packages.musl64 = pkgs.pkgsCross.musl64.callPackage ./package.nix { inherit craneLib; }; + packages.gnu64 = pkgs.pkgsCross.gnu64.callPackage ./package.nix { inherit craneLib; }; + packages.apple64 = pkgs.pkgsCross.x86_64-darwin.callPackage ./package.nix { inherit craneLib; }; + devShells.default = + with pkgs; mkShell (lib.zipAttrsWith + (name: values: if builtins.isList (builtins.head values) then builtins.concatLists values else builtins.head values) + [ + { + nativeBuildInputs = [ + pkg-config + cmake + rust-bindgen + rust + rust-analyzer + ]; + } + (with pkgsCross.mingwW64; env "x86_64-pc-windows-gnu" stdenv.cc) + (with pkgsCross.x86_64-darwin; env targetPlatform.config stdenv.cc) + (with pkgsCross.aarch64-multiplatform; env targetPlatform.config stdenv.cc) + (with pkgsCross.aarch64-multiplatform-musl; env targetPlatform.config stdenv.cc) + (with pkgsCross.gnu64; env targetPlatform.config stdenv.cc) + (with pkgsCross.musl64; env targetPlatform.config stdenv.cc) + (with pkgsCross.aarch64-darwin; env targetPlatform.config stdenv.cc) + ]); + }); } diff --git a/package.nix b/package.nix new file mode 100644 index 0000000..3e18acc --- /dev/null +++ b/package.nix @@ -0,0 +1,43 @@ +{ lib +, stdenv +, craneLib +, pkg-config +, cmake +, apple-sdk +}: +let target = lib.strings.toUpper (builtins.replaceStrings [ "-" ] [ "_" ] stdenv.targetPlatform.config); +in +craneLib.buildPackage { + src = lib.fileset.toSource rec { + root = ./.; + fileset = lib.fileset.unions [ + (craneLib.fileset.commonCargoSources root) + (lib.fileset.fileFilter (file: file.hasExt "h") root) + ]; + }; + + strictDeps = true; + doCheck = false; + + nativeBuildInputs = [ + pkg-config + cmake + stdenv.cc + ]; + + nativeInput = lib.optionals stdenv.isDarwin [ apple-sdk ]; + + CARGO_BUILD_TARGET = stdenv.targetPlatform.config; + + "CARGO_TARGET_${target}_LINKER" = "${stdenv.cc.targetPrefix}cc"; + + RUSTFLAGS = "-C target-feature=-crt-static"; + + HOST_CC = "${stdenv.cc.nativePrefix}cc"; + TARGET_CC = "${stdenv.cc.targetPrefix}cc"; + + BINDGEN_EXTRA_CLANG_ARGS = + if stdenv.isDarwin + then "-isystem ${apple-sdk.sdkroot}/usr/include" + else "-isystem ${stdenv.cc.libc.dev}/include"; +} diff --git a/rust-toolchain.toml b/rust-toolchain.toml index daf0642..9004dce 100644 --- a/rust-toolchain.toml +++ b/rust-toolchain.toml @@ -1,8 +1,14 @@ [toolchain] channel = "stable" targets = [ - "aarch64-apple-darwin", "x86_64-apple-darwin", + "aarch64-apple-darwin", + + "x86_64-unknown-linux-gnu", + "aarch64-unknown-linux-gnu", + "x86_64-unknown-linux-musl", - "x86_64-pc-windows-gnullvm", + "aarch64-unknown-linux-musl", + + "x86_64-pc-windows-gnu", ]