-
Notifications
You must be signed in to change notification settings - Fork 0
/
flake.nix
113 lines (100 loc) · 4.49 KB
/
flake.nix
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
{
description = "A simple network benchmarking tool";
nixConfig = {
extra-substituters = [ "https://beni.cachix.org" ];
extra-trusted-public-keys = [ "beni.cachix.org-1:v75ymNNwbn70Eo+x/WFHbpKraAW1DxbzZz7WDCDN3l0=" ];
};
inputs = {
nixpkgs.url = "nixpkgs/nixos-22.11";
crane = {
url = "github:ipetkov/crane";
inputs = {
flake-utils.follows = "flake-utils";
nixpkgs.follows = "nixpkgs";
};
};
fenix = {
url = "github:nix-community/fenix";
inputs.nixpkgs.follows = "nixpkgs";
};
flake-utils.url = "github:numtide/flake-utils";
};
outputs = { self, nixpkgs, crane, fenix, flake-utils, }:
let
mkCrate = localSystem: { pkgs ? import nixpkgs { inherit crossSystem localSystem; }, crossSystem ? localSystem, musl ? false }:
let
abi = if musl then "musl" else "gnu";
target = pkgs.stdenv.hostPlatform.qemuArch + "-unknown-linux-" + abi;
TARGET = with pkgs.lib; with strings; pipe target [ (replaceChars [ "-" ] [ "_" ]) toUpper ]; # UPPERCASE_TARGET_FORMAT
toolchain = with fenix.packages.${localSystem}; combine [ stable.cargo stable.rustc targets.${target}.stable.rust-std ];
craneLib = crane.lib.${localSystem}.overrideToolchain toolchain;
in
with pkgs; craneLib.buildPackage {
src = craneLib.cleanCargoSource ./.;
# depsBuildBuild = [ qemu ]; # unused because it's not cached for aarch64
nativeBuildInputs = [ stdenv.cc pkg-config ];
buildInputs = [ ]; # native dependencies like `openssl` go here
CARGO_BUILD_TARGET = target;
"CARGO_TARGET_${TARGET}_LINKER" = "${stdenv.cc.targetPrefix}cc";
HOST_CC = "${stdenv.cc.nativePrefix}cc";
# run checks on native build
doCheck = localSystem == crossSystem;
};
mkDocker = localSystem: { pkgs ? import nixpkgs { inherit crossSystem localSystem; }, crossSystem ? localSystem, musl ? false, debug ? false }:
let
netbench = mkCrate localSystem { inherit crossSystem musl; };
busybox = pkgs.busybox.override { enableStatic = true; useMusl = true; }; # unbloated "debug env"
in
with pkgs; with dockerTools; buildImage {
name = "netbench";
tag = "latest";
config = {
Cmd = [ "server" ];
Entrypoint = [ "/bin/netbench" ];
};
copyToRoot = buildEnv {
name = "image-root";
paths = [ netbench ] ++ lib.optional debug busybox;
pathsToLink = [ "/bin" ];
};
};
in
flake-utils.lib.eachDefaultSystem
(localSystem:
let
crate = mkCrate localSystem;
docker = mkDocker localSystem;
in
{
packages = {
default = crate { };
musl = crate { musl = true; };
docker = docker { musl = true; };
default-aarch64 = crate { crossSystem = "aarch64-linux"; };
musl-aarch64 = crate { musl = true; crossSystem = "aarch64-linux"; };
docker-aarch64 = docker { musl = true; crossSystem = "aarch64-linux"; };
};
# made for ci, for pushing the images to ghcr.io
apps.dockerTag = {
type = "app";
program = with import nixpkgs { inherit localSystem; };
let
version = with builtins; (fromTOML (readFile ./Cargo.toml)).package.version;
sk = "${skopeo}/bin/skopeo";
d = "${pkgs.docker}/bin/docker";
in
toString (writeShellScript "dockerTag.sh" ''
nix build .#docker
${sk} copy docker-archive:./result docker://ghcr.io/beni69/netbench:${version}-x86_64
nix build .#docker-aarch64
${sk} copy docker-archive:./result docker://ghcr.io/beni69/netbench:${version}-aarch64
${d} manifest create ghcr.io/beni69/netbench:v${version} ghcr.io/beni69/netbench:${version}-x86_64 ghcr.io/beni69/netbench:${version}-aarch64
${d} manifest push --purge ghcr.io/beni69/netbench:v${version}
${d} manifest create ghcr.io/beni69/netbench:latest ghcr.io/beni69/netbench:${version}-x86_64 ghcr.io/beni69/netbench:${version}-aarch64
${d} manifest push --purge ghcr.io/beni69/netbench:latest
'');
};
}) // {
overlays.default = final: pkgs: { beni.netbench = mkCrate pkgs.system { inherit pkgs; }; };
};
}