nix: naersk/fenix -> rustPlatform/rust-overlay

This commit is contained in:
seth 2024-03-24 15:08:45 -04:00
parent 6bd5db7208
commit 3c4cf67bbf
No known key found for this signature in database
GPG key ID: D31BD0D494BBEE86
9 changed files with 164 additions and 242 deletions

View file

@ -1,92 +0,0 @@
{
inputs,
flake-parts-lib,
withSystem,
...
}: {
flake.nixosModules.default = flake-parts-lib.importApply ./module.nix {
inherit withSystem;
};
perSystem = {
lib,
pkgs,
system,
self',
inputs',
...
}: let
crossPkgs =
rec {
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 = x86_64-darwin;
}
.${system};
refractionFor = arch: let
inherit (crossPkgs.${arch}.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;
};
refraction = self'.packages.refraction.override {
lto = true;
naersk = naersk';
};
newAttrs = {
CARGO_BUILD_TARGET = target;
"CC_${target'}" = "${cc}/bin/${cc.targetPrefix}cc";
"CARGO_TARGET_${targetUpper}_RUSTFLAGS" = "-C target-feature=+crt-static";
"CARGO_TARGET_${targetUpper}_LINKER" = newAttrs."CC_${target'}";
};
in
refraction.overrideAttrs newAttrs;
containerFor = arch:
pkgs.dockerTools.buildLayeredImage {
name = "refraction";
tag = "latest-${arch}";
contents = [pkgs.dockerTools.caCertificates];
config.Cmd = [
(lib.getExe (refractionFor arch))
];
architecture = crossPkgs.${arch}.go.GOARCH;
};
mkPackagesFor = arch: {
"refraction-static-${arch}" = refractionFor arch;
"container-${arch}" = containerFor arch;
};
in {
legacyPackages = lib.attrsets.mergeAttrsList (map mkPackagesFor ["x86_64" "aarch64"]);
};
}

View file

@ -0,0 +1,35 @@
{
flake-parts-lib,
withSystem,
...
}: {
imports = [./static.nix];
flake.nixosModules.default = flake-parts-lib.importApply ./module.nix {
inherit withSystem;
};
perSystem = {
lib,
pkgs,
self',
...
}: let
containerFor = arch:
pkgs.dockerTools.buildLayeredImage {
name = "refraction";
tag = "latest-${arch}";
contents = [pkgs.dockerTools.caCertificates];
config.Cmd = [
(lib.getExe self'.packages."refraction-static-${arch}")
];
architecture = withSystem "${arch}-linux" ({pkgs, ...}: pkgs.pkgsStatic.go.GOARCH);
};
in {
packages = {
container-x86_64 = containerFor "x86_64";
container-aarch64 = containerFor "aarch64";
};
};
}

41
nix/deployment/static.nix Normal file
View file

@ -0,0 +1,41 @@
{
perSystem = {
lib,
pkgs,
inputs',
self',
...
}: let
targets = with pkgs.pkgsCross; {
x86_64 = musl64.pkgsStatic;
aarch64 = aarch64-multiplatform.pkgsStatic;
};
toolchain = inputs'.rust-overlay.packages.rust.minimal.override {
extensions = ["rust-std"];
targets = map (pkgs: pkgs.stdenv.hostPlatform.config) (lib.attrValues targets);
};
rustPlatforms =
lib.mapAttrs (
lib.const (pkgs:
pkgs.makeRustPlatform (
lib.genAttrs ["cargo" "rustc"] (lib.const toolchain)
))
)
targets;
buildWith = rustPlatform:
self'.packages.refraction.override {
inherit rustPlatform;
lto = true;
};
in {
packages =
lib.mapAttrs' (
target: rustPlatform:
lib.nameValuePair "refraction-static-${target}" (buildWith rustPlatform)
)
rustPlatforms;
};
}

View file

@ -1,14 +1,18 @@
{
lib,
stdenv,
naersk,
rustPlatform,
darwin,
version,
self,
lto ? false,
}:
naersk.buildPackage {
rustPlatform.buildRustPackage {
pname = "refraction";
inherit version;
version =
(lib.importTOML ../Cargo.toml).package.version
+ "-${self.shortRev or self.dirtyShortRev or "unknown-dirty"}";
__structuredAttrs = true;
src = lib.fileset.toSource {
root = ../.;
@ -21,13 +25,21 @@ naersk.buildPackage {
];
};
cargoLock = {
lockFile = ../Cargo.lock;
};
buildInputs = lib.optionals stdenv.hostPlatform.isDarwin (with darwin.apple_sdk.frameworks; [
CoreFoundation
Security
SystemConfiguration
]);
cargoBuildFlags = lib.optionals lto ["-C" "lto=thin" "-C" "embed-bitcode=yes" "-Zdylib-lto"];
env = {
CARGO_BUILD_RUSTFLAGS = lib.concatStringsSep " " (
lib.optionals lto ["-C" "lto=thin" "-C" "embed-bitcode=yes" "-Zdylib-lto"]
);
};
meta = with lib; {
mainProgram = "refraction";

View file

@ -5,9 +5,7 @@
config,
self',
...
}: let
enableAll = lib.flip lib.genAttrs (lib.const {enable = true;});
in {
}: {
devShells.default = pkgs.mkShell {
shellHook = ''
${config.pre-commit.installationScript}
@ -39,12 +37,12 @@
treefmt = {
projectRootFile = "flake.nix";
programs = enableAll [
"alejandra"
"deadnix"
"prettier"
"rustfmt"
];
programs = {
alejandra.enable = true;
deadnix.enable = true;
prettier.enable = true;
rustfmt.enable = true;
};
settings.global = {
excludes = [
@ -55,15 +53,14 @@
};
};
pre-commit.settings = {
settings.treefmt.package = config.treefmt.build.wrapper;
hooks = enableAll [
"actionlint"
"nil"
"statix"
"treefmt"
];
pre-commit.settings.hooks = {
actionlint.enable = true;
nil.enable = true;
statix.enable = true;
treefmt = {
enable = true;
package = config.treefmt.build.wrapper;
};
};
procfiles.daemons.processes = {

View file

@ -1,19 +1,11 @@
{
self,
inputs,
...
}: {
{self, ...}: {
perSystem = {
pkgs,
system,
self',
...
}: {
packages = {
refraction = pkgs.callPackage ./derivation.nix {
version = builtins.substring 0 7 self.rev or "dirty";
naersk = inputs.naersk.lib.${system};
};
refraction = pkgs.callPackage ./derivation.nix {inherit self;};
default = self'.packages.refraction;
};