Compare commits
3 Commits
0ed265db6f
...
9c6e3c6240
Author | SHA1 | Date | |
---|---|---|---|
9c6e3c6240 | |||
7e5aeb5f28 | |||
c65a201222 |
11
README.md
11
README.md
@ -30,6 +30,17 @@ nix build
|
|||||||
|
|
||||||
A statically compiled binary will be placed in the `result` directory.
|
A statically compiled binary will be placed in the `result` directory.
|
||||||
|
|
||||||
|
A full release, which cross-compiles binaries for all supported platforms, can
|
||||||
|
be generated by doing:
|
||||||
|
|
||||||
|
```
|
||||||
|
nix build '.#release'
|
||||||
|
```
|
||||||
|
|
||||||
|
*NOTE* that cross-compiling will download a separate build environment for each
|
||||||
|
target platform, which can take up quite a bit of disk space (>10GB).
|
||||||
|
gigs).
|
||||||
|
|
||||||
## Configuration
|
## Configuration
|
||||||
|
|
||||||
Domani is configured via a YAML file whose path is given on the command-line.
|
Domani is configured via a YAML file whose path is given on the command-line.
|
||||||
|
207
flake.nix
207
flake.nix
@ -5,39 +5,46 @@
|
|||||||
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: [
|
||||||
depsBuildBuild = [
|
pkgsCross.stdenv.cc
|
||||||
pkgsCross.stdenv.cc
|
pkgsCross.windows.pthreads
|
||||||
pkgsCross.windows.pthreads
|
];
|
||||||
];
|
rustFlags = pkgsCross: [
|
||||||
};
|
"-C" "link-arg=-L${pkgsCross.windows.pthreads}/lib"
|
||||||
|
];
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -61,12 +68,29 @@
|
|||||||
# "$system.cross-$system" for every system.
|
# "$system.cross-$system" for every system.
|
||||||
#
|
#
|
||||||
eachCrossSystem = supportedSystems: callback:
|
eachCrossSystem = supportedSystems: callback:
|
||||||
eachSystem supportedSystems (buildSystem: builtins.foldl'
|
eachSystem supportedSystems (buildSystem: let
|
||||||
|
pkgs = mkPkgs buildSystem null;
|
||||||
|
crosses = builtins.foldl'
|
||||||
(inner: targetSystem: inner // {
|
(inner: targetSystem: inner // {
|
||||||
"cross-${targetSystem}" = callback buildSystem targetSystem;
|
"cross-${targetSystem}" = callback buildSystem targetSystem;
|
||||||
})
|
})
|
||||||
{ default = callback buildSystem buildSystem; }
|
{ default = callback buildSystem buildSystem; }
|
||||||
supportedSystems
|
supportedSystems;
|
||||||
|
in
|
||||||
|
crosses // (rec {
|
||||||
|
default = callback buildSystem buildSystem;
|
||||||
|
release = let
|
||||||
|
bins = pkgs.symlinkJoin {
|
||||||
|
name = "${default.name}-all-bins";
|
||||||
|
paths = builtins.attrValues crosses;
|
||||||
|
};
|
||||||
|
in
|
||||||
|
pkgs.runCommand "${default.name}-release" {} ''
|
||||||
|
cp -rL "${bins}" "$out"
|
||||||
|
chmod +w "$out"/bin
|
||||||
|
(cd "$out"/bin && sha256sum * > sha256.txt)
|
||||||
|
'';
|
||||||
|
})
|
||||||
);
|
);
|
||||||
|
|
||||||
mkPkgs = buildSystem: targetSystem: import nixpkgs ({
|
mkPkgs = buildSystem: targetSystem: import nixpkgs ({
|
||||||
@ -77,76 +101,105 @@
|
|||||||
# 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;
|
||||||
}));
|
}));
|
||||||
|
|
||||||
|
mkToolchain = buildSystem: targetSystem: let
|
||||||
|
buildTarget = buildTargets.${targetSystem};
|
||||||
|
rustTarget = buildTarget.rustTarget;
|
||||||
|
fenixPkgs = fenix.packages.${buildSystem};
|
||||||
|
|
||||||
|
# TODO I'd prefer to use the toolchain file
|
||||||
|
# https://github.com/nix-community/fenix/issues/123
|
||||||
|
fenixToolchain = fenixTarget: (builtins.getAttr "toolchainOf" fenixTarget) {
|
||||||
|
channel = "nightly";
|
||||||
|
date = "2023-07-23";
|
||||||
|
sha256 = "sha256-LU4C/i+maIOqBZagUaXpFyWZyOVfQ3Ah5/JTz7v6CG4=";
|
||||||
|
};
|
||||||
|
in
|
||||||
|
fenixPkgs.combine [
|
||||||
|
(fenixToolchain fenixPkgs).rustc
|
||||||
|
(fenixToolchain fenixPkgs).rustfmt
|
||||||
|
(fenixToolchain fenixPkgs).cargo
|
||||||
|
(fenixToolchain fenixPkgs).clippy
|
||||||
|
(fenixToolchain (fenixPkgs.targets).${rustTarget}).rust-std
|
||||||
|
];
|
||||||
|
|
||||||
|
buildEnv = buildSystem: targetSystem: let
|
||||||
|
pkgs = mkPkgs buildSystem null;
|
||||||
|
pkgsCross = mkPkgs buildSystem targetSystem;
|
||||||
|
buildTarget = buildTargets.${targetSystem};
|
||||||
|
in
|
||||||
|
rec {
|
||||||
|
nativeBuildInputs = (buildTarget.nativeBuildInputs pkgsCross) ++ [
|
||||||
|
(mkToolchain buildSystem targetSystem)
|
||||||
|
|
||||||
|
# Required for shell because of rust dependency build scripts which
|
||||||
|
# must run on the build system.
|
||||||
|
pkgs.stdenv.cc
|
||||||
|
];
|
||||||
|
|
||||||
|
OPENSSL_STATIC = "1";
|
||||||
|
OPENSSL_LIB_DIR = "${pkgsCross.pkgsStatic.openssl.out}/lib";
|
||||||
|
OPENSSL_INCLUDE_DIR = "${pkgsCross.pkgsStatic.openssl.dev}/include";
|
||||||
|
|
||||||
|
# Required because ring crate is special. This also seems to have
|
||||||
|
# fixed some issues with the x86_64-windows cross-compile :shrug:
|
||||||
|
TARGET_CC = "${pkgsCross.stdenv.cc}/bin/${pkgsCross.stdenv.cc.targetPrefix}cc";
|
||||||
|
|
||||||
|
CARGO_BUILD_TARGET = buildTarget.rustTarget;
|
||||||
|
CARGO_BUILD_RUSTFLAGS = [
|
||||||
|
"-C" "target-feature=+crt-static"
|
||||||
|
|
||||||
|
# -latomic is required to build openssl-sys for armv6l-linux, but
|
||||||
|
# it doesn't seem to hurt any other builds.
|
||||||
|
"-C" "link-args=-static -latomic"
|
||||||
|
|
||||||
|
# https://github.com/rust-lang/cargo/issues/4133
|
||||||
|
"-C" "linker=${TARGET_CC}"
|
||||||
|
] ++ (buildTarget.rustFlags pkgsCross);
|
||||||
|
};
|
||||||
|
|
||||||
in {
|
in {
|
||||||
|
|
||||||
packages = eachCrossSystem
|
packages = eachCrossSystem
|
||||||
(builtins.attrNames buildTargets)
|
(builtins.attrNames buildTargets)
|
||||||
(buildSystem: targetSystem: let
|
(buildSystem: targetSystem: let
|
||||||
pkgs = mkPkgs buildSystem null;
|
pkgs = mkPkgs buildSystem null;
|
||||||
pkgsCross = mkPkgs buildSystem targetSystem;
|
toolchain = mkToolchain buildSystem targetSystem;
|
||||||
rustTarget = buildTargets.${targetSystem}.rustTarget;
|
|
||||||
|
|
||||||
# TODO I'd prefer to use the toolchain file
|
|
||||||
# https://github.com/nix-community/fenix/issues/123
|
|
||||||
#toolchain = fenix.packages.${buildSystem}.fromToolchainFile {
|
|
||||||
# file = ./rust-toolchain.toml;
|
|
||||||
# sha256 = "sha256-LU4C/i+maIOqBZagUaXpFyWZyOVfQ3Ah5/JTz7v6CG4=";
|
|
||||||
#};
|
|
||||||
|
|
||||||
fenixPkgs = fenix.packages.${buildSystem};
|
|
||||||
|
|
||||||
mkToolchain = fenixPkgs: fenixPkgs.toolchainOf {
|
|
||||||
channel = "nightly";
|
|
||||||
date = "2023-07-23";
|
|
||||||
sha256 = "sha256-LU4C/i+maIOqBZagUaXpFyWZyOVfQ3Ah5/JTz7v6CG4=";
|
|
||||||
};
|
|
||||||
|
|
||||||
toolchain = fenixPkgs.combine [
|
|
||||||
(mkToolchain fenixPkgs).rustc
|
|
||||||
(mkToolchain fenixPkgs).cargo
|
|
||||||
(mkToolchain fenixPkgs.targets.${rustTarget}).rust-std
|
|
||||||
];
|
|
||||||
|
|
||||||
buildPackageAttrs = if
|
|
||||||
builtins.hasAttr "makeBuildPackageAttrs" buildTargets.${targetSystem}
|
|
||||||
then
|
|
||||||
buildTargets.${targetSystem}.makeBuildPackageAttrs pkgsCross
|
|
||||||
else
|
|
||||||
{};
|
|
||||||
|
|
||||||
naersk-lib = pkgs.callPackage naersk {
|
naersk-lib = pkgs.callPackage naersk {
|
||||||
cargo = toolchain;
|
cargo = toolchain;
|
||||||
rustc = toolchain;
|
rustc = toolchain;
|
||||||
};
|
};
|
||||||
|
|
||||||
in
|
in
|
||||||
naersk-lib.buildPackage (buildPackageAttrs // rec {
|
naersk-lib.buildPackage (rec {
|
||||||
src = ./.;
|
src = ./.;
|
||||||
strictDeps = true;
|
strictDeps = true;
|
||||||
doCheck = false;
|
doCheck = false;
|
||||||
|
release = true;
|
||||||
|
postInstall = ''
|
||||||
|
cd "$out"/bin
|
||||||
|
for f in "$(ls)"; do
|
||||||
|
if ext="$(echo "$f" | grep -oP '\.[a-z]+$')"; then
|
||||||
|
base="$(echo "$f" | cut -d. -f1)"
|
||||||
|
mv "$f" "$base-${targetSystem}$ext"
|
||||||
|
else
|
||||||
|
mv "$f" "$f-${targetSystem}"
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
'';
|
||||||
|
} // (buildEnv buildSystem targetSystem))
|
||||||
|
);
|
||||||
|
|
||||||
OPENSSL_STATIC = "1";
|
devShells = eachCrossSystem
|
||||||
OPENSSL_LIB_DIR = "${pkgsCross.pkgsStatic.openssl.out}/lib";
|
(builtins.attrNames buildTargets)
|
||||||
OPENSSL_INCLUDE_DIR = "${pkgsCross.pkgsStatic.openssl.dev}/include";
|
(buildSystem: targetSystem: let
|
||||||
|
pkgs = mkPkgs buildSystem null;
|
||||||
# Required because ring crate is special. This also seems to have
|
toolchain = mkToolchain buildSystem targetSystem;
|
||||||
# fixed some issues with the x86_64-windows cross-compile :shrug:
|
in
|
||||||
TARGET_CC = "${pkgsCross.stdenv.cc}/bin/${pkgsCross.stdenv.cc.targetPrefix}cc";
|
pkgs.mkShell ({
|
||||||
|
inputsFrom = []; # extra packages for dev
|
||||||
CARGO_BUILD_TARGET = rustTarget;
|
} // (buildEnv buildSystem targetSystem))
|
||||||
CARGO_BUILD_RUSTFLAGS = [
|
|
||||||
"-C" "target-feature=+crt-static"
|
|
||||||
|
|
||||||
# -latomic is required to build openssl-sys for armv6l-linux, but
|
|
||||||
# it doesn't seem to hurt any other builds.
|
|
||||||
"-C" "link-args=-static -latomic"
|
|
||||||
|
|
||||||
# https://github.com/rust-lang/cargo/issues/4133
|
|
||||||
"-C" "linker=${TARGET_CC}"
|
|
||||||
];
|
|
||||||
})
|
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user