Update nix flake with a devShells (plus many other improvements)

A lot of improvements got imported into here based on my work on the
garage nix setup.
This commit is contained in:
Brian Picciano 2024-01-08 20:13:48 +01:00
parent 0ed265db6f
commit c65a201222

150
flake.nix
View File

@ -5,39 +5,40 @@
nixpkgs.url = "github:NixOS/nixpkgs/nixos-22.11"; nixpkgs.url = "github:NixOS/nixpkgs/nixos-22.11";
}; };
outputs = { self, nixpkgs, naersk, fenix }: outputs = { self, nixpkgs, naersk, fenix }: let
let
newBuildTarget = {
nixPkgsSystem,
rustTarget ? nixPkgsSystem,
nativeBuildInputs ? pkgsCross: [],
rustFlags ? pkgsCross: [],
}: {
inherit nixPkgsSystem rustTarget nativeBuildInputs rustFlags;
};
buildTargets = { buildTargets = {
"x86_64-linux" = { "x86_64-linux" = newBuildTarget {
crossSystemConfig = "x86_64-unknown-linux-musl"; nixPkgsSystem = "x86_64-unknown-linux-musl";
rustTarget = "x86_64-unknown-linux-musl";
}; };
"i686-linux" = { "i686-linux" = newBuildTarget {
crossSystemConfig = "i686-unknown-linux-musl"; nixPkgsSystem = "i686-unknown-linux-musl";
rustTarget = "i686-unknown-linux-musl";
}; };
"aarch64-linux" = { "aarch64-linux" = newBuildTarget {
crossSystemConfig = "aarch64-unknown-linux-musl"; nixPkgsSystem = "aarch64-unknown-linux-musl";
rustTarget = "aarch64-unknown-linux-musl";
}; };
# Old Raspberry Pi's # Old Raspberry Pi's
"armv6l-linux" = { "armv6l-linux" = newBuildTarget {
crossSystemConfig = "armv6l-unknown-linux-musleabihf"; nixPkgsSystem = "armv6l-unknown-linux-musleabihf";
rustTarget = "arm-unknown-linux-musleabihf"; rustTarget = "arm-unknown-linux-musleabihf";
}; };
"x86_64-windows" = { "x86_64-windows" = newBuildTarget {
crossSystemConfig = "x86_64-w64-mingw32"; nixPkgsSystem = "x86_64-w64-mingw32";
rustTarget = "x86_64-pc-windows-gnu"; rustTarget = "x86_64-pc-windows-gnu";
makeBuildPackageAttrs = pkgsCross: { nativeBuildInputs = pkgsCross: [ pkgsCross.windows.pthreads ];
depsBuildBuild = [
pkgsCross.stdenv.cc
pkgsCross.windows.pthreads
];
};
}; };
}; };
@ -77,55 +78,54 @@
# build platform (and therefore it's not really cross-compiling). So we # build platform (and therefore it's not really cross-compiling). So we
# only set up the cross-compiling config if the target platform is # only set up the cross-compiling config if the target platform is
# different. # different.
crossSystem.config = buildTargets.${targetSystem}.crossSystemConfig; crossSystem.config = buildTargets.${targetSystem}.nixPkgsSystem;
})); }));
in { mkToolchain = buildSystem: targetSystem: let
packages = eachCrossSystem debug = v: builtins.trace v v;
(builtins.attrNames buildTargets) buildTarget = buildTargets.${targetSystem};
(buildSystem: targetSystem: let rustTarget = buildTarget.rustTarget;
pkgs = mkPkgs buildSystem null; #fenixPkgs = fenix.packages.${buildSystem};
pkgsCross = mkPkgs buildSystem targetSystem;
rustTarget = buildTargets.${targetSystem}.rustTarget; # TODO prefer to use the nix flake fenix
fenixPkgs = import ((mkPkgs buildSystem null).fetchFromGitHub {
owner = "nix-community";
repo = "fenix";
rev = "81ab0b4f7ae9ebb57daa0edf119c4891806e4d3a";
hash = "sha256-bZmI7ytPAYLpyFNgj5xirDkKuAniOkj1xHdv5aIJ5GM=";
}) {
system = buildSystem;
};
# TODO I'd prefer to use the toolchain file # TODO I'd prefer to use the toolchain file
# https://github.com/nix-community/fenix/issues/123 # https://github.com/nix-community/fenix/issues/123
#toolchain = fenix.packages.${buildSystem}.fromToolchainFile { fenixToolchain = fenixTarget: (builtins.getAttr "toolchainOf" fenixTarget) {
# file = ./rust-toolchain.toml;
# sha256 = "sha256-LU4C/i+maIOqBZagUaXpFyWZyOVfQ3Ah5/JTz7v6CG4=";
#};
fenixPkgs = fenix.packages.${buildSystem};
mkToolchain = fenixPkgs: fenixPkgs.toolchainOf {
channel = "nightly"; channel = "nightly";
date = "2023-07-23"; date = "2023-07-23";
sha256 = "sha256-LU4C/i+maIOqBZagUaXpFyWZyOVfQ3Ah5/JTz7v6CG4="; sha256 = "sha256-LU4C/i+maIOqBZagUaXpFyWZyOVfQ3Ah5/JTz7v6CG4=";
}; };
in
toolchain = fenixPkgs.combine [ fenixPkgs.combine [
(mkToolchain fenixPkgs).rustc (fenixToolchain fenixPkgs).rustc
(mkToolchain fenixPkgs).cargo (fenixToolchain fenixPkgs).rustfmt
(mkToolchain fenixPkgs.targets.${rustTarget}).rust-std (fenixToolchain fenixPkgs).cargo
(fenixToolchain fenixPkgs).clippy
(fenixToolchain (fenixPkgs.targets).${rustTarget}).rust-std
]; ];
buildPackageAttrs = if buildEnv = buildSystem: targetSystem: let
builtins.hasAttr "makeBuildPackageAttrs" buildTargets.${targetSystem} pkgs = mkPkgs buildSystem null;
then pkgsCross = mkPkgs buildSystem targetSystem;
buildTargets.${targetSystem}.makeBuildPackageAttrs pkgsCross buildTarget = buildTargets.${targetSystem};
else
{};
naersk-lib = pkgs.callPackage naersk {
cargo = toolchain;
rustc = toolchain;
};
in in
naersk-lib.buildPackage (buildPackageAttrs // rec { rec {
src = ./.; nativeBuildInputs = (buildTarget.nativeBuildInputs pkgsCross) ++ [
strictDeps = true; (mkToolchain buildSystem targetSystem)
doCheck = false;
# Required for shell because of rust dependency build scripts which
# must run on the build system.
pkgs.stdenv.cc
];
OPENSSL_STATIC = "1"; OPENSSL_STATIC = "1";
OPENSSL_LIB_DIR = "${pkgsCross.pkgsStatic.openssl.out}/lib"; OPENSSL_LIB_DIR = "${pkgsCross.pkgsStatic.openssl.out}/lib";
@ -135,7 +135,7 @@
# fixed some issues with the x86_64-windows cross-compile :shrug: # fixed some issues with the x86_64-windows cross-compile :shrug:
TARGET_CC = "${pkgsCross.stdenv.cc}/bin/${pkgsCross.stdenv.cc.targetPrefix}cc"; TARGET_CC = "${pkgsCross.stdenv.cc}/bin/${pkgsCross.stdenv.cc.targetPrefix}cc";
CARGO_BUILD_TARGET = rustTarget; CARGO_BUILD_TARGET = buildTarget.rustTarget;
CARGO_BUILD_RUSTFLAGS = [ CARGO_BUILD_RUSTFLAGS = [
"-C" "target-feature=+crt-static" "-C" "target-feature=+crt-static"
@ -146,7 +146,37 @@
# https://github.com/rust-lang/cargo/issues/4133 # https://github.com/rust-lang/cargo/issues/4133
"-C" "linker=${TARGET_CC}" "-C" "linker=${TARGET_CC}"
]; ];
}) };
in {
packages = eachCrossSystem
(builtins.attrNames buildTargets)
(buildSystem: targetSystem: let
pkgs = mkPkgs buildSystem null;
toolchain = mkToolchain buildSystem targetSystem;
naersk-lib = pkgs.callPackage naersk {
cargo = toolchain;
rustc = toolchain;
};
in
naersk-lib.buildPackage (rec {
src = ./.;
strictDeps = true;
doCheck = false;
release = true;
} // (buildEnv buildSystem targetSystem))
);
devShells = eachCrossSystem
(builtins.attrNames buildTargets)
(buildSystem: targetSystem: let
pkgs = mkPkgs buildSystem null;
toolchain = mkToolchain buildSystem targetSystem;
in
pkgs.mkShell ({
inputsFrom = []; # extra packages for dev
} // (buildEnv buildSystem targetSystem))
); );
}; };
} }