diff --git a/crane.nix b/crane.nix index cc6c625..d8c5cec 100644 --- a/crane.nix +++ b/crane.nix @@ -18,90 +18,98 @@ let }).overrideAttrs (old: { # Remove the propagated libiconv since we want to add our static version depsTargetTargetPropagated = lib.filter (d: d.pname != "libiconv") - (lib.flatten (old.depsTargetTargetPropagated or [])); + (lib.flatten (old.depsTargetTargetPropagated or [ ])); }); # 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 = []; + 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 + + ccbin = "${pkgsCross.stdenv.cc}/bin/${pkgsCross.stdenv.cc.targetPrefix}cc"; + in + { + name = crossSystem; + value = { + inherit rustTargetSpec; + cc = pkgsCross.stdenv.cc; + pkgs = pkgsCross; + buildInputs = makeBuildInputs pkgsCross; + env = { + "${cargoLinkerEnv}" = ccbin; + "${cargoCcEnv}" = ccbin; + }; + }; }; - - rustTargetSpec = rust.toRustTargetSpec pkgsCross.pkgsStatic.stdenv.hostPlatform; - rustTargetSpecUnderscored = builtins.replaceStrings [ "-" ] [ "_" ] rustTargetSpec; - - cargoLinkerEnv = lib.strings.toUpper "CARGO_TARGET_${rustTargetSpecUnderscored}_LINKER"; - cargoCcEnv = "CC_${rustTargetSpecUnderscored}"; # for ring - - ccbin = "${pkgsCross.stdenv.cc}/bin/${pkgsCross.stdenv.cc.targetPrefix}cc"; - in { - name = crossSystem; - value = { - inherit rustTargetSpec; - cc = pkgsCross.stdenv.cc; - pkgs = pkgsCross; - buildInputs = makeBuildInputs pkgsCross; - env = { - "${cargoLinkerEnv}" = ccbin; - "${cargoCcEnv}" = ccbin; - }; - }; - }; - systems = lib.filter (s: s == system || lib.hasInfix "linux" s) supportedSystems - # Cross from aarch64-darwin -> x86_64-darwin doesn't work yet - # Hopefully the situation will improve with the SDK bumps - ++ lib.optional (system == "x86_64-darwin") "aarch64-darwin"; - in builtins.listToAttrs (map makeCrossPlatform systems); + systems = lib.filter (s: s == system || lib.hasInfix "linux" s) supportedSystems + # Cross from aarch64-darwin -> x86_64-darwin doesn't work yet + # Hopefully the situation will improve with the SDK bumps + ++ lib.optional (system == "x86_64-darwin") "aarch64-darwin"; + in + builtins.listToAttrs (map makeCrossPlatform systems); cargoTargets = lib.mapAttrsToList (_: p: p.rustTargetSpec) crossPlatforms; - cargoCrossEnvs = lib.foldl (acc: p: acc // p.env) {} (builtins.attrValues crossPlatforms); + cargoCrossEnvs = lib.foldl (acc: p: acc // p.env) { } (builtins.attrValues crossPlatforms); - makeBuildInputs = pkgs: with pkgs; [] + makeBuildInputs = pkgs: with pkgs; [ ] ++ lib.optionals pkgs.stdenv.isDarwin [ - darwin.apple_sdk.frameworks.Security - (libiconv.override { enableStatic = true; enableShared = false; }) - ]; + darwin.apple_sdk.frameworks.Security + (libiconv.override { enableStatic = true; enableShared = false; }) + ]; - buildFor = system: let - crossPlatform = crossPlatforms.${system}; - inherit (crossPlatform) pkgs; - craneLib = (crane.mkLib pkgs).overrideToolchain rustNightly; - crateName = craneLib.crateNameFromCargoToml { - cargoToml = ./nix-actions-cache/Cargo.toml; - }; - - src = nix-gitignore.gitignoreSource [] ./.; - - commonArgs = { - inherit (crateName) pname version; - inherit src; - - buildInputs = makeBuildInputs pkgs; - - cargoExtraArgs = "--target ${crossPlatform.rustTargetSpec}"; - - cargoVendorDir = craneLib.vendorMultipleCargoDeps { - inherit (craneLib.findCargoFiles src) cargoConfigs; - cargoLockList = [ - ./Cargo.lock - "${rustNightly.passthru.availableComponents.rust-src}/lib/rustlib/src/rust/Cargo.lock" - ]; + buildFor = system: + let + crossPlatform = crossPlatforms.${system}; + inherit (crossPlatform) pkgs; + craneLib = (crane.mkLib pkgs).overrideToolchain rustNightly; + crateName = craneLib.crateNameFromCargoToml { + cargoToml = ./nix-actions-cache/Cargo.toml; }; - } // crossPlatform.env; - crate = craneLib.buildPackage (commonArgs // { - cargoArtifacts = craneLib.buildDepsOnly commonArgs; + src = nix-gitignore.gitignoreSource [ ] ./.; - # The resulting executable must be standalone - allowedRequisites = []; - }); - in crate; -in { + commonArgs = { + inherit (crateName) pname version; + inherit src; + + buildInputs = makeBuildInputs pkgs; + + cargoExtraArgs = "--target ${crossPlatform.rustTargetSpec}"; + + cargoVendorDir = craneLib.vendorMultipleCargoDeps { + inherit (craneLib.findCargoFiles src) cargoConfigs; + cargoLockList = [ + ./Cargo.lock + "${rustNightly.passthru.availableComponents.rust-src}/lib/rustlib/src/rust/Cargo.lock" + ]; + }; + } // crossPlatform.env; + + crate = craneLib.buildPackage (commonArgs // { + cargoArtifacts = craneLib.buildDepsOnly commonArgs; + + # The resulting executable must be standalone + allowedRequisites = [ ]; + }); + in + crate; +in +{ inherit crossPlatforms cargoTargets cargoCrossEnvs rustNightly; nix-actions-cache = buildFor system; diff --git a/flake.lock b/flake.lock index 9ce30c3..d28aa5f 100644 --- a/flake.lock +++ b/flake.lock @@ -5,20 +5,18 @@ "flake-compat": [ "flake-compat" ], - "flake-utils": [ - "flake-utils" - ], + "flake-utils": "flake-utils", "nixpkgs": [ "nixpkgs" ], "rust-overlay": "rust-overlay" }, "locked": { - "lastModified": 1683505101, - "narHash": "sha256-VBU64Jfu2V4sUR5+tuQS9erBRAe/QEYUxdVMcJGMZZs=", + "lastModified": 1687310026, + "narHash": "sha256-20RHFbrnC+hsG4Hyeg/58LvQAK7JWfFItTPFAFamu8E=", "owner": "ipetkov", "repo": "crane", - "rev": "7b5bd9e5acb2bb0cfba2d65f34d8568a894cdb6c", + "rev": "116b32c30b5ff28e49f4fcbeeb1bbe3544593204", "type": "github" }, "original": { @@ -47,6 +45,24 @@ "inputs": { "systems": "systems" }, + "locked": { + "lastModified": 1685518550, + "narHash": "sha256-o2d0KcvaXzTrPRIo0kOLV0/QXHhDQ5DTi+OxcjO8xqY=", + "owner": "numtide", + "repo": "flake-utils", + "rev": "a1720a10a6cfe8234c0e93907ffe81be440f4cef", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "flake-utils", + "type": "github" + } + }, + "flake-utils_2": { + "inputs": { + "systems": "systems_2" + }, "locked": { "lastModified": 1681202837, "narHash": "sha256-H+Rh19JDwRtpVPAWp64F+rlEtxUWBAQW28eAi3SRSzg=", @@ -63,11 +79,11 @@ }, "nixpkgs": { "locked": { - "lastModified": 1684049129, - "narHash": "sha256-7WB9LpnPNAS8oI7hMoHeKLNhRX7k3CI9uWBRSfmOCCE=", + "lastModified": 1686960236, + "narHash": "sha256-AYCC9rXNLpUWzD9hm+askOfpliLEC9kwAo7ITJc4HIw=", "owner": "NixOS", "repo": "nixpkgs", - "rev": "0470f36b02ef01d4f43c641bbf07020bcab71bf1", + "rev": "04af42f3b31dba0ef742d254456dc4c14eedac86", "type": "github" }, "original": { @@ -81,7 +97,6 @@ "inputs": { "crane": "crane", "flake-compat": "flake-compat", - "flake-utils": "flake-utils", "nixpkgs": "nixpkgs", "rust-overlay": "rust-overlay_2" } @@ -98,11 +113,11 @@ ] }, "locked": { - "lastModified": 1683080331, - "narHash": "sha256-nGDvJ1DAxZIwdn6ww8IFwzoHb2rqBP4wv/65Wt5vflk=", + "lastModified": 1685759304, + "narHash": "sha256-I3YBH6MS3G5kGzNuc1G0f9uYfTcNY9NYoRc3QsykLk4=", "owner": "oxalica", "repo": "rust-overlay", - "rev": "d59c3fa0cba8336e115b376c2d9e91053aa59e56", + "rev": "c535b4f3327910c96dcf21851bbdd074d0760290", "type": "github" }, "original": { @@ -113,19 +128,17 @@ }, "rust-overlay_2": { "inputs": { - "flake-utils": [ - "flake-utils" - ], + "flake-utils": "flake-utils_2", "nixpkgs": [ "nixpkgs" ] }, "locked": { - "lastModified": 1683512408, - "narHash": "sha256-QMJGp/37En+d5YocJuSU89GL14bBYkIJQ6mqhRfqkkc=", + "lastModified": 1687400833, + "narHash": "sha256-rVENiSupjAE8o1+ZXNRIqewUzM2brm+aeme8MUrwl0U=", "owner": "oxalica", "repo": "rust-overlay", - "rev": "75b07756c3feb22cf230e75fb064c1b4c725b9bc", + "rev": "fc0a266e836c079a9131108f4334e5af219dbb93", "type": "github" }, "original": { @@ -148,6 +161,21 @@ "repo": "default", "type": "github" } + }, + "systems_2": { + "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", diff --git a/flake.nix b/flake.nix index c6261c8..d766efb 100644 --- a/flake.nix +++ b/flake.nix @@ -3,19 +3,16 @@ inputs = { nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable"; - flake-utils.url = "github:numtide/flake-utils"; rust-overlay = { url = "github:oxalica/rust-overlay"; inputs.nixpkgs.follows = "nixpkgs"; - inputs.flake-utils.follows = "flake-utils"; }; crane = { url = "github:ipetkov/crane"; inputs.nixpkgs.follows = "nixpkgs"; inputs.flake-compat.follows = "flake-compat"; - inputs.flake-utils.follows = "flake-utils"; }; flake-compat = { @@ -24,121 +21,124 @@ }; }; - outputs = { self, nixpkgs, flake-utils, rust-overlay, crane, ... }: let - supportedSystems = flake-utils.lib.defaultSystems; - in flake-utils.lib.eachSystem supportedSystems (system: let - pkgs = import nixpkgs { - inherit system; - overlays = [ - rust-overlay.overlay + outputs = { self, nixpkgs, ... }@inputs: + let + overlays = [ inputs.rust-overlay.overlays.default ]; + supportedSystems = [ + "aarch64-linux" + "x86_64-linux" + "aarch64-darwin" + "x86_64-darwin" ]; - }; + forEachSupportedSystem = f: nixpkgs.lib.genAttrs supportedSystems (system: f rec { + pkgs = import nixpkgs { inherit overlays system; }; + cranePkgs = pkgs.callPackage ./crane.nix { + inherit supportedSystems; + inherit (inputs) crane; + }; + inherit (pkgs) lib; + }); + in + { + packages = forEachSupportedSystem ({ pkgs, cranePkgs, ... }: rec { + inherit (cranePkgs) nix-actions-cache; + default = nix-actions-cache; + }); - inherit (pkgs) lib; + devShells = forEachSupportedSystem ({ pkgs, cranePkgs, lib }: { + default = pkgs.mkShell ({ + inputsFrom = [ cranePkgs.nix-actions-cache ]; + packages = with pkgs; [ + bashInteractive + cranePkgs.rustNightly - cranePkgs = pkgs.callPackage ./crane.nix { - inherit crane supportedSystems; - }; - in { - packages = rec { - inherit (cranePkgs) nix-actions-cache; - default = nix-actions-cache; - }; - devShells = { - default = pkgs.mkShell ({ - inputsFrom = [ cranePkgs.nix-actions-cache ]; - packages = with pkgs; [ - bashInteractive - cranePkgs.rustNightly + cargo-bloat + cargo-edit + cargo-udeps + cargo-watch - cargo-bloat - cargo-edit - cargo-udeps + age + ]; + shellHook = + let + crossSystems = lib.filter (s: s != pkgs.system) (builtins.attrNames cranePkgs.crossPlatforms); + in + '' + # Returns compiler environment variables for a platform + # + # getTargetFlags "suffixSalt" "nativeBuildInputs" "buildInputs" + getTargetFlags() { + # Here we only call the setup-hooks of nativeBuildInputs. + # + # What's off-limits for us: + # + # - findInputs + # - activatePackage + # - Other functions in stdenv setup that depend on the private accumulator variables + ( + suffixSalt="$1" + nativeBuildInputs="$2" + buildInputs="$3" - age - ]; + # Offsets for the nativeBuildInput (e.g., gcc) + hostOffset=-1 + targetOffset=0 - # We _can_ cross-compile to multiple systems with one shell :) - # - # Currently stdenv isn't set up to do that, but we can invoke - # the setup mechinary in a sub-shell then compose the results. - shellHook = let - crossSystems = lib.filter (s: s != pkgs.system) (builtins.attrNames cranePkgs.crossPlatforms); - in '' - # Returns compiler environment variables for a platform - # - # getTargetFlags "suffixSalt" "nativeBuildInputs" "buildInputs" - getTargetFlags() { - # Here we only call the setup-hooks of nativeBuildInputs. - # - # What's off-limits for us: - # - # - findInputs - # - activatePackage - # - Other functions in stdenv setup that depend on the private accumulator variables - ( - suffixSalt="$1" - nativeBuildInputs="$2" - buildInputs="$3" + # In stdenv, the hooks are first accumulated before being called. + # Here we call them immediately + addEnvHooks() { + local depHostOffset="$1" + # For simplicity, we only call the hook on buildInputs + for pkg in $buildInputs; do + depTargetOffset=1 + $2 $pkg + done + } - # Offsets for the nativeBuildInput (e.g., gcc) - hostOffset=-1 - targetOffset=0 + unset _PATH + unset NIX_CFLAGS_COMPILE + unset NIX_LDFLAGS - # In stdenv, the hooks are first accumulated before being called. - # Here we call them immediately - addEnvHooks() { - local depHostOffset="$1" - # For simplicity, we only call the hook on buildInputs - for pkg in $buildInputs; do - depTargetOffset=1 - $2 $pkg - done + # For simplicity, we only call the setup-hooks of nativeBuildInputs + for nbi in $nativeBuildInputs; do + addToSearchPath _PATH "$nbi/bin" + + if [ -e "$nbi/nix-support/setup-hook" ]; then + source "$nbi/nix-support/setup-hook" + fi + done + + echo "export NIX_CFLAGS_COMPILE_''${suffixSalt}='$NIX_CFLAGS_COMPILE'" + echo "export NIX_LDFLAGS_''${suffixSalt}='$NIX_LDFLAGS'" + echo "export PATH=$PATH''${_PATH+:$_PATH}" + ) } - unset _PATH + target_flags=$(mktemp) + ${lib.concatMapStrings (system: let + crossPlatform = cranePkgs.crossPlatforms.${system}; + in '' + getTargetFlags \ + "${crossPlatform.cc.suffixSalt}" \ + "${crossPlatform.cc} ${crossPlatform.cc.bintools}" \ + "${builtins.concatStringsSep " " (crossPlatform.buildInputs ++ crossPlatform.pkgs.stdenv.defaultBuildInputs)}" >$target_flags + . $target_flags + '') crossSystems} + rm $target_flags + + # Suffix flags for current system as well + export NIX_CFLAGS_COMPILE_${pkgs.stdenv.cc.suffixSalt}="$NIX_CFLAGS_COMPILE" + export NIX_LDFLAGS_${pkgs.stdenv.cc.suffixSalt}="$NIX_LDFLAGS" unset NIX_CFLAGS_COMPILE unset NIX_LDFLAGS + ''; + } // cranePkgs.cargoCrossEnvs); - # For simplicity, we only call the setup-hooks of nativeBuildInputs - for nbi in $nativeBuildInputs; do - addToSearchPath _PATH "$nbi/bin" - - if [ -e "$nbi/nix-support/setup-hook" ]; then - source "$nbi/nix-support/setup-hook" - fi - done - - echo "export NIX_CFLAGS_COMPILE_''${suffixSalt}='$NIX_CFLAGS_COMPILE'" - echo "export NIX_LDFLAGS_''${suffixSalt}='$NIX_LDFLAGS'" - echo "export PATH=$PATH''${_PATH+:$_PATH}" - ) - } - - target_flags=$(mktemp) - ${lib.concatMapStrings (system: let - crossPlatform = cranePkgs.crossPlatforms.${system}; - in '' - getTargetFlags \ - "${crossPlatform.cc.suffixSalt}" \ - "${crossPlatform.cc} ${crossPlatform.cc.bintools}" \ - "${builtins.concatStringsSep " " (crossPlatform.buildInputs ++ crossPlatform.pkgs.stdenv.defaultBuildInputs)}" >$target_flags - . $target_flags - '') crossSystems} - rm $target_flags - - # Suffix flags for current system as well - export NIX_CFLAGS_COMPILE_${pkgs.stdenv.cc.suffixSalt}="$NIX_CFLAGS_COMPILE" - export NIX_LDFLAGS_${pkgs.stdenv.cc.suffixSalt}="$NIX_LDFLAGS" - unset NIX_CFLAGS_COMPILE - unset NIX_LDFLAGS - ''; - } // cranePkgs.cargoCrossEnvs); - keygen = pkgs.mkShellNoCC { - packages = with pkgs; [ - age - ]; - }; + keygen = pkgs.mkShellNoCC { + packages = with pkgs; [ + age + ]; + }; + }); }; - }); } diff --git a/shell.nix b/shell.nix index bd803e2..a63c7ee 100644 --- a/shell.nix +++ b/shell.nix @@ -13,4 +13,5 @@ let shell = flake.shellNix.default // { reproduce = flake.defaultNix.outputs.reproduce.${builtins.currentSystem}; }; -in shell +in +shell