-
Notifications
You must be signed in to change notification settings - Fork 0
/
flake.nix
210 lines (191 loc) · 6.19 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
{
description = "nix-rust-template";
nixConfig = {
extra-substituters = [
"https://crane.cachix.org"
"https://nix-community.cachix.org"
];
extra-trusted-public-keys = [
"crane.cachix.org-1:8Scfpmn9w+hGdXH/Q9tTLiYAE/2dnJYRJP7kl80GuRk="
"nix-community.cachix.org-1:mB9FSh9qf2dCimDSUo8Zy7bkq5CX+/rkCWyvRCYg3Fs="
];
};
inputs = {
flake-parts.url = "github:hercules-ci/flake-parts";
nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
treefmt-nix.url = "github:numtide/treefmt-nix";
treefmt-nix.inputs.nixpkgs.follows = "nixpkgs";
fenix.url = "github:nix-community/fenix";
fenix.inputs.nixpkgs.follows = "nixpkgs";
crane.url = "github:ipetkov/crane";
advisory-db.url = "github:rustsec/advisory-db";
advisory-db.flake = false;
};
outputs =
inputs@{
flake-parts,
treefmt-nix,
...
}:
flake-parts.lib.mkFlake { inherit inputs; } {
systems = [
"x86_64-linux"
"aarch64-linux"
"x86_64-darwin"
"aarch64-darwin"
];
imports = [
treefmt-nix.flakeModule
./nixos
];
perSystem =
{
self',
inputs',
pkgs,
lib,
...
}:
let
rustToolchain =
with inputs'.fenix.packages;
combine [
stable.toolchain
targets.wasm32-unknown-unknown.stable.rust-std
];
craneLib = (inputs.crane.mkLib pkgs).overrideToolchain rustToolchain;
commonAttrs = {
pname = "game";
src = lib.fileset.toSource {
root = ./.;
fileset = lib.fileset.unions [
./Cargo.toml
./Cargo.lock
./game
];
};
# https://github.com/bevyengine/bevy/blob/latest/docs/linux_dependencies.md
nativeBuildInputs =
with pkgs;
[
pkg-config
]
++ lib.optionals stdenv.isDarwin [
rustPlatform.bindgenHook
];
buildInputs =
with pkgs;
[
]
++ lib.optionals stdenv.hostPlatform.isLinux [
alsa-lib
vulkan-loader
udev
# To use the x11 feature
xorg.libX11
xorg.libXcursor
xorg.libXi
xorg.libXrandr
# To use the wayland feature
libxkbcommon
wayland
]
++ lib.optionals stdenv.isDarwin [
libiconv
darwin.apple_sdk.frameworks.Cocoa
darwin.apple_sdk.frameworks.CoreAudio
darwin.apple_sdk.frameworks.AudioUnit
];
# the coverage report will run the tests
doCheck = false;
};
commonAttrsWasm = commonAttrs // {
cargoExtraArgs = "--target wasm32-unknown-unknown";
nativeBuildInputs = commonAttrs.nativeBuildInputs ++ [ pkgs.lld ];
CARGO_TARGET_WASM32_UNKNOWN_UNKNOWN_LINKER = "lld";
};
in
{
devShells.default = pkgs.mkShell {
inputsFrom = [ self'.packages.default ];
WINIT_X11_SCALE_FACTOR = "2";
LD_LIBRARY_PATH = "$LD_LIBRARY_PATH:${lib.makeLibraryPath commonAttrs.buildInputs}";
# wasm
CARGO_TARGET_WASM32_UNKNOWN_UNKNOWN_LINKER = "lld";
packages = [
pkgs.lld
] ++ lib.optionals pkgs.stdenv.isLinux [ pkgs.ldtk ];
};
packages = {
game-deps-wasm = craneLib.buildDepsOnly commonAttrsWasm;
game-deps = craneLib.buildDepsOnly commonAttrs;
game-docs = craneLib.cargoDoc (
commonAttrs
// {
cargoArtifacts = self'.packages.game-deps;
}
);
game-wasm = craneLib.buildPackage (
commonAttrsWasm
// {
cargoArtifacts = self'.packages.game-deps;
}
);
game-dist =
pkgs.runCommand "game-wasm-bindgen" { nativeBuildInputs = [ pkgs.wasm-bindgen-cli ]; }
''
wasm-bindgen --no-typescript --target web \
--out-dir $out \
--out-name "game" \
${self'.packages.game-wasm}/bin/game.wasm
ln -s ${./game/assets}/ $out/assets
ln -s ${./dist}/index.html $out/index.html
ln -s ${./dist}/js $out/js
'';
game = craneLib.buildPackage (
commonAttrs
// {
cargoArtifacts = self'.packages.game-deps;
meta.mainProgram = "game";
postInstall = ''
ln -s ${./game/assets}/ $out/bin/assets
'';
postFixup = lib.optionalString pkgs.stdenv.hostPlatform.isLinux ''
patchelf $out/bin/game \
--add-rpath ${
lib.makeLibraryPath [
pkgs.vulkan-loader
pkgs.libxkbcommon
]
}
'';
}
);
default = self'.packages.game;
};
checks = {
inherit (self'.packages) game-docs game game-dist;
lint = craneLib.cargoClippy (
commonAttrs
// {
cargoArtifacts = self'.packages.game-deps;
cargoClippyExtraArgs = "--all-targets -- --deny warnings";
}
);
coverage-report = craneLib.cargoTarpaulin (
commonAttrs
// {
cargoArtifacts = self'.packages.game-deps;
}
);
};
treefmt = {
projectRootFile = ".git/config";
programs.nixfmt.enable = true;
programs.rustfmt.enable = true;
programs.rustfmt.package = craneLib.rustfmt;
settings.formatter = { };
};
};
};
}