-
Notifications
You must be signed in to change notification settings - Fork 2
/
flake.nix
155 lines (143 loc) · 4.59 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
{
description = "A Nix-flake-based Rust development environment";
inputs = {
crane = {
url = "github:ipetkov/crane";
inputs = {
nixpkgs.follows = "nixpkgs";
flake-utils.follows = "flake-utils";
};
};
flake-utils.url = "github:numtide/flake-utils";
nixpkgs.url = "github:NixOS/nixpkgs";
rust-overlay = {
url = "github:oxalica/rust-overlay";
inputs = {
nixpkgs.follows = "nixpkgs";
flake-utils.follows = "flake-utils";
};
};
};
outputs = {
self,
crane,
flake-utils,
nixpkgs,
rust-overlay,
}:
flake-utils.lib.eachDefaultSystem (system: let
pkgs = import nixpkgs {
inherit system;
overlays = [(import 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.hostPlatorm)
# ];
# };
rustToolchain = pkgs.rust-bin.stable.latest.default;
# Set-up build dependencies and configure rust
craneLib = (crane.mkLib pkgs).overrideToolchain rustToolchain;
inherit (pkgs) lib;
# Shamelessly stolen from:
# https://github.com/fedimint/fedimint/blob/66519d5e978e22bb10a045f406101891e1bb7eb5/flake.nix#L99
filterSrcWithRegexes = regexes: src: let
basePath = toString src + "/";
in
lib.cleanSourceWith {
filter = path: type: let
relPath = lib.removePrefix basePath (toString path);
includePath =
(type == "directory")
|| lib.any (re: builtins.match re relPath != null) regexes;
# uncomment to debug:
# builtins.trace "${relPath}: ${lib.boolToString includePath}"
in
includePath;
inherit src;
};
cargo-details = lib.importTOML ./Cargo.toml;
pname = cargo-details.package.name;
commonArgs = {
nativeBuildInputs = with pkgs;
[glib openssl pkg-config]
++ lib.optionals pkgs.stdenv.isDarwin [
pkgs.libiconv
pkgs.darwin.apple_sdk.frameworks.Security
pkgs.darwin.apple_sdk.frameworks.SystemConfiguration
];
# CARGO_BUILD_TARGET = buildTarget;
};
# Compile and cache only cargo dependencies
dep-files-filter = ["Cargo.lock" "Cargo.toml" ".*/Cargo.toml"];
cargo-deps = craneLib.buildDepsOnly (commonArgs
// {
src = filterSrcWithRegexes dep-files-filter ./.;
inherit pname;
});
# Compile and cache only workspace code (seperately from 3rc party dependencies)
package-file-filter = dep-files-filter ++ [".*.rs"];
cargo-package = craneLib.buildPackage (commonArgs
// {
inherit cargo-deps pname;
src = filterSrcWithRegexes package-file-filter ./.;
});
in {
checks = {
# inherit # Comment in when you want tests to run on every new shell
# cargo-package
# ;
};
devShells.default = pkgs.mkShell {
packages =
(with pkgs; [
# rust specific
cargo-audit
cargo-auditable
cargo-cross
cargo-deny
cargo-outdated
rust-analyzer
# Editor stuffs
helix
lldb
rust-analyzer
# Nix stuff
nix-output-monitor
])
++ commonArgs.nativeBuildInputs
++ [
# Packages made in this flake
rustToolchain
# cargo-package # Comment in when you want tests to run on every new shell
]
++ lib.optionals (pkgs.stdenv.isLinux)
(with pkgs; [cargo-watch]); # Currently broken on macOS
shellHook = ''
${rustToolchain}/bin/cargo --version
${pkgs.helix}/bin/hx --version
${pkgs.helix}/bin/hx --health rust
'';
};
packages = {
rust = cargo-package;
docker = pkgs.dockerTools.buildImage {
name = pname;
tag = "v${cargo-details.package.version}";
extraCommands = "mkdir -p data";
config = {
Cmd = "--help";
Entrypoint = ["${cargo-package}/bin/${pname}"];
};
};
};
packages.default = cargo-package;
# Now `nix fmt` works!
formatter = pkgs.alejandra;
});
}