-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathflake.nix
More file actions
123 lines (119 loc) · 4.87 KB
/
flake.nix
File metadata and controls
123 lines (119 loc) · 4.87 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
{
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
flake-utils.url = "github:numtide/flake-utils";
rust-overlay = {
url = "github:oxalica/rust-overlay";
inputs.nixpkgs.follows = "nixpkgs";
};
crane.url = "github:ipetkov/crane";
treefmt-nix = {
url = "github:numtide/treefmt-nix";
inputs.nixpkgs.follows = "nixpkgs";
};
};
outputs =
{
self,
nixpkgs,
flake-utils,
rust-overlay,
crane,
treefmt-nix,
...
}:
flake-utils.lib.eachDefaultSystem (
system:
let
# Initialize nixpkgs
pkgs = nixpkgs.legacyPackages.${system};
inherit (pkgs) lib;
# Setup the rust toolchain
rust-bin = rust-overlay.lib.mkRustBin { } pkgs;
rust' = (rust-bin.fromRustupToolchainFile ./rust-toolchain.toml);
# Setup rust nix packaging
craneLib = (crane.mkLib pkgs).overrideToolchain (_: rust');
stdenvSelector =
p: if p.stdenv.hostPlatform.isElf then p.stdenvAdapters.useMoldLinker p.stdenv else p.stdenv;
commonArgs = {
src = craneLib.cleanCargoSource ./.;
strictDeps = true;
buildInputs = with pkgs; [ openssl ];
nativeBuildInputs = with pkgs; [ pkg-config ];
# Use mold linker for faster builds on ELF platforms
stdenv = stdenvSelector;
};
cargoArtifacts = craneLib.buildDepsOnly commonArgs;
commonArgsWithDeps = commonArgs // {
inherit cargoArtifacts;
};
cranePackage = craneLib.buildPackage (
commonArgsWithDeps
// {
meta = {
mainProgram = "backend";
license = lib.licenses.gpl3Plus;
};
}
);
# Setup treefmt-nix
treefmtModule = import ./treefmt.nix { inherit rust'; };
treefmtEval = treefmt-nix.lib.evalModule pkgs treefmtModule;
# Construct docker image
dockerImage = pkgs.dockerTools.buildImage {
name = "polyfrost/backend";
tag = null;
copyToRoot = pkgs.buildEnv {
name = "image-root";
paths = [ cranePackage ];
pathsToLink = [ "/bin" ];
};
config = {
Cmd = [ "/bin/${cranePackage.meta.mainProgram}" ];
};
uid = 1000;
gid = 1000;
};
in
{
packages = {
default = self.packages.${system}.backend;
backend = cranePackage;
backend-docker = dockerImage;
};
formatter = treefmtEval.config.build.wrapper;
checks = {
formatting = treefmtEval.config.build.check self;
clippy = craneLib.cargoClippy (
commonArgsWithDeps // { cargoClippyExtraArgs = "--all-targets -- --deny warnings"; }
);
deny = craneLib.cargoDeny (
commonArgs
// {
cargoDenyChecks = "all";
# Used to allow network access so yanked crates and advisories can be downloaded
outputHash = "sha256-pQpattmS9VmO3ZIQUFn66az8GSmB4IvYhTTCFn6SUmo=";
outputHashAlgo = "sha256";
outputHashMode = "recursive";
}
);
};
devShells.default =
craneLib.devShell.override { mkShell = pkgs.mkShell.override { stdenv = stdenvSelector pkgs; }; }
{
# Add all build-time dependencies to the environment
packages =
cranePackage.buildInputs
++ cranePackage.nativeBuildInputs
++ (with pkgs; [
cargo-deny
cargo-udeps
cargo-nextest
evcxr
lldb
self.formatter.${system}
]);
};
}
);
}