-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflake.nix
136 lines (121 loc) · 3.96 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
{
description = "A Nix-flake-based Rust development environment";
inputs = {
advisory-db = {
url = "github:rustsec/advisory-db";
flake = false;
};
crane = {
url = "github:ipetkov/crane";
};
flake-utils.url = "github:numtide/flake-utils";
nixpkgs.url = "github:nixos/nixpkgs";
rust-overlay = {
url = "github:oxalica/rust-overlay";
inputs = {
nixpkgs.follows = "nixpkgs";
};
};
};
outputs = {self, ...} @ inputs:
inputs.flake-utils.lib.eachDefaultSystem (system: let
pkgs = import inputs.nixpkgs {
inherit system;
overlays = [(import inputs.rust-overlay)];
};
# Target musl when building on 64-bit linux
buildTarget =
{"x86_64-linux" = "x86_64-unknown-linux-musl";}.${system}
or (pkgs.rust.toRustTargetSpec pkgs.stdenv.hostPlatform);
rustToolchain = pkgs.rust-bin.stable.latest.default.override {
targets = [
buildTarget
(pkgs.rust.toRustTargetSpec pkgs.stdenv.hostPlatform)
];
};
# Set-up build dependencies and configure rust
craneLib = (inputs.crane.mkLib pkgs).overrideToolchain rustToolchain;
cargo-details = pkgs.lib.importTOML ./Cargo.toml;
pname = cargo-details.package.name;
src = craneLib.cleanCargoSource (craneLib.path ./.);
commonArgs = {
inherit src pname;
nativeBuildInputs = with pkgs; [pkg-config];
CARGO_BUILD_TARGET = buildTarget;
};
# Compile and cache only cargo dependencies
cargoArtifacts = craneLib.buildDepsOnly commonArgs;
# Compile and cache only workspace code (seperately from 3rc party dependencies)
cargo-package = craneLib.buildPackage (commonArgs // {inherit cargoArtifacts;});
in {
checks = {
inherit
# Build the crate as part of `nix flake check` for convenience
cargo-package
;
# Run clippy (and deny all warnings) on the crate source,
# again, resuing the dependency artifacts from above.
#
# Note that this is done as a separate derivation so that
# we can block the CI if there are issues here, but not
# prevent downstream consumers from building our crate by itself.
cargo-clippy = craneLib.cargoClippy (commonArgs
// {
inherit cargoArtifacts;
cargoClippyExtraArgs = "--all-targets -- --deny warnings";
});
# Generate rust-generated documentation
cargo-doc = craneLib.cargoDoc (commonArgs
// {
inherit cargoArtifacts;
cargoDocExtraArgs = ""; # TODO: Figure out why seems to be required
});
# Check formatting
cargo-fmt = craneLib.cargoFmt {inherit src;};
# Run tests with cargo-nextest
cargo-nextest =
craneLib.cargoNextest (commonArgs
// {inherit cargoArtifacts;});
# Audit dependencies
cargo-audit = craneLib.cargoAudit {
inherit src;
inherit (inputs) advisory-db;
};
};
devShells.default = pkgs.mkShell {
packages =
(with pkgs; [
cargo-audit
cargo-auditable
cargo-cross
cargo-deny
cargo-nextest
cargo-outdated
cargo-watch
rust-analyzer
# Editor stuffs
lldb
rust-analyzer
])
++ [
# Packages made in this flake
rustToolchain
# cargo-package
];
};
packages = {
rust = cargo-package;
docker = pkgs.dockerTools.buildImage {
name = pname;
tag = "v${cargo-details.package.version}";
config = {
Cmd = "--help";
Entrypoint = ["${cargo-package}/bin/${pname}"];
};
};
};
packages.default = cargo-package;
# Now `nix fmt` works!
formatter = pkgs.nixfmt-rfc-style;
});
}