Repository navigation
Expand file tree
/
Copy pathflake.nix
More file actions
179 lines (161 loc) · 6.23 KB
/
Copy pathflake.nix
File metadata and controls
179 lines (161 loc) · 6.23 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
175
176
177
178
179
{
description = "Rust dev environment";
inputs = {
nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable";
nixpkgs-intel-darwin.url = "github:NixOS/nixpkgs/nixpkgs-26.05-darwin";
rust-overlay.url = "github:oxalica/rust-overlay";
flake-utils.url = "github:numtide/flake-utils";
};
outputs =
{
self,
nixpkgs,
nixpkgs-intel-darwin,
rust-overlay,
flake-utils,
...
}:
flake-utils.lib.eachDefaultSystem (
system:
let
overlays = [ (import rust-overlay) ];
# 26.05 is the last supported nixpkgs release for Intel macOS.
nixpkgsForSystem = if system == "x86_64-darwin" then nixpkgs-intel-darwin else nixpkgs;
pkgs = import nixpkgsForSystem { inherit system overlays; };
rustToolchainConfig = builtins.fromTOML (builtins.readFile ./rust-toolchain.toml);
rustToolchainChannel = rustToolchainConfig.toolchain.channel;
rustToolchain = pkgs.rust-bin.fromRustupToolchainFile ./rust-toolchain.toml;
rustPlatform = pkgs.makeRustPlatform {
cargo = rustToolchain;
rustc = rustToolchain;
};
dylintVersion = "6.4.0";
dylintSource = pkgs.fetchFromGitHub {
owner = "trailofbits";
repo = "dylint";
rev = "v${dylintVersion}";
hash = "sha256-RLalWaoObvaAKleawusmTb+3O/ZHEll2az7NZao2sDE=";
};
dylintCargoHash = "sha256-zEfi1H/AX0dnoZByHUQtsiGiWse8TOnjeifToCtQtNo=";
cargoDylint = rustPlatform.buildRustPackage {
pname = "cargo-dylint";
version = dylintVersion;
src = dylintSource;
cargoHash = dylintCargoHash;
nativeBuildInputs = [ pkgs.pkg-config ];
# nixpkgs sets LIBGIT2_NO_VENDOR, so libgit2-sys needs the system library.
buildInputs = [
pkgs.openssl
pkgs.libgit2
];
buildAndTestSubdir = "cargo-dylint";
# Use the exact published driver, not the temporary Nix build source path.
buildFeatures = [ "dylint/__driver_from_crates_io" ];
doCheck = false;
};
dylintLink = rustPlatform.buildRustPackage {
pname = "dylint-link";
version = dylintVersion;
src = dylintSource;
cargoHash = dylintCargoHash;
nativeBuildInputs = [ pkgs.pkg-config ];
buildInputs = [ pkgs.openssl ];
buildAndTestSubdir = "dylint-link";
doCheck = false;
};
# Dylint asks `rustup +stable which cargo` while resolving lint libraries.
# The dev shell's pinned toolchain is newer than the user's rustup `stable`,
# so route that lookup back to the pinned Cargo and delegate all other
# rustup operations normally.
dylintRustup = pkgs.writeShellScriptBin "rustup" ''
if [ "$#" -eq 3 ] && [ "$1" = "+stable" ] && [ "$2" = "which" ] && [ "$3" = "cargo" ]; then
echo "${rustToolchain}/bin/cargo"
else
exec ${pkgs.rustup}/bin/rustup "$@"
fi
'';
# Keep this checkout on the pinned Nix toolchain. Nested projects that explicitly
# request another channel still delegate to the user's rustup installation.
dylintCargo = pkgs.writeShellScriptBin "cargo" ''
find_toolchain_dir() {
dir="$PWD"
while [ "$dir" != "/" ]; do
if [ -f "$dir/rust-toolchain.toml" ] || [ -f "$dir/rust-toolchain" ]; then
printf '%s\n' "$dir"
return
fi
dir="$(${pkgs.coreutils}/bin/dirname "$dir")"
done
}
toolchain="''${RUSTUP_TOOLCHAIN:-}"
if [ -z "$toolchain" ]; then
toolchain_dir="$(find_toolchain_dir || true)"
if [ -n "$toolchain_dir" ]; then
toolchain_file="$toolchain_dir/rust-toolchain"
if [ -f "$toolchain_dir/rust-toolchain.toml" ]; then
toolchain_file="$toolchain_dir/rust-toolchain.toml"
fi
toolchain="$(${pkgs.gnused}/bin/sed -n 's/^[[:space:]]*channel[[:space:]]*=[[:space:]]*"\([^"]*\)".*/\1/p' "$toolchain_file" | ${pkgs.coreutils}/bin/head -n 1)"
if [ -z "$toolchain" ]; then
toolchain="$(${pkgs.coreutils}/bin/head -n 1 "$toolchain_file")"
fi
fi
fi
if [ -n "$toolchain" ] && [ "$toolchain" != "${rustToolchainChannel}" ]; then
cargo_path="$(${pkgs.rustup}/bin/rustup which --toolchain "$toolchain" cargo)"
toolchain_bin="$(${pkgs.coreutils}/bin/dirname "$cargo_path")"
toolchain_root="$(${pkgs.coreutils}/bin/dirname "$toolchain_bin")"
toolchain_name="$(${pkgs.coreutils}/bin/basename "$toolchain_root")"
exec ${pkgs.coreutils}/bin/env \
PATH="$toolchain_bin:$PATH" \
RUSTC="$toolchain_bin/rustc" \
RUSTDOC="$toolchain_bin/rustdoc" \
RUSTUP_TOOLCHAIN="$toolchain_name" \
"$cargo_path" "$@"
fi
exec ${rustToolchain}/bin/cargo "$@"
'';
simul = rustPlatform.buildRustPackage {
pname = "simul";
version = (builtins.fromTOML (builtins.readFile ./Cargo.toml)).package.version;
src = ./.;
cargoLock = {
lockFile = ./Cargo.lock;
};
nativeBuildInputs = [ ];
buildInputs = [ ];
};
in
{
packages.default = simul;
apps.default = flake-utils.lib.mkApp { drv = simul; };
devShells.default = pkgs.mkShell {
packages = with pkgs; [
dylintCargo
rustToolchain
openssl
zlib
pkg-config
dylintRustup
git
llvmPackages.bolt
# Cargo checks / lints / tools
cargoDylint
dylintLink
cargo-audit
cargo-deny
cargo-edit
cargo-license
cargo-pgo
cargo-udeps
cargo-watch
just
];
shellHook = ''
# Tells rust-analyzer where the stdlib sources are
export RUST_SRC_PATH=${rustToolchain}/lib/rustlib/src/rust/library
'';
};
}
);
}