-
Notifications
You must be signed in to change notification settings - Fork 0
/
flake.nix
222 lines (191 loc) · 7 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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# nix build .#myServer
# nix build .#myClient
# nix build .#FEdockerImage
# nix build .#BEdockerImage
#
{
description = "Build a cargo project";
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
# The version of wasm-bindgen-cli needs to match the version in Cargo.lock
# Update this to include the version you need
nixpkgs-for-wasm-bindgen.url = "github:NixOS/nixpkgs/4e6868b1aa3766ab1de169922bb3826143941973";
crane = {
url = "github:ipetkov/crane";
inputs.nixpkgs.follows = "nixpkgs";
};
flake-utils.url = "github:numtide/flake-utils";
rust-overlay = {
url = "github:oxalica/rust-overlay";
inputs = {
nixpkgs.follows = "nixpkgs";
flake-utils.follows = "flake-utils";
};
};
};
outputs = { self, nixpkgs, crane, flake-utils, rust-overlay, nixpkgs-for-wasm-bindgen, ... }:
flake-utils.lib.eachDefaultSystem (system:
let
pkgs = import nixpkgs {
inherit system;
overlays = [ (import rust-overlay) ];
};
inherit (pkgs) lib;
rustToolchain = pkgs.pkgsBuildHost.rust-bin.fromRustupToolchainFile ./rust-toolchain.toml;
craneLib = ((crane.mkLib pkgs).overrideToolchain rustToolchain).overrideScope (_final: _prev: {
# The version of wasm-bindgen-cli needs to match the version in Cargo.lock. You
# can unpin this if your nixpkgs commit contains the appropriate wasm-bindgen-cli version
inherit (import nixpkgs-for-wasm-bindgen { inherit system; }) wasm-bindgen-cli;
});
# When filtering sources, we want to allow assets other than .rs files
src = lib.cleanSourceWith {
src = ./.; # The original, unfiltered source
filter = path: type:
(lib.hasSuffix "\.html" path) ||
(lib.hasSuffix "\.scss" path) ||
(lib.hasSuffix "favicon.ico" path) ||
# Example of a folder for images, icons, etc
(lib.hasInfix "/assets/" path) ||
# Default filter from crane (allow .rs files)
(craneLib.filterCargoSources path type)
;
};
# Arguments to be used by both the client and the server
# When building a workspace with crane, it's a good idea
# to set "pname" and "version".
commonArgs = {
inherit src;
pname = "trunk-workspace";
version = "0.1.0";
strictDeps = true;
buildInputs = with pkgs; [
# Add additional build inputs here
] ++ lib.optionals pkgs.stdenv.isDarwin [
# Additional darwin specific inputs can be set here
darwin.apple_sdk.frameworks.SystemConfiguration
];
};
# Native packages
nativeArgs = commonArgs // {
pname = "trunk-workspace-native";
};
# Build *just* the cargo dependencies, so we can reuse
# all of that work (e.g. via cachix) when running in CI
cargoArtifacts = craneLib.buildDepsOnly nativeArgs;
# Simple JSON API that can be queried by the client
myServer = craneLib.buildPackage (nativeArgs // {
inherit cargoArtifacts;
cargoExtraArgs = "--package=backend";
});
FEServer = craneLib.buildPackage (nativeArgs // {
inherit cargoArtifacts;
cargoExtraArgs = "--package=fe_server";
});
# Wasm packages
# it's not possible to build the server on the
# wasm32 target, so we only build the client.
wasmArgs = commonArgs // {
pname = "trunk-workspace-wasm";
cargoExtraArgs = "--package=frontend";
CARGO_BUILD_TARGET = "wasm32-unknown-unknown";
};
cargoArtifactsWasm = craneLib.buildDepsOnly (wasmArgs // {
doCheck = false;
});
# Build the frontend of the application.
# This derivation is a directory you can put on a webserver.
myClient = craneLib.buildTrunkPackage (wasmArgs // {
pname = "trunk-workspace-client";
cargoArtifacts = cargoArtifactsWasm;
trunkIndexPath = "frontend/index.html";
# The version of wasm-bindgen-cli here must match the one from Cargo.lock.
wasm-bindgen-cli = pkgs.wasm-bindgen-cli.override {
version = "0.2.89";
hash = "sha256-IPxP68xtNSpwJjV2yNMeepAS0anzGl02hYlSTvPocz8=";
cargoHash = "sha256-pBeQaG6i65uJrJptZQLuIaCb/WCQMhba1Z1OhYqA8Zc=";
};
});
FEdockerImage = pkgs.dockerTools.buildImage {
name = "snake_fe";
tag = "0.1";
created = "now";
# copyToRoot = pkgs.buildEnv {
# name = "image-root";
# paths = with pkgs; [
# bashInteractive
# # ls, etc
# busybox
# # curl
# ];
# pathsToLink = [
# "/bin"
# "/etc"
# ];
# };
config = {
Cmd = [ "${FEServer}/bin/fe_server" ];
Env = [
"FE_SRV__ENV=prod"
"FE_SRV__CONF_DIR=${./fe_server/conf}"
"FE_SRV__DIR=${myClient}"
"FE_SRV__FALLBACK=${myClient}/index.html"
];
};
};
BEdockerImage = pkgs.dockerTools.buildImage
{
name = "snake_be";
tag = "0.1";
created = "now";
config = {
Cmd = [ "${myServer}/bin/backend" ];
Env = [
"BE__ENV=prod"
"BE__CONF_DIR=${./backend/conf}"
];
};
};
in
{
packages =
{
inherit
myServer
myClient
FEdockerImage
BEdockerImage
# FEServer
;
default = myClient;
};
apps.default = flake-utils.lib.mkApp {
name = "backend";
drv = myServer;
};
devShells.default = craneLib.devShell {
# Inherit inputs from checks.
checks = self.checks.${system};
# Extra inputs can be added here; cargo and rustc are provided by default.
packages = with pkgs; [
trunk
dive
];
};
checks = {
# Build the crate as part of `nix flake check` for convenience
inherit myServer myClient;
# Run clippy (and deny all warnings) on the crate source,
# again, reusing 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.
my-app-clippy = craneLib.cargoClippy (commonArgs // {
inherit cargoArtifacts;
cargoClippyExtraArgs = "--all-targets -- --deny warnings";
});
# Check formatting
my-app-fmt = craneLib.cargoFmt commonArgs;
};
});
}