From 421921ef1c492c9a21a999cf7b637d8237aad97e Mon Sep 17 00:00:00 2001 From: Zhaofeng Li Date: Fri, 19 May 2023 02:48:52 -0600 Subject: [PATCH] Add package --- .github/workflows/build.yaml | 16 +++---- crane.nix | 89 ++++++++++++++++++++++++++++++++++++ flake.nix | 50 ++++++-------------- 3 files changed, 110 insertions(+), 45 deletions(-) create mode 100644 crane.nix diff --git a/.github/workflows/build.yaml b/.github/workflows/build.yaml index a3f48d2..064dc37 100644 --- a/.github/workflows/build.yaml +++ b/.github/workflows/build.yaml @@ -13,15 +13,15 @@ jobs: - uses: DeterminateSystems/nix-installer-action-cache@main - - name: "Build something" - run: "nix develop -c cargo build --release" + - name: Build package + run: "nix build .# -L" - name: Upload a Build Artifact uses: actions/upload-artifact@v3.1.2 with: # Artifact name name: nix-actions-cache-macOS - path: target/release/nix-actions-cache + path: result/bin/nix-actions-cache retention-days: 1 check-artifacts-macOS: @@ -40,7 +40,7 @@ jobs: cache-binary: ./cache-binary/nix-actions-cache - name: "Build something" - run: "nix develop -c date" + run: "nix build .# -L" build-artifacts-Linux: runs-on: ubuntu-22.04 @@ -49,15 +49,15 @@ jobs: - uses: DeterminateSystems/nix-installer-action-cache@main - - name: "Build something" - run: "nix develop -c cargo build --release --target x86_64-unknown-linux-musl" + - name: Build package + run: "nix build .# -L" - name: Upload a Build Artifact uses: actions/upload-artifact@v3.1.2 with: # Artifact name name: nix-actions-cache-Linux - path: target/x86_64-unknown-linux-musl/release/nix-actions-cache + path: result/bin/nix-actions-cache retention-days: 1 check-artifacts-Linux: @@ -76,4 +76,4 @@ jobs: cache-binary: ./cache-binary/nix-actions-cache - name: "Build something" - run: "nix develop -c date" + run: "nix build .# -L" diff --git a/crane.nix b/crane.nix new file mode 100644 index 0000000..dff3c91 --- /dev/null +++ b/crane.nix @@ -0,0 +1,89 @@ +{ stdenv +, pkgs +, lib +, crane +, rustNightly +, rust +, nix-gitignore +, supportedSystems +}: + +let + inherit (stdenv.hostPlatform) system; + + # For easy cross-compilation in devShells + # We are just composing the pkgsCross.*.stdenv.cc together + crossPlatforms = let + makeCrossPlatform = crossSystem: let + pkgsCross = + if crossSystem == system then pkgs + else import pkgs.path { + inherit system crossSystem; + overlays = []; + }; + + rustTargetSpec = rust.toRustTargetSpec pkgsCross.pkgsStatic.stdenv.hostPlatform; + rustTargetSpecUnderscored = builtins.replaceStrings [ "-" ] [ "_" ] rustTargetSpec; + + cargoLinkerEnv = lib.strings.toUpper "CARGO_TARGET_${rustTargetSpecUnderscored}_LINKER"; + cargoCcEnv = "CC_${rustTargetSpecUnderscored}"; # for ring + + cc = "${pkgsCross.stdenv.cc}/bin/${pkgsCross.stdenv.cc.targetPrefix}cc"; + in { + name = crossSystem; + value = { + inherit rustTargetSpec cc; + pkgs = pkgsCross; + env = { + "${cargoLinkerEnv}" = cc; + "${cargoCcEnv}" = cc; + }; + }; + }; + systems = lib.filter (s: s == system || lib.hasInfix "linux" s) supportedSystems; + in builtins.listToAttrs (map makeCrossPlatform systems); + + cargoTargets = lib.mapAttrsToList (_: p: p.rustTargetSpec) crossPlatforms; + cargoCrossEnvs = lib.foldl (acc: p: acc // p.env) {} (builtins.attrValues crossPlatforms); + + buildFor = system: let + crossPlatform = crossPlatforms.${system}; + inherit (crossPlatform) pkgs; + craneLib = (crane.mkLib pkgs).overrideToolchain rustNightly; + + pname = "nix-actions-cache"; + + src = nix-gitignore.gitignoreSource [] ./.; + + buildInputs = with pkgs; [] + ++ lib.optionals pkgs.stdenv.isDarwin [ + darwin.apple_sdk.frameworks.Security + ]; + + # The Rust toolchain from rust-overlay has a dynamic libiconv in depsTargetTargetPropagated + # Our static libiconv needs to take precedence + nativeBuildInputs = with pkgs; [] + ++ lib.optionals pkgs.stdenv.isDarwin [ + (libiconv.override { enableStatic = true; enableShared = false; }) + ]; + + cargoExtraArgs = "--target ${crossPlatform.rustTargetSpec}"; + + cargoArtifacts = craneLib.buildDepsOnly ({ + inherit pname src buildInputs nativeBuildInputs cargoExtraArgs; + + doCheck = false; + } // crossPlatform.env); + crate = craneLib.buildPackage ({ + inherit pname src buildInputs nativeBuildInputs cargoExtraArgs; + inherit cargoArtifacts; + + # The resulting executable must be standalone + allowedRequisites = []; + } // crossPlatform.env); + in crate; +in { + inherit crossPlatforms cargoTargets cargoCrossEnvs; + + nix-actions-cache = buildFor system; +} diff --git a/flake.nix b/flake.nix index 3365cdd..6f1413b 100644 --- a/flake.nix +++ b/flake.nix @@ -27,10 +27,6 @@ outputs = { self, nixpkgs, flake-utils, rust-overlay, crane, ... }: let supportedSystems = flake-utils.lib.defaultSystems; nightlyVersion = "2023-05-01"; - - makeCranePkgs = pkgs: let - craneLib = crane.mkLib pkgs; - in pkgs.callPackage ./crane.nix { inherit craneLib; }; in flake-utils.lib.eachSystem supportedSystems (system: let pkgs = import nixpkgs { inherit system; @@ -41,40 +37,21 @@ inherit (pkgs) lib; - crossPlatforms = let - makeCrossPlatform = crossSystem: let - pkgsCross = if crossSystem == system then pkgs else import nixpkgs { - inherit system crossSystem; - overlays = []; - }; - rustTargetSpec = pkgs.rust.toRustTargetSpec pkgsCross.pkgsStatic.stdenv.hostPlatform; - rustTargetSpecUnderscored = builtins.replaceStrings [ "-" ] [ "_" ] rustTargetSpec; - in { - inherit rustTargetSpec; - cc = "${pkgsCross.stdenv.cc}/bin/${pkgsCross.stdenv.cc.targetPrefix}cc"; - cargoLinkerEnv = lib.strings.toUpper "CARGO_TARGET_${rustTargetSpecUnderscored}_LINKER"; - cargoCcEnv = "CC_${rustTargetSpecUnderscored}"; # for ring - }; - systems = lib.filter (lib.hasInfix "linux") supportedSystems; - in map makeCrossPlatform systems; - rustNightly = pkgs.rust-bin.nightly.${nightlyVersion}.default.override { extensions = [ "rust-src" "rust-analyzer-preview" ]; - targets = map (p: p.rustTargetSpec) crossPlatforms; + targets = cranePkgs.cargoTargets; }; - cargoCrossEnvs = lib.listToAttrs (lib.flatten (map (p: [ - { - name = p.cargoCcEnv; - value = p.cc; - } - { - name = p.cargoLinkerEnv; - value = p.cc; - } - ]) crossPlatforms)); + cranePkgs = pkgs.callPackage ./crane.nix { + inherit crane supportedSystems rustNightly; + }; in { + packages = rec { + inherit (cranePkgs) nix-actions-cache; + default = nix-actions-cache; + }; devShells = { + inputsFrom = [ cranePkgs.nix-actions-cache ]; default = pkgs.mkShell ({ packages = with pkgs; [ bashInteractive @@ -83,11 +60,10 @@ cargo-bloat cargo-edit cargo-udeps - ] - ++ lib.optional stdenv.hostPlatform.isDarwin darwin.apple_sdk.frameworks.Security - ++ lib.optional stdenv.hostPlatform.isDarwin (libiconv.override { enableStatic = true; enableShared = false; }) - ; - } // cargoCrossEnvs); + + age + ]; + } // cranePkgs.cargoCrossEnvs); keygen = pkgs.mkShellNoCC { packages = with pkgs; [ age