forked from cargo2nix/cargo2nix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
flake.nix
162 lines (150 loc) · 8.16 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
{
inputs = {
nixpkgs.url = "github:nixos/nixpkgs?ref=release-22.05";
rust-overlay = {
url = "github:oxalica/rust-overlay";
inputs.nixpkgs.follows = "nixpkgs";
inputs.flake-utils.follows = "flake-utils";
};
flake-utils.url = "github:numtide/flake-utils";
flake-compat = {
url = github:edolstra/flake-compat;
flake = false;
};
};
outputs = inputs: with inputs;
let
overlays = import ./overlay (rust-overlay.overlay);
combinedOverlay = overlays.combined;
in flake-utils.lib.eachDefaultSystem (system:
let
# 1. Setup nixpkgs with rust and cargo2nix overlays.
pkgs = import nixpkgs {
inherit system;
overlays = [
combinedOverlay
];
# set `crossSystem` (see examples/3-cross-compiling) for configuring cross
};
# 2. Builds the rust package set, which contains all crates in your cargo workspace's dependency graph.
# `makePackageSet` accepts the following arguments:
# - `packageFun` (required): The generated `Cargo.nix` file, which returns the whole dependency graph.
# - `workspaceSrc` (optional): Sources for the workspace can be provided or default to the current directory.
# You must set some combination of `rustChannel` + `rustVersion` or `rustToolchain`.
# - `rustToolchain` (optional): Completely override the toolchain. Must provide rustc, cargo, rust-std, and rust-src components
# - `rustChannel` (optional): "nightly" "stable" "beta". To support legacy use, this can be a version when supplied alone. If unspecified, defaults to "stable".
# - `rustVersion` (optional): "1.60.0" "2020-12-30". If not supplied, "latest" will be assumed.
# - `rustProfile` (optional): "minimal" or "default" usually. "minimal" if not specified (for faster builds)
# - `extraRustComponents` (optional): ["rustfmt" "clippy"].
# - `packageOverrides` (optional):
# A function taking a package set and returning a list of overrides.
# Overrides are introduced to provide native inputs to build the crates generated in `Cargo.nix`.
# See `overlay/lib/overrides.nix` on how to create overrides and `overlay/overrides.nix` for a list of predefined overrides.
# Most of the time, you can just use `overrides.all`. You can hand-pick overrides later if your build becomes too slow.
# - `rootFeatures` (optional):
# A list of activated features on your workspace's crates.
# Each feature should be of the form `<crate_name>[/<feature>]`.
# If `/<feature>` is omitted, the crate is activated with no default features.
# The default behavior is to activate all crates with default features.
# - `fetchCrateAlternativeRegistry` (optional): A fetcher for crates on alternative registries.
# - `release` (optional): Whether to enable release mode (equivalent to `cargo build --release`), defaults to `true`.
# - `hostPlatformCpu` (optional):
# Equivalent to rust's target-cpu codegen option. If specified "-Ctarget-cpu=<value>" will be added to the set of rust
# flags used for compilation of the package set.
# - `hostPlatformFeatures` (optional):
# Equivalent to rust's target-feature codegen option. If specified "-Ctarget-feature=<values>" will be added to the set of rust
# flags used for compilation of the package set. The value should be a list of the features to be turned on, without the leading "+",
# e.g. `[ "aes" "sse2" "ssse3" "sse4.1" ]`. They will be prefixed with a "+", and comma delimited before passing through to rust.
# Crates that check for CPU features such as the `aes` crate will be evaluated against this argument.
# rustcLinkFlags (optional):
# Pass extra flags directly to rustc during non-build invocations
# rustcBuildFlags (optional):
# Pass extra flags directly to Rustc during build invocations
# - `target` (optional):
# Set an explicit Rust output target. Overrides the translation
# from Nix targets to Rust targets. See overlay/lib/rust-triple.nix
# for more info.
rustPkgs = pkgs.rustBuilder.makePackageSet {
packageFun = import ./Cargo.nix;
rustVersion = "1.61.0";
packageOverrides = pkgs: pkgs.rustBuilder.overrides.all;
};
# `rustPkgs` now contains all crates in the dependency graph.
# To build normal binaries, use `rustPkgs.<registry>.<crate>.<version> { }`.
# To build test binaries (equivalent to `cargo build --tests`), use
# `rustPkgs.<registry>.<crate>.<version>{ compileMode = "test"; }`.
# To build bench binaries (equivalent to `cargo build --benches`), use
# `rustPkgs.<registry>.<crate>.<version>{ compileMode = "bench"; }`.
# For convenience, you can also refer to the crates in the workspace using
# `rustPkgs.workspace.<crate>`.
#
# When a crate is not associated with any registry, such as when building
# locally, the registry is "unknown" as shown below:
# rustPkgs.unknown.cargo2nix."0.11.0"
# An example of a crates.io path:
# rustPkgs."registry+https://github.com/rust-lang/crates.io-index".openssl."0.10.30"
cargo2nixBin = (rustPkgs.workspace.cargo2nix {}).bin; # supports override & overrideAttrs
# The workspace defines a development shell with all of the dependencies
# and environment settings necessary for a regular `cargo build`.
# Passes through all arguments to pkgs.mkShell for adding supplemental
# dependencies.
workspaceShell = (rustPkgs.workspaceShell {
# packages = [ pkgs.somethingExtra ];
# shellHook = ''
# export PS1="\033[0;31m☠dev-shell☠ $ \033[0m"
# '';
}); # supports override & overrideAttrs
# A shell for users to quickly bootstrap projects. Contains cargo2nix
# and the rustToolchain used to build this cargo2nix.
bootstrapShell = pkgs.mkShell {
packages = [ cargo2nixBin ];
# inputsFrom = [ cargo2nixBin ];
nativeBuildInputs = cargo2nixBin.nativeBuildInputs;
};
in rec {
devShells = {
# nix develop
default = workspaceShell;
# nix develop .#bootstrap
bootstrap = bootstrapShell;
};
packages = rec {
# nix build .#packages.x86_64-linux.cargo2nix
# nix build .#cargo2nix
cargo2nix = cargo2nixBin;
# nix build
default = cargo2nix;
# `runTests` runs all tests for a crate inside a Nix derivation. This
# may be problematic as Nix may restrict filesystem, network access,
# socket creation, which the test binary may need.
# If you run to those problems, build test binaries (as shown above in
# workspace derivation arguments) and run them manually outside a Nix
# derivation.s
ci = pkgs.rustBuilder.runTests rustPkgs.workspace.cargo2nix {
/* Add `depsBuildBuild` test-only deps here, if any. */
};
# for legacy users
shell = devShells.default;
};
apps = rec {
# nix run .#cargo2nix
# nix run github:cargo2nix/cargo2nix
cargo2nix = { type = "app"; program = "${packages.default}/bin/cargo2nix"; };
# nix run
# nix run github:cargo2nix/cargo2nix
default = cargo2nix;
};
}
) // {
# The above outputs are mapped over system for `nix run` and `nix develop`
# workflows. They are merged with these system-independent attributes,
# which are top level attributes can be used directly in downstream
# flakes. If `cargo2nix` is your flake input, `cargo2nix.overlay` is the
# overlay.
inherit overlays;
# Nix flake check complains. I will keep this attribute alive until next
# version branch-off.
overlay = builtins.trace
"cargo2nix.overlay is deprecated. Use cargo2nix.overlays.default" overlays.default;
};
}