From efd5df958efe5b91d8efd29282baad394cd6021e Mon Sep 17 00:00:00 2001 From: Sebastian Hugentobler Date: Wed, 3 Jul 2024 14:20:11 +0200 Subject: [PATCH] static compilation wiith musl and nix for linux systems --- .gitignore | 1 + flake.lock | 34 ++++++++++++- flake.nix | 114 ++++++++++++++++++++++++++++++++++++++++++-- rust-toolchain.toml | 4 ++ 4 files changed, 149 insertions(+), 4 deletions(-) create mode 100644 rust-toolchain.toml diff --git a/.gitignore b/.gitignore index 88939a1..390ec9e 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,4 @@ node_modules /target nzz/ +result diff --git a/flake.lock b/flake.lock index 6998b46..048f570 100644 --- a/flake.lock +++ b/flake.lock @@ -37,6 +37,25 @@ "type": "github" } }, + "naersk": { + "inputs": { + "nixpkgs": "nixpkgs_2" + }, + "locked": { + "lastModified": 1718727675, + "narHash": "sha256-uFsCwWYI2pUpt0awahSBorDUrUfBhaAiyz+BPTS2MHk=", + "owner": "nix-community", + "repo": "naersk", + "rev": "941ce6dc38762a7cfb90b5add223d584feed299b", + "type": "github" + }, + "original": { + "owner": "nix-community", + "ref": "master", + "repo": "naersk", + "type": "github" + } + }, "nixpkgs": { "locked": { "lastModified": 1719690277, @@ -54,6 +73,18 @@ } }, "nixpkgs_2": { + "locked": { + "lastModified": 0, + "narHash": "sha256-H3+EC5cYuq+gQW8y0lSrrDZfH71LB4DAf+TDFyvwCNA=", + "path": "/nix/store/j4jzjbr302cw5bl0n3pch5j9bh5qwmaj-source", + "type": "path" + }, + "original": { + "id": "nixpkgs", + "type": "indirect" + } + }, + "nixpkgs_3": { "locked": { "lastModified": 1719075281, "narHash": "sha256-CyyxvOwFf12I91PBWz43iGT1kjsf5oi6ax7CrvaMyAo=", @@ -73,7 +104,8 @@ "inputs": { "fenix": "fenix", "flake-utils": "flake-utils", - "nixpkgs": "nixpkgs_2" + "naersk": "naersk", + "nixpkgs": "nixpkgs_3" } }, "rust-analyzer-src": { diff --git a/flake.nix b/flake.nix index 72fa8d3..993e6ca 100644 --- a/flake.nix +++ b/flake.nix @@ -1,8 +1,10 @@ +# thanks to https://code.betamike.com/micropelago/domani for the flake, I still do not completely understand it :) { description = "little-hesinde project"; inputs = { nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable"; flake-utils.url = "github:numtide/flake-utils"; + naersk.url = "github:nix-community/naersk/master"; fenix.url = "github:nix-community/fenix"; }; @@ -10,14 +12,69 @@ { nixpkgs, flake-utils, + naersk, fenix, ... }: + let + mkToolchain = + fenixPkgs: + fenixPkgs.fromToolchainFile { + file = ./rust-toolchain.toml; + sha256 = "sha256-Ngiz76YP4HTY75GGdH2P+APE/DEIx2R/Dn+BwwOyzZU="; + }; + + buildTargets = { + "x86_64-linux" = { + crossSystemConfig = "x86_64-unknown-linux-musl"; + rustTarget = "x86_64-unknown-linux-musl"; + }; + + "i686-linux" = { + crossSystemConfig = "i686-unknown-linux-musl"; + rustTarget = "i686-unknown-linux-musl"; + }; + + "aarch64-linux" = { + crossSystemConfig = "aarch64-unknown-linux-musl"; + rustTarget = "aarch64-unknown-linux-musl"; + }; + }; + + eachSystem = + supportedSystems: callback: + builtins.foldl' (overall: system: overall // { ${system} = callback system; }) { } supportedSystems; + + eachCrossSystem = + supportedSystems: callback: + eachSystem supportedSystems ( + buildSystem: + builtins.foldl' ( + inner: targetSystem: inner // { "cross-${targetSystem}" = callback buildSystem targetSystem; } + ) { default = callback buildSystem buildSystem; } supportedSystems + ); + + mkPkgs = + buildSystem: targetSystem: + import nixpkgs ( + { + system = buildSystem; + } + // ( + if targetSystem == null then + { } + else + { crossSystem.config = buildTargets.${targetSystem}.crossSystemConfig; } + ) + ); + + in flake-utils.lib.eachDefaultSystem ( system: let pkgs = import nixpkgs { inherit system; }; - rust = fenix.packages.${system}.stable; + toolchain = mkToolchain fenix.packages.${system}; + in { devShells.default = @@ -25,11 +82,62 @@ mkShell { buildInputs = [ geckodriver - rust.toolchain + toolchain cargo-deny rust-analyzer ]; }; } - ); + ) + // { + packages = eachCrossSystem (builtins.attrNames buildTargets) ( + buildSystem: targetSystem: + let + pkgs = mkPkgs buildSystem null; + pkgsCross = mkPkgs buildSystem targetSystem; + rustTarget = buildTargets.${targetSystem}.rustTarget; + + fenixPkgs = fenix.packages.${buildSystem}; + toolchain = mkToolchain fenixPkgs; + + buildPackageAttrs = + if builtins.hasAttr "makeBuildPackageAttrs" buildTargets.${targetSystem} then + buildTargets.${targetSystem}.makeBuildPackageAttrs pkgsCross + else + { }; + + naersk-lib = pkgs.callPackage naersk { + cargo = toolchain; + rustc = toolchain; + }; + + in + naersk-lib.buildPackage ( + buildPackageAttrs + // rec { + src = ./.; + strictDeps = true; + doCheck = false; + + OPENSSL_STATIC = "1"; + OPENSSL_LIB_DIR = "${pkgsCross.pkgsStatic.openssl.out}/lib"; + OPENSSL_INCLUDE_DIR = "${pkgsCross.pkgsStatic.openssl.dev}/include"; + + # Required because ring crate is special. This also seems to have + # fixed some issues with the x86_64-windows cross-compile :shrug: + TARGET_CC = "${pkgsCross.stdenv.cc}/bin/${pkgsCross.stdenv.cc.targetPrefix}cc"; + + CARGO_BUILD_TARGET = rustTarget; + CARGO_BUILD_RUSTFLAGS = [ + "-C" + "target-feature=+crt-static" + + # https://github.com/rust-lang/cargo/issues/4133 + "-C" + "linker=${TARGET_CC}" + ]; + } + ) + ); + }; } diff --git a/rust-toolchain.toml b/rust-toolchain.toml new file mode 100644 index 0000000..aefd856 --- /dev/null +++ b/rust-toolchain.toml @@ -0,0 +1,4 @@ +[toolchain] +channel = "stable" +targets = [ "i686-unknown-linux-musl", "aarch64-unknown-linux-musl", "x86_64-unknown-linux-musl" ] +profile = "minimal"