From faa038757ced955bb5c0a98dae7be6e7185af677 Mon Sep 17 00:00:00 2001 From: seth Date: Wed, 20 Mar 2024 02:28:43 -0400 Subject: nix: cleanup derivation + static package --- nix/deployment.nix | 61 +++++++------------------------ nix/derivation.nix | 104 +++++++++++++++++++++++++++-------------------------- nix/packages.nix | 7 ---- nix/static.nix | 38 ++++++++++++++++++++ 4 files changed, 104 insertions(+), 106 deletions(-) create mode 100644 nix/static.nix diff --git a/nix/deployment.nix b/nix/deployment.nix index 88a9f51..514b307 100644 --- a/nix/deployment.nix +++ b/nix/deployment.nix @@ -14,58 +14,21 @@ system, config, inputs', + self', ... }: let - crossPkgs = { - "x86_64-linux" = { - "x86_64" = pkgs.pkgsStatic; - "aarch64" = pkgs.pkgsCross.aarch64-multiplatform.pkgsStatic; - }; - - "aarch64-linux" = { - "x86_64" = pkgs.pkgsCross.musl64; - "aarch64" = pkgs.pkgsStatic; - }; - - "x86_64-darwin" = { - "x86_64" = pkgs.pkgsCross.musl64; - "aarch64" = pkgs.pkgsCross.aarch64-multiplatform.pkgsStatic; - }; - - "aarch64-darwin" = crossPkgs."x86_64-darwin"; + crossPkgs = with pkgs.pkgsCross; { + x86_64 = musl64; + aarch64 = aarch64-multiplatform.pkgsStatic; }; - teawieFor = arch: let - inherit (crossPkgs.${system}.${arch}.llvmPackages.stdenv) cc; - - target = "${arch}-unknown-linux-musl"; - target' = builtins.replaceStrings ["-"] ["_"] target; - targetUpper = lib.toUpper target'; - - toolchain = with inputs'.fenix.packages; - combine [ - minimal.cargo - minimal.rustc - targets.${target}.latest.rust-std - ]; - - naersk = inputs.naersk.lib.${system}.override { - cargo = toolchain; - rustc = toolchain; + teawieFor = arch: + pkgs.callPackage ./static.nix { + inherit (self'.packages) teawiebot; + pkgsStatic = crossPkgs.${arch}; + fenix = inputs'.fenix.packages; + naersk = inputs.naersk.lib.${system}; }; - in - (config.packages.teawiebot.override { - inherit naersk; - lto = true; - optimizeSize = true; - }) - .overrideAttrs (new: - lib.const { - CARGO_BUILD_TARGET = target; - "CC_${target'}" = "${cc}/bin/${cc.targetPrefix}cc"; - "CARGO_TARGET_${targetUpper}_RUSTFLAGS" = "-C target-feature=+crt-static"; - "CARGO_TARGET_${targetUpper}_LINKER" = new."CC_${target'}"; - }); containerFor = arch: pkgs.dockerTools.buildLayeredImage { @@ -73,10 +36,10 @@ tag = "latest-${arch}"; contents = [pkgs.dockerTools.caCertificates]; config.Cmd = [ - (lib.getExe (teawieFor arch)) + (lib.getExe self'.packages."teawiebot-static-${arch}") ]; - architecture = crossPkgs.${system}.${arch}.go.GOARCH; + architecture = crossPkgs.${arch}.go.GOARCH; }; in { packages = { diff --git a/nix/derivation.nix b/nix/derivation.nix index 5c504ee..3456e6c 100644 --- a/nix/derivation.nix +++ b/nix/derivation.nix @@ -2,57 +2,61 @@ lib, stdenv, naersk, - CoreFoundation, - Security, - SystemConfiguration, + darwin, self, lto ? false, optimizeSize ? false, -}: let - filter = path: type: let - path' = toString path; - base = baseNameOf path'; - parent = baseNameOf (dirOf path'); - - dirBlocklist = ["parts"]; - - matches = lib.any (suffix: lib.hasSuffix suffix base) [".rs"]; - isCargo = base == "Cargo.lock" || base == "Cargo.toml"; - isCopypasta = parent == "copypastas"; - isAllowedDir = !(builtins.elem base dirBlocklist); - in - (type == "directory" && isAllowedDir) || matches || isCargo || isCopypasta; - - filterSource = src: - lib.cleanSourceWith { - src = lib.cleanSource src; - inherit filter; - }; -in - naersk.buildPackage { - pname = "teawiebot"; - version = builtins.substring 0 8 self.lastModifiedDate or "dirty"; - - src = filterSource ../.; - - buildInputs = lib.optionals stdenv.hostPlatform.isDarwin [ - CoreFoundation - Security - SystemConfiguration +}: +naersk.buildPackage { + pname = "teawiebot"; + version = + toString (lib.importTOML ../Cargo.toml).package.version + + "-${self.shortRev or self.dirtyShortRev or "dirty"}"; + + src = lib.fileset.toSource { + root = ../.; + fileset = lib.fileset.unions [ + ../src + ../Cargo.toml + ../Cargo.lock + ../build.rs ]; - - GIT_SHA = builtins.substring 0 7 self.rev or "dirty"; - - RUSTFLAGS = - lib.optionalString lto " -C lto=thin -C embed-bitcode=yes -Zdylib-lto" - + lib.optionalString optimizeSize " -C codegen-units=1 -C panic=abort -C strip=symbols -C opt-level=z"; - - meta = with lib; { - mainProgram = "teawiebot"; - description = "funni bot"; - homepage = "https://github.com/getchoo/teawiebot"; - license = licenses.mit; - platforms = with platforms; linux ++ darwin; - maintainers = with maintainers; [getchoo]; - }; - } + }; + + buildInputs = lib.optionals stdenv.isDarwin (with darwin.apple_sdk.frameworks; [ + CoreFoundation + Security + SystemConfiguration + ]); + + env = { + GIT_SHA = self.shortRev or self.dirtyShortRev or "unknown-dirty"; + CARGO_BUILD_RUSTFLAGS = lib.concatStringsSep " " ( + lib.optionals lto [ + "-C" + "lto=thin" + "-C" + "embed-bitcode=yes" + "-Zdylib-lto" + ] + ++ lib.optionals optimizeSize [ + "-C" + "codegen-units=1" + "-C" + "panic=abort" + "-C" + "strip=symbols" + "-C" + "opt-level=z" + ] + ); + }; + + meta = with lib; { + mainProgram = "teawiebot"; + description = "funni bot"; + homepage = "https://github.com/getchoo/teawiebot"; + license = licenses.mit; + maintainers = with maintainers; [getchoo]; + }; +} diff --git a/nix/packages.nix b/nix/packages.nix index 85a2cfe..ba6dc6e 100644 --- a/nix/packages.nix +++ b/nix/packages.nix @@ -12,13 +12,6 @@ packages = { teawiebot = pkgs.callPackage ./derivation.nix { inherit self; - inherit - (pkgs.darwin.apple_sdk.frameworks) - CoreFoundation - Security - SystemConfiguration - ; - naersk = inputs.naersk.lib.${system}; }; diff --git a/nix/static.nix b/nix/static.nix new file mode 100644 index 0000000..22fbad6 --- /dev/null +++ b/nix/static.nix @@ -0,0 +1,38 @@ +{ + lib, + pkgsStatic, + fenix, + naersk, + teawiebot, +}: let + inherit (pkgsStatic.stdenv) cc; + + target = pkgsStatic.stdenv.hostPlatform.config; + target' = builtins.replaceStrings ["-"] ["_"] target; + targetUpper = lib.toUpper target'; + + toolchain = with fenix; + combine [ + minimal.cargo + minimal.rustc + targets.${target}.latest.rust-std + ]; + + naersk' = naersk.override { + cargo = toolchain; + rustc = toolchain; + }; +in + (teawiebot.override { + naersk = naersk'; + lto = true; + optimizeSize = true; + }) + .overrideAttrs (new: old: { + env = { + "CC_${target'}" = "${cc}/bin/${cc.targetPrefix}cc"; + CARGO_BUILD_TARGET = target; + CARGO_BUILD_RUSTFLAGS = old.env.CARGO_BUILD_RUSTFLAGS + " -C target-feature=+crt-static"; + "CARGO_TARGET_${targetUpper}_LINKER" = new.env."CC_${target'}"; + }; + }) -- cgit v1.2.3