flake.nix: add static cross-compiled builds

The nix invocations come from the garage .nix files
This commit is contained in:
Armaël Guéneau 2024-08-02 11:21:31 +02:00
parent c8854fcbd4
commit cd360b329f
2 changed files with 71 additions and 16 deletions

44
compile.nix Normal file
View file

@ -0,0 +1,44 @@
{ system, target ? null, pkgsSrc, cargo2nixOverlay, compiler ? "rustc",
release ? false, }:
let
pkgs = if target != null then
import pkgsSrc {
inherit system;
crossSystem = {
config = target;
isStatic = true;
};
overlays = [ cargo2nixOverlay ];
}
else
import pkgsSrc {
inherit system;
overlays = [ cargo2nixOverlay ];
};
toolchainOptions = {
rustVersion = "1.75.0";
};
packageFun = import ./Cargo.nix;
codegenOpts = {
"armv6l-unknown-linux-musleabihf" = [
"target-feature=+crt-static"
"link-arg=-static"
]; # compile as dynamic with static-pie
"x86_64-unknown-linux-musl" =
[ "target-feature=+crt-static" "link-arg=-static-pie" ];
};
# NixOS and Rust/Cargo triples do not match for ARM, fix it here.
rustTarget = if target == "armv6l-unknown-linux-musleabihf" then
"arm-unknown-linux-musleabihf"
else
target;
in pkgs.rustBuilder.makePackageSet ({
inherit release packageFun codegenOpts;
target = rustTarget;
} // toolchainOptions)

View file

@ -10,25 +10,36 @@
}; };
outputs = { self, nixpkgs, cargo2nix, flake-utils }: outputs = { self, nixpkgs, cargo2nix, flake-utils }:
let
compile = import ./compile.nix;
in
flake-utils.lib.eachDefaultSystem (system: flake-utils.lib.eachDefaultSystem (system:
let let
pkgs = import nixpkgs { pkgs = nixpkgs.legacyPackages.${system};
inherit system; in
overlays = [cargo2nix.overlays.default]; rec {
}; packages =
let
rustPkgs = pkgs.rustBuilder.makePackageSet { packageFor = target: (compile {
rustVersion = "1.75.0"; inherit system target;
packageFun = import ./Cargo.nix; pkgsSrc = nixpkgs;
}; cargo2nixOverlay = cargo2nix.overlays.default;
release = true;
in rec { }).workspace.restic-alarm { compileMode = "build"; };
packages = { in
restic-alarm = (rustPkgs.workspace.restic-alarm {}); {
default = packages.restic-alarm; # default = native release build
}; default = packages.restic-alarm;
restic-alarm = packageFor null;
# other = cross-compiled, statically-linked builds
amd64 = packageFor "x86_64-unknown-linux-musl";
i386 = packageFor "i686-unknown-linux-musl";
arm64 = packageFor "aarch64-unknown-linux-musl";
arm = packageFor "armv6l-unknown-linux-musleabihf";
};
} }
) // rec { )
// rec {
nixosModules.restic-alarm = { config, lib, pkgs, ... }: nixosModules.restic-alarm = { config, lib, pkgs, ... }:
with lib; with lib;
let let