From 34240efd2be8a265b32f1d0c6c939279c1a12aa3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFs=20Postula?= Date: Fri, 1 Nov 2024 12:21:12 +0100 Subject: [PATCH] nix building --- .gitignore | 2 + Cargo.lock | 2 +- Cargo.toml | 2 +- default.nix | 7 + flake.lock | 133 +++++++++++++++ flake.nix | 251 ++++++++++++++++++++++++++++ rust-toolchain.toml | 7 + src/commands/check_workspace/mod.rs | 4 +- 8 files changed, 404 insertions(+), 4 deletions(-) create mode 100644 default.nix create mode 100644 flake.lock create mode 100644 flake.nix diff --git a/.gitignore b/.gitignore index c403c34..12ddb6e 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,4 @@ /target .idea/ +result/ +result diff --git a/Cargo.lock b/Cargo.lock index 98a4575..1ee60dc 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -294,7 +294,7 @@ dependencies = [ [[package]] name = "cargo-fslabscli" -version = "2.3.1" +version = "2.3.2" dependencies = [ "anyhow", "assert_fs", diff --git a/Cargo.toml b/Cargo.toml index 90aed7f..edc6f15 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "cargo-fslabscli" -version = "2.3.1" +version = "2.3.2" edition = "2021" authors = ["FSLABS DevOps Gods"] repository = "https://github.com/ForesightMiningSoftwareCorporation/fslabsci" diff --git a/default.nix b/default.nix new file mode 100644 index 0000000..39bacff --- /dev/null +++ b/default.nix @@ -0,0 +1,7 @@ +(import ( + fetchTarball { + url = "https://github.com/edolstra/flake-compat/archive/99f1c2157fba4bfe6211a321fd0ee43199025dbf.tar.gz"; + sha256 = "0x2jn3vrawwv9xp15674wjz9pixwjyj3j771izayl962zziivbx2"; } +) { + src = ./.; +}).defaultNix diff --git a/flake.lock b/flake.lock new file mode 100644 index 0000000..ca71851 --- /dev/null +++ b/flake.lock @@ -0,0 +1,133 @@ +{ + "nodes": { + "fenix": { + "inputs": { + "nixpkgs": [ + "nixpkgs" + ], + "rust-analyzer-src": "rust-analyzer-src" + }, + "locked": { + "lastModified": 1730442928, + "narHash": "sha256-U1DWb5c3EfkA7pqx5V1H4AWRA+EaE6UJ0lIRvK1RxgM=", + "owner": "nix-community", + "repo": "fenix", + "rev": "87b4d20f896c99018dde4702a9c6157b516f2a76", + "type": "github" + }, + "original": { + "owner": "nix-community", + "repo": "fenix", + "type": "github" + } + }, + "flake-utils": { + "inputs": { + "systems": "systems" + }, + "locked": { + "lastModified": 1726560853, + "narHash": "sha256-X6rJYSESBVr3hBoH0WbKE5KvhPU5bloyZ2L4K60/fPQ=", + "owner": "numtide", + "repo": "flake-utils", + "rev": "c1dfcf08411b08f6b8615f7d8971a2bfa81d5e8a", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "flake-utils", + "type": "github" + } + }, + "naersk": { + "inputs": { + "nixpkgs": "nixpkgs" + }, + "locked": { + "lastModified": 1721727458, + "narHash": "sha256-r/xppY958gmZ4oTfLiHN0ZGuQ+RSTijDblVgVLFi1mw=", + "owner": "nix-community", + "repo": "naersk", + "rev": "3fb418eaf352498f6b6c30592e3beb63df42ef11", + "type": "github" + }, + "original": { + "owner": "nix-community", + "ref": "master", + "repo": "naersk", + "type": "github" + } + }, + "nixpkgs": { + "locked": { + "lastModified": 1729665710, + "narHash": "sha256-AlcmCXJZPIlO5dmFzV3V2XF6x/OpNWUV8Y/FMPGd8Z4=", + "path": "/nix/store/lsy6c2f9alj2gkjj36h754kk63x6701l-source", + "rev": "2768c7d042a37de65bb1b5b3268fc987e534c49d", + "type": "path" + }, + "original": { + "id": "nixpkgs", + "type": "indirect" + } + }, + "nixpkgs_2": { + "locked": { + "lastModified": 1730200266, + "narHash": "sha256-l253w0XMT8nWHGXuXqyiIC/bMvh1VRszGXgdpQlfhvU=", + "owner": "nixos", + "repo": "nixpkgs", + "rev": "807e9154dcb16384b1b765ebe9cd2bba2ac287fd", + "type": "github" + }, + "original": { + "owner": "nixos", + "ref": "nixos-unstable", + "repo": "nixpkgs", + "type": "github" + } + }, + "root": { + "inputs": { + "fenix": "fenix", + "flake-utils": "flake-utils", + "naersk": "naersk", + "nixpkgs": "nixpkgs_2" + } + }, + "rust-analyzer-src": { + "flake": false, + "locked": { + "lastModified": 1730386175, + "narHash": "sha256-0Uq+/B8eu7pw8B8pxuGdFYKjcVLwNMcHfDxU9sXh7rg=", + "owner": "rust-lang", + "repo": "rust-analyzer", + "rev": "0ba893e1a00d92557ac91efb771d72eee36ca687", + "type": "github" + }, + "original": { + "owner": "rust-lang", + "ref": "nightly", + "repo": "rust-analyzer", + "type": "github" + } + }, + "systems": { + "locked": { + "lastModified": 1681028828, + "narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=", + "owner": "nix-systems", + "repo": "default", + "rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e", + "type": "github" + }, + "original": { + "owner": "nix-systems", + "repo": "default", + "type": "github" + } + } + }, + "root": "root", + "version": 7 +} diff --git a/flake.nix b/flake.nix new file mode 100644 index 0000000..581bc72 --- /dev/null +++ b/flake.nix @@ -0,0 +1,251 @@ +{ + inputs = { + fenix.url = "github:nix-community/fenix"; + fenix.inputs.nixpkgs.follows = "nixpkgs"; + flake-utils.url = "github:numtide/flake-utils"; + naersk.url = "github:nix-community/naersk/master"; + nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable"; + }; + + outputs = + { + self, + nixpkgs, + flake-utils, + naersk, + fenix, + }: + let + supportedSystems = flake-utils.lib.eachDefaultSystem [ + "aarch64-linux" + "x86_64-linux" + ]; + newBuildTarget = + { + nixPkgsSystem, + rustTarget ? nixPkgsSystem, + nativeBuildInputs ? pkgsCross: [ ], + rustFlags ? pkgsCross: [ ], + }: + { + inherit + nixPkgsSystem + rustTarget + nativeBuildInputs + rustFlags + ; + }; + + buildTargets = { + "x86_64-linux" = newBuildTarget { + nixPkgsSystem = "x86_64-unknown-linux-musl"; + }; + + "aarch64-linux" = newBuildTarget { + nixPkgsSystem = "aarch64-unknown-linux-musl"; + }; + }; + + eachSystem = + supportedSystems: callback: + builtins.foldl' (overall: system: overall // { ${system} = callback system; }) { } supportedSystems; + + # eachCrossSystem [system] ({buildSystem, targetSystem, isDefault }: ...) + # + # Returns an attrset with a key "$buildSystem.cross-$targetSystem" for + # every combination of the elements of the array of system strings. The + # value of the attrs will be the result of calling the callback with each + # combination. + # + # There will also be keys "$system.default", which are aliases of + # "$system.cross-$system" for every system. + # + eachCrossSystem = + supportedSystems: callback: + eachSystem supportedSystems ( + buildSystem: + let + pkgs = mkPkgs buildSystem null; + crosses = builtins.foldl' ( + inner: targetSystem: + inner + // { + "cross-${targetSystem}" = callback { + inherit buildSystem targetSystem; + isDefault = false; + }; + } + ) { } supportedSystems; + in + crosses + // (rec { + default = callback { + inherit buildSystem; + targetSystem = buildSystem; + isDefault = true; + }; + release = + let + bins = pkgs.symlinkJoin { + name = "${default.name}-all-bins"; + paths = builtins.attrValues crosses; + }; + in + pkgs.runCommand "${default.name}-release" { } '' + cp -rL "${bins}" "$out" + chmod +w "$out"/bin + (cd "$out"/bin && sha256sum * > sha256.txt) + ''; + }) + ); + + mkPkgs = + buildSystem: targetSystem: + import nixpkgs ( + { + system = buildSystem; + } + // ( + if targetSystem == null then + { } + else + { + # The nixpkgs cache doesn't have any packages where cross-compiling has + # been enabled, even if the target platform is actually the same as the + # build platform (and therefore it's not really cross-compiling). So we + # only set up the cross-compiling config if the target platform is + # different. + crossSystem.config = buildTargets.${targetSystem}.nixPkgsSystem; + } + ) + ); + + mkToolchain = + buildSystem: targetSystem: + let + buildTarget = buildTargets.${targetSystem}; + rustTarget = buildTarget.rustTarget; + fenixPkgs = fenix.packages.${buildSystem}; + + # TODO I'd prefer to use the toolchain file + # https://github.com/nix-community/fenix/issues/123 + fenixToolchain = + fenixTarget: + (builtins.getAttr "toolchainOf" fenixTarget) { + channel = "1.80.0"; + sha256 = "sha256-6eN/GKzjVSjEhGO9FhWObkRFaE1Jf+uqMSdQnb8lcB4="; + }; + in + fenixPkgs.combine [ + (fenixToolchain fenixPkgs).rustc + (fenixToolchain fenixPkgs).rustfmt + (fenixToolchain fenixPkgs).cargo + (fenixToolchain fenixPkgs).clippy + (fenixToolchain (fenixPkgs.targets).${rustTarget}).rust-std + ]; + + buildEnv = + buildSystem: targetSystem: + let + pkgs = mkPkgs buildSystem null; + pkgsCross = mkPkgs buildSystem targetSystem; + buildTarget = buildTargets.${targetSystem}; + in + rec { + nativeBuildInputs = (buildTarget.nativeBuildInputs pkgsCross) ++ [ + (mkToolchain buildSystem targetSystem) + + # Required for shell because of rust dependency build scripts which + # must run on the build system. + pkgs.stdenv.cc + ]; + + 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 = buildTarget.rustTarget; + CARGO_BUILD_RUSTFLAGS = [ + "-C" + "target-feature=+crt-static" + + # -latomic is required to build openssl-sys for armv6l-linux, but + # it doesn't seem to hurt any other builds. + "-C" + "link-args=-static -latomic" + + # https://github.com/rust-lang/cargo/issues/4133 + "-C" + "linker=${TARGET_CC}" + ] ++ (buildTarget.rustFlags pkgsCross); + }; + + in + { + + packages = eachCrossSystem (builtins.attrNames buildTargets) ( + { + buildSystem, + targetSystem, + isDefault, + }: + let + pkgs = mkPkgs buildSystem null; + toolchain = mkToolchain buildSystem targetSystem; + naersk-lib = pkgs.callPackage naersk { + cargo = toolchain; + rustc = toolchain; + }; + in + naersk-lib.buildPackage ( + rec { + src = ./.; + strictDeps = true; + doCheck = false; + release = true; + postInstall = + if isDefault then + "" + else + '' + cd "$out"/bin + for f in "$(ls)"; do + if ext="$(echo "$f" | grep -oP '\.[a-z]+$')"; then + base="$(echo "$f" | cut -d. -f1)" + mv "$f" "$base-${targetSystem}$ext" + else + mv "$f" "$f-${targetSystem}" + fi + done + ''; + } + // (buildEnv buildSystem targetSystem) + ) + ); + + devShells = eachCrossSystem (builtins.attrNames buildTargets) ( + { + buildSystem, + targetSystem, + isDefault, + }: + let + pkgs = mkPkgs buildSystem null; + toolchain = mkToolchain buildSystem targetSystem; + in + pkgs.mkShell ( + { + buildInputs = [ ]; + shellHook = '' + export CARGO_HOME=$(pwd)/.cargo + ''; + } + // (buildEnv buildSystem targetSystem) + ) + ); + }; +} diff --git a/rust-toolchain.toml b/rust-toolchain.toml index a5b1f06..07f70da 100644 --- a/rust-toolchain.toml +++ b/rust-toolchain.toml @@ -1,3 +1,10 @@ [toolchain] profile = "default" channel = "1.80" +targets = [ + "x86_64-unknown-linux-gnu", + "x86_64-unknown-linux-musl", + "aarch64-unknown-linux-musl", + "x86_64-pc-windows-gnu" +] + diff --git a/src/commands/check_workspace/mod.rs b/src/commands/check_workspace/mod.rs index ca1a0c3..fa4cfab 100644 --- a/src/commands/check_workspace/mod.rs +++ b/src/commands/check_workspace/mod.rs @@ -135,9 +135,9 @@ pub struct Result { pub path: PathBuf, pub publish_detail: PackageMetadataFslabsCiPublish, pub publish: bool, - #[serde(skip_serializing)] + // #[serde(skip_serializing)] pub dependencies: Vec, - #[serde(skip_serializing)] + // #[serde(skip_serializing)] pub dependant: Vec, pub changed: bool, pub dependencies_changed: bool,