-
Notifications
You must be signed in to change notification settings - Fork 0
/
flake.nix
115 lines (111 loc) · 3.5 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
114
115
{
inputs = {
fenix.url = "github:nix-community/fenix";
flake-utils.url = "github:numtide/flake-utils";
naersk.url = "github:nix-community/naersk";
nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
};
outputs =
{
self,
fenix,
flake-utils,
naersk,
nixpkgs,
}:
flake-utils.lib.eachDefaultSystem (
system:
let
pkgs = (import nixpkgs) { inherit system; };
makePackage =
rustTarget:
args@{
ccPackage,
nativeBuildInputs ? [ ],
depsBuildBuild ? [ ],
...
}:
let
toolchain =
let
fenixPkgs = fenix.packages.${system};
fenixToolchain =
fenixTarget:
(builtins.getAttr "toolchainOf" fenixTarget) {
channel = "1.80.0";
sha256 = "sha256-6eN/GKzjVSjEhGO9FhWObkRFaE1Jf+uqMSdQnb8lcB4=";
};
in
fenixPkgs.combine [
(fenixToolchain fenixPkgs).rustc
(fenixToolchain fenixPkgs).rustfmt
(fenixToolchain fenixPkgs).cargo
(fenixToolchain fenixPkgs).clippy
(fenixToolchain (fenixPkgs.targets).${rustTarget}).rust-std
];
naersk' = naersk.lib.${system}.override {
cargo = toolchain;
rustc = toolchain;
};
in
naersk'.buildPackage {
src = ./.;
strictDeps = true;
doCheck = false;
release = true;
nativeBuildInputs = nativeBuildInputs ++ [ ccPackage ];
depsBuildBuild = depsBuildBuild ++ [ ccPackage ];
TARGET_CC = "${ccPackage}/bin/${ccPackage.targetPrefix}cc";
CARGO_BUILD_TARGET = rustTarget;
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-${rustTarget}$ext"
else
mv "$f" "$f-${rustTarget}"
fi
done
'';
}
// args;
targets = {
x86_64-unknown-linux-musl = {
ccPackage = pkgs.pkgsStatic.stdenv.cc;
CARGO_BUILD_RUSTFLAGS = [
"-C"
"target-feature=+crt-static"
"-C"
"link-args=-static -latomic"
];
};
x86_64-pc-windows-gnu = {
ccPackage = pkgs.pkgsCross.mingwW64.stdenv.cc;
CARGO_TARGET_X86_64_PC_WINDOWS_GNU_RUSTFLAGS = "-L native=${pkgs.pkgsCross.mingwW64.windows.pthreads}/lib";
depsBuildBuild = with pkgs; [
pkgsCross.mingwW64.windows.pthreads
];
};
};
in
rec {
packages = (
nixpkgs.lib.mapAttrs (name: value: (makePackage name value)) targets
// {
release = pkgs.runCommand "release-binaries" { } ''
mkdir -p "$out"/bin
for pkg in ${
builtins.concatStringsSep " " (
map (p: "${p}/bin") (builtins.attrValues (builtins.removeAttrs packages [ "release" ]))
)
}; do
cp -r "$pkg"/* "$out"/bin/
done
(cd "$out"/bin && sha256sum * > sha256.txt)
'';
}
);
}
);
}