-
Notifications
You must be signed in to change notification settings - Fork 3
/
flake.nix
298 lines (261 loc) · 9.08 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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
{
description = "scalals";
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixos-24.05";
nix-filter.url = "github:numtide/nix-filter";
flake-compat = {
url = "github:edolstra/flake-compat";
flake = false;
};
flake-utils.url = "github:numtide/flake-utils";
git-hooks = {
inputs = {
nixpkgs.follows = "nixpkgs";
};
url = "github:cachix/git-hooks.nix";
};
sbt = {
inputs = {
nixpkgs.follows = "nixpkgs";
flake-utils.follows = "flake-utils";
};
url = "github:zaninime/sbt-derivation";
};
};
outputs =
{
self,
nixpkgs,
nix-filter,
flake-utils,
git-hooks,
sbt,
...
}:
flake-utils.lib.eachSystem
[
"aarch64-linux"
"aarch64-darwin"
"x86_64-darwin"
"x86_64-linux"
]
(
system:
let
filter = nix-filter.lib;
jreHeadlessOverlay = _: prev: { jre = prev.openjdk11_headless; };
scalafmtOverlay = final: prev: {
scalafmt = prev.scalafmt.overrideAttrs (
old:
let
version = builtins.head (
builtins.match ''[ \n]*version *= *"([^ \n]+)".*'' (builtins.readFile ./.scalafmt.conf)
);
outputHash = "sha256-vR6bI9HsTJNbdq4BAnv4/LmV00csS1QrnxW8nRsX1/M=";
in
{
inherit version;
passthru = {
inherit outputHash;
};
buildInputs = [
(prev.stdenv.mkDerivation {
name = "scalafmt-deps-${version}";
buildCommand = ''
export COURSIER_CACHE=$(pwd)
${prev.coursier}/bin/cs fetch org.scalameta:scalafmt-cli_2.13:${version} > deps
mkdir -p $out/share/java
cp $(< deps) $out/share/java/
'';
outputHashMode = "recursive";
inherit outputHash;
outputHashAlgo = if outputHash == "" then "sha256" else null;
})
];
}
);
};
pkgs = import nixpkgs {
inherit system;
overlays = [
jreHeadlessOverlay
scalafmtOverlay
];
};
inherit (pkgs) lib stdenvNoCC zig_0_12;
zig = zig_0_12;
mkShell = pkgs.mkShell.override { stdenv = stdenvNoCC; };
clang = pkgs.writeScriptBin "clang" ''
#!${pkgs.bash}/bin/bash
declare -a args
for arg; do
arg="''${arg/-unknown-/-}"
arg="''${arg/-apple-darwin-none/-macos-none}"
args+=( "$arg" )
done
exec ${zig}/bin/zig cc "''${args[@]}"
'';
clangpp = pkgs.writeShellApplication {
name = "clang++";
runtimeInputs = [ pkgs.gnused ];
text = ''
declare -a args=()
declare -a tmpfiles=()
trap '[[ "''${#tmpfiles[@]}" -gt 0 ]] && rm -v "''${tmpfiles[@]}"' EXIT
for arg; do
case "$arg" in
@* )
infile="''${arg:1}"
resp=$( mktemp )
tmpfiles+=( "$resp" )
sed -e 's,-unknown-,-,' -e 's,-apple-darwin-none,-macos-none,' "$infile" >> "$resp"
args+=( "@$resp" )
;;
* )
arg="''${arg/-unknown-/-}"
arg="''${arg/-apple-darwin-none/-macos-none}"
args+=( "$arg" )
esac
done
${zig}/bin/zig c++ "''${args[@]}"
'';
};
nativeBuildInputs = with pkgs; [
git
ninja
zig
which
clang
clangpp
];
in
rec {
formatter = pkgs.nixfmt-rfc-style;
packages = rec {
inherit (pkgs) scalafmt;
scalals = sbt.lib.mkSbtDerivation rec {
inherit pkgs nativeBuildInputs;
overrides = {
stdenv = stdenvNoCC;
};
pname = "scalals-native";
# read the first non-empty string from the VERSION file
version = builtins.head (builtins.match "[ \n]*([^ \n]+).*" (builtins.readFile ./VERSION));
depsSha256 = "sha256-WhH2QsBRMAPdvk7A4ONZC8Ptqj72oDYwiXcT5S97ntw=";
src = filter {
root = self;
include = [
"native"
"project"
"shared"
./.jvmopts
./build.sbt
];
};
# explicitly override version from sbt-dynver which does not work within a nix build
patchPhase = ''
echo 'ThisBuild / version := "${version}"' > version.sbt
'';
env = {
SCALANATIVE_MODE = "release-full"; # {debug, release-fast, release-full}
SCALANATIVE_LTO = if stdenvNoCC.isLinux then "thin" else "none"; # {none, full, thin}
XDG_CACHE_HOME = "xdg_cache"; # needed by zig cc for a writable directory
NIX_CFLAGS_COMPILE =
if stdenvNoCC.isLinux && stdenvNoCC.isx86_64 then
# zig uses -march=native by default
"-march=sandybridge"
else if stdenvNoCC.isDarwin then
# need to set target explicitly, see https://github.com/ziglang/zig/issues/14651
"-target ${stdenvNoCC.hostPlatform.qemuArch}-macos.11.0-none"
else
null;
};
buildPhase = ''
sbt tpolecatReleaseMode 'project scalalsNative' 'show nativeConfig' ninjaCompile ninja
ninja -f native/target/build.ninja
'';
dontPatchELF = true;
depsWarmupCommand = "sbt tpolecatDevMode scalalsNative/compile";
installPhase = ''
mkdir --parents $out/bin
cp "$(ninja -f native/target/build.ninja -t targets rule exe)" $out/bin/scalals
'';
};
default = scalals;
};
apps.default = flake-utils.lib.mkApp {
drv = packages.default;
exePath = "/bin/scalals";
};
checks = {
pre-commit-check = git-hooks.lib.${system}.run {
src = ./.;
hooks = {
nix-fmt = {
enable = true;
name = "nix fmt";
entry = "${pkgs.nixfmt-rfc-style}/bin/nixfmt";
types = [ "nix" ];
};
scalafmt = {
enable = true;
name = "scalafmt";
entry = "${pkgs.scalafmt}/bin/scalafmt --respect-project-filters";
types = [ "scala" ];
};
};
};
};
devShells =
{
default = mkShell {
name = "scalals";
env.SBT_TPOLECAT_DEV = "1";
shellHook = ''
${checks.pre-commit-check.shellHook}
'';
packages = [ pkgs.metals ];
nativeBuildInputs = nativeBuildInputs ++ [ pkgs.sbt ];
};
graalVM = pkgs.mkShell {
name = "scalals / graalvm";
# Workaround GraalVM issue where the builder does not have access to the
# environment variables since 21.0.0
# https://github.com/oracle/graal/pull/6095
# https://github.com/oracle/graal/pull/6095
# https://github.com/oracle/graal/issues/7502
env.NATIVE_IMAGE_DEPRECATED_BUILDER_SANITATION = "true";
shellHook = ''
${checks.pre-commit-check.shellHook}
'';
nativeBuildInputs = [
pkgs.graalvm-ce
pkgs.sbt
];
};
}
// (lib.optionalAttrs (system == "x86_64-linux") (
let
inherit (pkgs.pkgsCross.aarch64-multiplatform-musl) llvmPackages_13 mkShell;
in
{
aarch64-cross = mkShell.override { stdenv = llvmPackages_13.libcxxStdenv; } {
name = "scalals-arm64";
env.NIX_CFLAGS_LINK = "-static";
nativeBuildInputs = [
pkgs.lld_13
pkgs.git
pkgs.ninja
pkgs.which
pkgs.sbt
];
};
}
));
# compatibility for nix < 2.7.0
defaultApp = apps.default;
defaultPackage = packages.default;
devShell = devShells.default;
}
);
}