-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
flake.nix
193 lines (164 loc) · 5.56 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
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
{
description = "A compression multi-tool for the command line.";
inputs = {
nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable";
crane = {
url = "github:ipetkov/crane";
inputs.nixpkgs.follows = "nixpkgs";
};
fenix = {
# Needed because rust-overlay, normally used by crane, doesn't have llvm-tools for coverage
url = "github:nix-community/fenix";
inputs.nixpkgs.follows = "nixpkgs";
inputs.rust-analyzer-src.follows = "";
};
advisory-db = {
# Rust dependency security advisories
url = "github:rustsec/advisory-db";
flake = false;
};
# Flake helper for better organization with modules.
flake-parts = {
url = "github:hercules-ci/flake-parts";
inputs.nixpkgs-lib.follows = "nixpkgs";
};
# For creating a universal `nix fmt`
treefmt-nix = {
url = "github:numtide/treefmt-nix";
inputs.nixpkgs.follows = "nixpkgs";
};
};
outputs = inputs @ {
self,
flake-parts,
...
}:
flake-parts.lib.mkFlake {inherit inputs;} {
systems = [
"aarch64-darwin"
"aarch64-linux"
"x86_64-darwin"
"x86_64-linux"
];
imports = [
flake-parts.flakeModules.easyOverlay
inputs.treefmt-nix.flakeModule
];
perSystem = {
config,
system,
pkgs,
...
}: let
# Use the stable rust tools from fenix
fenixStable = inputs.fenix.packages.${system}.stable;
rustSrc = fenixStable.rust-src;
toolChain = fenixStable.completeToolchain;
# Use the toolchain with the crane helper functions
craneLib = (inputs.crane.mkLib pkgs).overrideToolchain toolChain;
# Common arguments for mkCargoDerivation, a helper for the crane functions
# Arguments can be included here even if they aren't used, but we only
# place them here if they would otherwise show up in multiple places
commonArgs = {
inherit cargoArtifacts;
# Clean the src to only have the Rust-relevant files
src = craneLib.cleanCargoSource ./.;
strictDeps = true;
};
# Build only the cargo dependencies so we can cache them all when running in CI
cargoArtifacts = craneLib.buildDepsOnly commonArgs;
# Build the actual crate itself, reusing the cargoArtifacts
cmprss = craneLib.buildPackage (commonArgs
// {
doCheck = false; # Tests are run as a separate build with nextest
meta.mainProgram = "cmprss";
});
in {
packages = {
default = cmprss;
cmprss = cmprss;
# Check code coverage with tarpaulin
coverage = craneLib.cargoTarpaulin (commonArgs
// {
# Use lcov output as thats far more widely supported
cargoTarpaulinExtraArgs = "--skip-clean --include-tests --output-dir $out --out lcov";
});
# Run clippy (and deny all warnings) on the crate source
clippy = craneLib.cargoClippy (commonArgs
// {
cargoClippyExtraArgs = "--all-targets -- --deny warnings";
});
# Check docs build successfully
doc = craneLib.cargoDoc commonArgs;
# Check formatting
fmt = craneLib.cargoFmt commonArgs;
# Run tests with cargo-nextest
test = craneLib.cargoNextest commonArgs;
# Audit dependencies
# This only runs when Cargo.lock files change
audit = craneLib.cargoAudit (commonArgs
// {
inherit (inputs) advisory-db;
});
};
checks = {
inherit cmprss;
# Build almost every package in checks, with exceptions:
# - coverage: It requires a full rebuild, and only needs to be run occasionally
inherit (self.packages.${system}) clippy doc fmt test audit;
};
# This also sets up `nix fmt` to run all formatters
treefmt = {
projectRootFile = ./flake.nix;
programs = {
alejandra.enable = true;
prettier.enable = true;
rustfmt = {
enable = true;
package = toolChain;
};
};
};
apps = rec {
default = cmprss;
cmprss.program = self.packages.${system}.cmprss;
};
overlayAttrs = {
inherit (config.packages) cmprss;
};
devShells.default = pkgs.mkShell {
name = "cmprss";
shellHook = ''
echo ---------------------
task --list
echo ---------------------
'';
# Include the packages from the defined checks and packages
# Installs the full cargo toolchain and the extra tools, e.g. cargo-tarpaulin.
inputsFrom =
(builtins.attrValues self.checks.${system})
++ (builtins.attrValues self.packages.${system});
# Extra inputs can be added here
packages = with pkgs; [
act # For running Github Actions locally
alejandra
deadnix
git-cliff
go-task
gum # Pretty printing in scripts
nodePackages.prettier
statix
# For running tests
diffutils
# Official tools
bzip2
gnutar
gzip
xz
];
# Many tools read this to find the sources for rust stdlib
RUST_SRC_PATH = "${rustSrc}/lib/rustlib/src/rust/library";
};
};
};
}