perf(nix): refactor mkStatic

this should speed up evaluation a bit by caching all the things
This commit is contained in:
seth 2024-08-06 23:39:45 -04:00 committed by Sefa Eyeoglu
parent 1b5c6d14d1
commit 55bc54722e
2 changed files with 22 additions and 18 deletions

View file

@ -71,17 +71,17 @@
packages' = self.packages.${system}; packages' = self.packages.${system};
mkStatic = pkgs.callPackage ./nix/static.nix { mkStatic = pkgs.callPackage ./nix/static.nix {
inherit (self.packages.${pkgs.system}) refraction; inherit (packages') refraction;
rust-overlay = rust-overlay.packages.${system}; rust-overlay = rust-overlay.packages.${system};
}; };
mkContainerFor = containerize =
refraction: refraction:
pkgs.dockerTools.buildLayeredImage { pkgs.dockerTools.buildLayeredImage {
name = "refraction"; name = "refraction";
tag = "latest-${refraction.stdenv.hostPlatform.qemuArch}"; tag = "latest-${refraction.passthru.architecture}";
config.Cmd = [ (lib.getExe refraction) ]; config.Cmd = [ (lib.getExe refraction) ];
inherit (refraction) architecture; inherit (refraction.passthru) architecture;
}; };
in in
{ {
@ -89,8 +89,8 @@
static-x86_64 = mkStatic { arch = "x86_64"; }; static-x86_64 = mkStatic { arch = "x86_64"; };
static-aarch64 = mkStatic { arch = "aarch64"; }; static-aarch64 = mkStatic { arch = "aarch64"; };
container-x86_64 = mkContainerFor packages'.static-x86_64; container-x86_64 = containerize packages'.static-x86_64;
container-aarch64 = mkContainerFor packages'.static-aarch64; container-aarch64 = containerize packages'.static-aarch64;
default = packages'.refraction; default = packages'.refraction;
} }

View file

@ -4,30 +4,34 @@
rust-overlay, rust-overlay,
pkgsCross, pkgsCross,
}: }:
{ arch }:
let let
targets = with pkgsCross; { crossPlatformFor = with pkgsCross; {
x86_64 = musl64.pkgsStatic; x86_64 = musl64.pkgsStatic;
aarch64 = aarch64-multiplatform.pkgsStatic; aarch64 = aarch64-multiplatform.pkgsStatic;
}; };
getRustcTarget = pkgs: pkgs.stdenv.hostPlatform.rust.rustcTarget;
toolchain = rust-overlay.rust.minimal.override { toolchain = rust-overlay.rust.minimal.override {
extensions = [ "rust-std" ]; extensions = [ "rust-std" ];
targets = lib.mapAttrsToList (lib.const getRustcTarget) targets; targets = lib.mapAttrsToList (_: pkgs: pkgs.stdenv.hostPlatform.rust.rustcTarget) crossPlatformFor;
}; };
mkRustPlatformWith = rustPlatformFor = lib.mapAttrs (
pkgs: _: pkgs:
pkgs.makeRustPlatform ( pkgs.makeRustPlatform (
lib.genAttrs [ lib.genAttrs [
"cargo" "cargo"
"rustc" "rustc"
] (lib.const toolchain) ] (_: toolchain)
); )
rustPlatforms = lib.mapAttrs (lib.const mkRustPlatformWith) targets; ) crossPlatformFor;
in in
refraction.override { { arch }:
rustPlatform = rustPlatforms.${arch}; (refraction.override {
rustPlatform = rustPlatformFor.${arch};
optimizeSize = true; optimizeSize = true;
} }).overrideAttrs
(old: {
passthru = old.passthru or { } // {
inherit toolchain;
};
})