-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflake.nix
More file actions
194 lines (170 loc) · 6.35 KB
/
Copy pathflake.nix
File metadata and controls
194 lines (170 loc) · 6.35 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
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
{
description = "Rust flake template";
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
flake-parts.url = "github:hercules-ci/flake-parts";
treefmt-nix = {
url = "github:unionlabs/treefmt-nix";
inputs.nixpkgs.follows = "nixpkgs";
};
rust-overlay = {
url = "github:oxalica/rust-overlay";
inputs.nixpkgs.follows = "nixpkgs";
};
crane = {
url = "github:ipetkov/crane";
};
aoc-inputs = {
url = "git+ssh://git@github.com/benluelo/advent-of-code-inputs.git";
flake = false;
};
};
outputs = inputs@{ self, nixpkgs, rust-overlay, flake-parts, aoc-inputs, treefmt-nix, ... }:
flake-parts.lib.mkFlake { inherit inputs; } {
systems =
[ "x86_64-linux" "aarch64-linux" "aarch64-darwin" "x86_64-darwin" ];
imports = [
treefmt-nix.flakeModule
];
perSystem = { config, self', inputs', pkgs, system, ... }:
let
craneLib = (self.inputs.crane.mkLib pkgs).overrideToolchain rust-nightly;
rust-nightly = pkgs.rust-bin.fromRustupToolchainFile ./rust/rust-toolchain.toml;
cargoVendorDir = craneLib.vendorMultipleCargoDeps {
inherit (craneLib.findCargoFiles ./rust) cargoConfigs;
cargoLockList = [
./rust/Cargo.lock
"${rust-nightly.passthru.availableComponents.rust-src}/lib/rustlib/src/rust/library/Cargo.lock"
];
};
CARGO_BUILD_TARGET =
if system == "aarch64-linux" then "aarch64-unknown-linux-gnu"
else if system == "x86_64-linux" then "x86_64-unknown-linux-gnu"
else if system == "aarch64-darwin" then "aarch64-apple-darwin"
else if system == "x86_64-darwin" then "x86_64-apple-darwin"
else throw "unsupported system `${system}`";
fs = pkgs.lib.fileset;
filteredSrc =
fs.toSource {
root = ./.;
fileset = fs.unions [
./rust/Cargo.toml
./rust/Cargo.lock
./rust/src
(craneLib.fileset.commonCargoSources ./.)
];
};
# TODO: Filter out days >12 for years >=2025
years = [ 2021 2022 2023 2024 2025 ];
days = builtins.genList (builtins.add 1) 25;
mkAocDay = year: day: const:
let
dayYear = "${toString year}-${toString day}";
suffix = "${if const then "-const" else ""}";
pname = "advent-of-code-${dayYear}${suffix}";
attrs = {
inherit pname;
version = "0.0.0";
src = builtins.trace "${filteredSrc}" filteredSrc;
buildInputs = [ rust-nightly ] ++ (
pkgs.lib.optionals pkgs.stdenv.isDarwin [ pkgs.darwin.apple_sdk.MacOSX-SDK ]
);
nativeBuildInputs = pkgs.lib.optionals pkgs.stdenv.isDarwin [ ];
inherit cargoVendorDir;
preBuild = ''
cp -r ${aoc-inputs} inputs
cd rust
${
if pkgs.stdenv.isDarwin
then ''
echo ${pkgs.darwin.apple_sdk.MacOSX-SDK}
export SDKROOT="${pkgs.darwin.apple_sdk.MacOSX-SDK}"
''
else ""
}
# export RUSTC_LOG=rustc_codegen_ssa::back::link=info
'';
cargoCheckExtraArgs = "--no-default-features -F ${if const then "const," else ""}${dayYear}";
cargoTestExtraArgs = "--no-default-features -F ${if const then "const," else ""}${dayYear}";
cargoBuildCommand = "cargo rustc --release --no-default-features -F ${if const then "const" else ""},${dayYear} --target ${CARGO_BUILD_TARGET} -j1 -Z build-std=std,alloc,core -Z build-std-features=compiler-builtins-mem";
meta.mainProgram = pname;
};
in
craneLib.buildPackage (attrs // {
cargoArtifacts = craneLib.buildDepsOnly ((builtins.removeAttrs attrs [ "src" ]) // {
dummySrc = attrs.src;
});
});
in
{
_module.args.pkgs = import nixpkgs {
inherit system;
overlays = [
rust-overlay.overlays.default
];
};
packages = (builtins.listToAttrs (
map
({ year, day, const }: {
name = "rust-${toString year}-${toString day}${if const then "-const" else ""}";
value = mkAocDay year day const;
})
(pkgs.lib.cartesianProduct {
year = years;
day = days;
const = [ true false ];
})
)) // {
rust-full =
let
crateInfo = craneLib.crateNameFromCargoToml { cargoToml = ./rust/Cargo.toml; };
in
craneLib.buildPackage (
crateInfo
// {
src = craneLib.cleanCargoSource ./rust;
doCheck = false;
}
);
};
devShells = {
default = pkgs.mkShell {
buildInputs = [ rust-nightly ]
++ (with pkgs; [
marksman
nil
cargo-flamegraph
strace
]);
nativeBuildInputs = [ config.treefmt.build.wrapper ]
++ pkgs.lib.attrsets.attrValues config.treefmt.build.programs;
};
};
treefmt = {
projectRootFile = "flake.nix";
programs = {
nixpkgs-fmt.enable = true;
taplo.enable = true;
dprint = {
enable = true;
settings = {
lineWidth = 80;
includes = [ "*.md" ];
# markdown = {
# textWrap = "always";
# };
plugins = [
"https://plugins.dprint.dev/markdown-0.17.8.wasm"
];
};
};
rustfmt = {
enable = true;
package = rust-nightly;
edition = "2024";
};
};
};
};
};
}