-
Notifications
You must be signed in to change notification settings - Fork 0
/
flake.nix
223 lines (197 loc) · 7.3 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
223
{
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://[email protected]/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
crane = rec {
lib = self.inputs.crane.mkLib pkgs;
nightly = lib.overrideToolchain rust-nightly;
};
rust-nightly = pkgs.rust-bin.fromRustupToolchainFile ./rust/rust-toolchain.toml;
vendored = crane.lib.vendorMultipleCargoDeps {
inherit (crane.lib.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}`";
years = [ 2022 2023 ];
days = [ 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 ];
link-args = [ "-v" ];
# ++ (pkgs.lib.optionals pkgs.stdenv.isDarwin [
# "-e"
# "__start"
# "-Z"
# "-pie"
# "-no_eh_labels"
# "-dead_strip"
# "-allow_stack_execute"
# "-S"
# "-no_uuid"
# ]) ++ (pkgs.lib.optionals pkgs.stdenv.isLinux [
# "-e"
# "_start"
# "--no-eh-frame-hdr"
# "-z"
# "norelro"
# "-z"
# "nodefaultlib"
# "-z"
# "nognustack"
# "-nostdlib"
# "--disable-new-dtags"
# "--no-dynamic-linker"
# "--hash-style=sysv"
# "--no-rosegment"
# "-N"
# "--icf=all"
# "--ignore-data-address-equality"
# "--ignore-data-address-equality"
# "--noinhibit-exec"
# "--print-gc-sections"
# "--print-icf-sections"
# ]);
mkAocDay = year: day: const:
let
dayYear = "${toString year}-${toString day}";
suffix = "${if const then "-const" else ""}";
pname = "advent-of-code-${dayYear}${suffix}";
in
pkgs.stdenv.mkDerivation {
name = pname;
buildInputs = if pkgs.stdenv.isLinux then [ pkgs.elfkickers ] else [ ];
src = pkgs.stdenv.mkDerivation {
name = pname;
src = crane.lib.cleanCargoSource ./.;
buildInputs = [ rust-nightly ] ++ (
pkgs.lib.optionals pkgs.stdenv.isDarwin [ pkgs.darwin.apple_sdk.MacOSX-SDK ]
);
nativeBuildInputs = pkgs.lib.optionals pkgs.stdenv.isDarwin [ ];
buildPhase = ''
echo $PATH
cp -r --no-preserve=mode . $out
cp ${crane.lib.configureCargoVendoredDepsHook}/nix-support/setup-hook $out/setup-hook
source $out/setup-hook
mkdir $out/.cargo
touch "$out/.cargo/config.toml"
configureCargoVendoredDepsHook ${vendored} "$out/.cargo/config.toml"
ls -al ${aoc-inputs}/*
cp -r ${aoc-inputs} $out/inputs
# schrodinger's directory: this only exists if i print it's contents
ls $out/inputs/*
cd $out/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
cargo \
rustc \
-vvv \
--release \
--no-default-features \
-F ${if const then "const" else ""},${dayYear} \
--target ${CARGO_BUILD_TARGET} \
-j1 \
-Z build-std=alloc,core \
-Z build-std-features=core/panic_immediate_abort,compiler-builtins-mem \
-- \
-C linker=rust-lld \
-C link-args='${pkgs.lib.concatStringsSep "\n" link-args}'
'';
};
installPhase = pkgs.lib.concatStringsSep "\n" [
''
mkdir -p $out/bin
cp --no-preserve=mode $src/rust/target/${CARGO_BUILD_TARGET}/release/advent-of-code "$out/bin/${pname}"
''
# (pkgs.lib.optionalString pkgs.stdenv.isLinux ''
# ls -l $out/bin
# strip "$out/bin/${pname}"
# '')
''
ls -l $out/bin
chmod +x "$out/bin/${pname}"
''
];
meta.mainProgram = pname;
};
in
{
_module.args.pkgs = import nixpkgs {
inherit system;
overlays = with inputs; [
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 ];
})
);
devShells = {
default = pkgs.mkShell {
buildInputs = [ rust-nightly ]
++ (with pkgs; [
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;
programs.taplo.enable = true;
programs.rustfmt = {
enable = true;
package = rust-nightly;
};
};
};
};
}