2023-05-03 07:58:06 +00:00
|
|
|
{
|
|
|
|
inputs = {
|
2023-11-12 09:57:02 +00:00
|
|
|
fenix.url = "github:nix-community/fenix";
|
2024-02-27 10:30:28 +00:00
|
|
|
fenix.inputs.nixpkgs.follows = "nixpkgs";
|
2023-05-03 07:58:06 +00:00
|
|
|
naersk.url = "github:nix-community/naersk/master";
|
2023-05-04 12:56:31 +00:00
|
|
|
nixpkgs.url = "github:NixOS/nixpkgs/nixos-22.11";
|
2023-05-03 07:58:06 +00:00
|
|
|
};
|
|
|
|
|
2024-01-08 19:13:48 +00:00
|
|
|
outputs = { self, nixpkgs, naersk, fenix }: let
|
|
|
|
|
|
|
|
newBuildTarget = {
|
|
|
|
nixPkgsSystem,
|
|
|
|
rustTarget ? nixPkgsSystem,
|
|
|
|
nativeBuildInputs ? pkgsCross: [],
|
|
|
|
rustFlags ? pkgsCross: [],
|
|
|
|
}: {
|
|
|
|
inherit nixPkgsSystem rustTarget nativeBuildInputs rustFlags;
|
|
|
|
};
|
|
|
|
|
2023-11-12 09:57:02 +00:00
|
|
|
buildTargets = {
|
2024-01-08 19:13:48 +00:00
|
|
|
"x86_64-linux" = newBuildTarget {
|
|
|
|
nixPkgsSystem = "x86_64-unknown-linux-musl";
|
2023-11-12 09:57:02 +00:00
|
|
|
};
|
|
|
|
|
2024-01-08 19:13:48 +00:00
|
|
|
"i686-linux" = newBuildTarget {
|
|
|
|
nixPkgsSystem = "i686-unknown-linux-musl";
|
2023-11-12 09:57:02 +00:00
|
|
|
};
|
|
|
|
|
2024-01-08 19:13:48 +00:00
|
|
|
"aarch64-linux" = newBuildTarget {
|
|
|
|
nixPkgsSystem = "aarch64-unknown-linux-musl";
|
2023-11-12 09:57:02 +00:00
|
|
|
};
|
|
|
|
|
2023-11-15 19:39:15 +00:00
|
|
|
# Old Raspberry Pi's
|
2024-01-08 19:13:48 +00:00
|
|
|
"armv6l-linux" = newBuildTarget {
|
|
|
|
nixPkgsSystem = "armv6l-unknown-linux-musleabihf";
|
2023-11-15 19:39:15 +00:00
|
|
|
rustTarget = "arm-unknown-linux-musleabihf";
|
|
|
|
};
|
|
|
|
|
2024-01-08 19:13:48 +00:00
|
|
|
"x86_64-windows" = newBuildTarget {
|
|
|
|
nixPkgsSystem = "x86_64-w64-mingw32";
|
2023-11-12 09:57:02 +00:00
|
|
|
rustTarget = "x86_64-pc-windows-gnu";
|
2024-01-09 10:17:52 +00:00
|
|
|
nativeBuildInputs = pkgsCross: [
|
|
|
|
pkgsCross.stdenv.cc
|
|
|
|
pkgsCross.windows.pthreads
|
|
|
|
];
|
|
|
|
rustFlags = pkgsCross: [
|
|
|
|
"-C" "link-arg=-L${pkgsCross.windows.pthreads}/lib"
|
|
|
|
];
|
2023-11-12 09:57:02 +00:00
|
|
|
};
|
2023-11-08 20:40:25 +00:00
|
|
|
};
|
2023-05-08 16:25:51 +00:00
|
|
|
|
2023-11-12 09:57:02 +00:00
|
|
|
# eachSystem [system] (system: ...)
|
|
|
|
#
|
|
|
|
# Returns an attrset with a key for every system in the given array, with
|
|
|
|
# the key's value being the result of calling the callback with that key.
|
|
|
|
eachSystem = supportedSystems: callback: builtins.foldl'
|
|
|
|
(overall: system: overall // { ${system} = callback system; })
|
|
|
|
{}
|
|
|
|
supportedSystems;
|
2023-11-10 11:58:11 +00:00
|
|
|
|
2024-01-15 16:07:55 +00:00
|
|
|
# eachCrossSystem [system] ({buildSystem, targetSystem, isDefault }: ...)
|
2023-11-12 09:57:02 +00:00
|
|
|
#
|
|
|
|
# Returns an attrset with a key "$buildSystem.cross-$targetSystem" for
|
|
|
|
# every combination of the elements of the array of system strings. The
|
|
|
|
# value of the attrs will be the result of calling the callback with each
|
|
|
|
# combination.
|
|
|
|
#
|
|
|
|
# There will also be keys "$system.default", which are aliases of
|
|
|
|
# "$system.cross-$system" for every system.
|
|
|
|
#
|
|
|
|
eachCrossSystem = supportedSystems: callback:
|
2024-01-09 10:17:52 +00:00
|
|
|
eachSystem supportedSystems (buildSystem: let
|
|
|
|
pkgs = mkPkgs buildSystem null;
|
|
|
|
crosses = builtins.foldl'
|
2023-11-12 09:57:02 +00:00
|
|
|
(inner: targetSystem: inner // {
|
2024-01-15 16:07:55 +00:00
|
|
|
"cross-${targetSystem}" = callback {
|
|
|
|
inherit buildSystem targetSystem;
|
|
|
|
isDefault = false;
|
|
|
|
};
|
2023-11-08 20:40:25 +00:00
|
|
|
})
|
2024-01-15 16:07:55 +00:00
|
|
|
{}
|
2024-01-09 10:17:52 +00:00
|
|
|
supportedSystems;
|
|
|
|
in
|
|
|
|
crosses // (rec {
|
2024-01-15 16:07:55 +00:00
|
|
|
default = callback {
|
|
|
|
inherit buildSystem;
|
|
|
|
targetSystem = buildSystem;
|
|
|
|
isDefault = true;
|
|
|
|
};
|
2024-01-09 10:17:52 +00:00
|
|
|
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)
|
|
|
|
'';
|
|
|
|
})
|
2023-11-12 09:57:02 +00:00
|
|
|
);
|
2023-05-08 16:25:51 +00:00
|
|
|
|
2023-11-12 09:57:02 +00:00
|
|
|
mkPkgs = buildSystem: targetSystem: import nixpkgs ({
|
2023-11-08 20:40:25 +00:00
|
|
|
system = buildSystem;
|
2023-11-12 09:57:02 +00:00
|
|
|
} // (if targetSystem == null then {} else {
|
2023-11-08 20:40:25 +00:00
|
|
|
# The nixpkgs cache doesn't have any packages where cross-compiling has
|
|
|
|
# been enabled, even if the target platform is actually the same as the
|
|
|
|
# build platform (and therefore it's not really cross-compiling). So we
|
|
|
|
# only set up the cross-compiling config if the target platform is
|
|
|
|
# different.
|
2024-01-08 19:13:48 +00:00
|
|
|
crossSystem.config = buildTargets.${targetSystem}.nixPkgsSystem;
|
2023-11-09 22:52:07 +00:00
|
|
|
}));
|
2023-05-08 16:25:51 +00:00
|
|
|
|
2024-01-08 19:13:48 +00:00
|
|
|
mkToolchain = buildSystem: targetSystem: let
|
|
|
|
buildTarget = buildTargets.${targetSystem};
|
|
|
|
rustTarget = buildTarget.rustTarget;
|
2024-01-09 10:17:52 +00:00
|
|
|
fenixPkgs = fenix.packages.${buildSystem};
|
2024-01-08 19:13:48 +00:00
|
|
|
|
|
|
|
# TODO I'd prefer to use the toolchain file
|
|
|
|
# https://github.com/nix-community/fenix/issues/123
|
|
|
|
fenixToolchain = fenixTarget: (builtins.getAttr "toolchainOf" fenixTarget) {
|
2024-02-27 10:30:28 +00:00
|
|
|
channel = "1.76.0";
|
|
|
|
sha256 = "sha256-e4mlaJehWBymYxJGgnbuCObVlqMlQSilZ8FljG9zPHY=";
|
2024-01-08 19:13:48 +00:00
|
|
|
};
|
|
|
|
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}"
|
2024-01-09 10:17:52 +00:00
|
|
|
] ++ (buildTarget.rustFlags pkgsCross);
|
2024-01-08 19:13:48 +00:00
|
|
|
};
|
|
|
|
|
2023-11-12 09:57:02 +00:00
|
|
|
in {
|
2024-01-08 19:13:48 +00:00
|
|
|
|
2023-11-12 09:57:02 +00:00
|
|
|
packages = eachCrossSystem
|
|
|
|
(builtins.attrNames buildTargets)
|
2024-01-15 16:07:55 +00:00
|
|
|
({ buildSystem, targetSystem, isDefault }: let
|
2023-11-12 09:57:02 +00:00
|
|
|
pkgs = mkPkgs buildSystem null;
|
2024-01-08 19:13:48 +00:00
|
|
|
toolchain = mkToolchain buildSystem targetSystem;
|
2023-11-12 09:57:02 +00:00
|
|
|
naersk-lib = pkgs.callPackage naersk {
|
|
|
|
cargo = toolchain;
|
|
|
|
rustc = toolchain;
|
|
|
|
};
|
|
|
|
in
|
2024-01-08 19:13:48 +00:00
|
|
|
naersk-lib.buildPackage (rec {
|
2023-11-12 09:57:02 +00:00
|
|
|
src = ./.;
|
|
|
|
strictDeps = true;
|
|
|
|
doCheck = false;
|
2024-01-08 19:13:48 +00:00
|
|
|
release = true;
|
2024-01-15 16:07:55 +00:00
|
|
|
postInstall = if isDefault then "" else ''
|
2024-01-09 10:17:52 +00:00
|
|
|
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
|
|
|
|
'';
|
2024-01-08 19:13:48 +00:00
|
|
|
} // (buildEnv buildSystem targetSystem))
|
|
|
|
);
|
2023-11-12 09:57:02 +00:00
|
|
|
|
2024-01-08 19:13:48 +00:00
|
|
|
devShells = eachCrossSystem
|
|
|
|
(builtins.attrNames buildTargets)
|
2024-01-15 16:07:55 +00:00
|
|
|
({ buildSystem, targetSystem, isDefault }: let
|
2024-01-08 19:13:48 +00:00
|
|
|
pkgs = mkPkgs buildSystem null;
|
|
|
|
toolchain = mkToolchain buildSystem targetSystem;
|
|
|
|
in
|
|
|
|
pkgs.mkShell ({
|
2024-03-05 20:00:02 +00:00
|
|
|
buildInputs = [
|
|
|
|
pkgs.nmap
|
|
|
|
pkgs.websocat
|
|
|
|
];
|
2024-01-14 15:43:33 +00:00
|
|
|
shellHook = ''
|
|
|
|
export CARGO_HOME=$(pwd)/.cargo
|
|
|
|
|
|
|
|
if [ -f "config-dev.yml" ]; then
|
|
|
|
export DOMANI_CONFIG_PATH=config-dev.yml
|
|
|
|
fi
|
|
|
|
'';
|
2024-01-08 19:13:48 +00:00
|
|
|
} // (buildEnv buildSystem targetSystem))
|
2023-11-12 09:57:02 +00:00
|
|
|
);
|
2023-11-08 20:40:25 +00:00
|
|
|
};
|
2023-05-03 07:58:06 +00:00
|
|
|
}
|