Repository navigation
Expand file tree
/
Copy pathflake.nix
More file actions
174 lines (171 loc) · 5.56 KB
/
Copy pathflake.nix
File metadata and controls
174 lines (171 loc) · 5.56 KB
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
{
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
crane.url = "github:ipetkov/crane";
rust-overlay = {
url = "github:oxalica/rust-overlay";
inputs.nixpkgs.follows = "nixpkgs";
};
};
outputs =
{
nixpkgs,
crane,
rust-overlay,
...
}:
let
supportedSystems = [
"x86_64-linux"
"x86_64-darwin"
"aarch64-linux"
"aarch64-darwin"
];
overlays = [ (import rust-overlay) ];
eachSystem = nixpkgs.lib.genAttrs supportedSystems;
rustTargetFor =
pkgs:
if pkgs.stdenv.hostPlatform.isWindows then
"x86_64-pc-windows-gnu"
else
{
x86_64-linux = "x86_64-unknown-linux-musl";
aarch64-linux = "aarch64-unknown-linux-musl";
}
.${pkgs.stdenv.hostPlatform.system} or null;
toolchainFor =
rustTarget: pkgs:
pkgs.rust-bin.nightly.latest.minimal.override (
{
extensions = [
"clippy"
"rust-src"
];
}
// pkgs.lib.optionalAttrs (rustTarget != null) {
targets = [ rustTarget ];
}
);
in
{
packages = eachSystem (
system:
let
mkPackage =
{ crossSystem ? null }:
let
pkgs = import nixpkgs (
{
localSystem = system;
inherit overlays;
}
// nixpkgs.lib.optionalAttrs (crossSystem != null) {
inherit crossSystem;
}
);
rustTarget = rustTargetFor pkgs;
craneLib = (crane.mkLib pkgs).overrideToolchain (toolchainFor rustTarget);
src = pkgs.lib.cleanSourceWith {
src = ./.;
filter =
path: _:
let
relative = pkgs.lib.removePrefix "${toString ./.}/" (toString path);
in
relative == "Cargo.toml"
|| relative == "Cargo.lock"
|| relative == "build.rs"
|| relative == "proto"
|| pkgs.lib.hasPrefix "proto/" relative
|| relative == "src"
|| pkgs.lib.hasPrefix "src/" relative;
};
craneArgs = {
inherit src;
cargoTomlContents = builtins.readFile ./Cargo.toml;
cargoLockContents = builtins.readFile ./Cargo.lock;
strictDeps = true;
SSL_CERT_FILE = "${pkgs.cacert}/etc/ssl/certs/ca-bundle.crt";
}
// pkgs.lib.optionalAttrs (rustTarget != null) {
CARGO_BUILD_TARGET = rustTarget;
}
// pkgs.lib.optionalAttrs (
pkgs.stdenv.hostPlatform.isLinux || pkgs.stdenv.hostPlatform.isWindows
) {
CARGO_BUILD_RUSTFLAGS = "-C target-feature=+crt-static";
}
// pkgs.lib.optionalAttrs pkgs.stdenv.hostPlatform.isWindows {
doCheck = false;
}
// pkgs.lib.optionalAttrs pkgs.stdenv.hostPlatform.isDarwin {
RUSTFLAGS = "-C link-arg=-Wl,-dead_strip_dylibs";
};
cargoArtifacts = craneLib.buildDepsOnly craneArgs;
in
craneLib.buildPackage (
craneArgs
// {
inherit cargoArtifacts;
meta.mainProgram = if pkgs.stdenv.hostPlatform.isWindows then "brainpod.exe" else "brainpod";
}
// pkgs.lib.optionalAttrs pkgs.stdenv.hostPlatform.isLinux {
nativeBuildInputs = [ pkgs.buildPackages.binutils ];
postFixup = ''
if readelf -l "$out/bin/brainpod" | grep -F INTERP; then
echo "brainpod contains a dynamic ELF interpreter" >&2
exit 1
fi
if readelf -d "$out/bin/brainpod" | grep -F '(NEEDED)'; then
echo "brainpod contains a dynamic library dependency" >&2
exit 1
fi
'';
}
// pkgs.lib.optionalAttrs pkgs.stdenv.hostPlatform.isDarwin {
postFixup = ''
if otool -L "$out/bin/brainpod" | tail -n +2 | grep -F /nix/store/; then
echo "brainpod contains a dynamic Nix store dependency" >&2
exit 1
fi
'';
}
);
in
{
default = mkPackage {
crossSystem =
{
x86_64-linux.config = "x86_64-unknown-linux-musl";
aarch64-linux.config = "aarch64-unknown-linux-musl";
}
.${system} or null;
};
}
// nixpkgs.lib.optionalAttrs (system == "x86_64-linux") {
windows = mkPackage {
crossSystem = {
config = "x86_64-w64-mingw32";
libc = "msvcrt";
};
};
}
);
devShells = eachSystem (
system:
let
pkgs = import nixpkgs { inherit system overlays; };
rustTarget = rustTargetFor pkgs;
craneLib = (crane.mkLib pkgs).overrideToolchain (toolchainFor rustTarget);
in
{
default = craneLib.devShell {
packages = with pkgs; [
rustfmt
rust-analyzer
];
};
}
);
};
}