-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathflake.nix
408 lines (330 loc) · 15 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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
{
description = "zig2nix flake";
inputs = {
nixpkgs.url = "github:nixos/nixpkgs";
flake-utils.url = "github:numtide/flake-utils";
};
outputs = { self, flake-utils, ... }: with builtins; let
outputs = (flake-utils.lib.eachDefaultSystem (system: let
pkgs = self.inputs.nixpkgs.outputs.legacyPackages.${system};
#! Structures.
# Use our own zig hook.
# The nixpkgs one forces flags which can't be overridden.
# Also -target is recommended over use of -Dcpu=baseline.
# https://ziggit.dev/t/exe-files-not-interchangeable-among-identical-linux-systems/2708/6
# I would've reused the setup-hook.sh, but it breaks when cross-compiling.
zigHook = { makeSetupHook, zig }: makeSetupHook {
name = "zig-hook";
propagatedBuildInputs = [ zig ];
substitutions.zig_default_flags = [];
passthru = { inherit zig; };
} ./src/setup-hook.sh;
# Zig versions
# <https://ziglang.org/download/index.json>
zigv = import ./src/zig/versions.nix {
inherit zigHook;
inherit (pkgs) callPackage;
};
# zig2nix bridge utility
# always compiled with zig-latest, but passes the correct zig to the PATH of the process
zig2nix-for-version = zig: let
pkg = pkgs.callPackage ./src/zig2nix/default.nix {
zig = zigv.latest;
zigBuildFlags = [ "-Dcpu=baseline" ];
};
in pkgs.writeShellApplication {
name = "zig2nix";
runtimeInputs = [ zig ];
text = ''${pkg}/bin/zig2nix "$@"'';
};
#:! Helper function for building and running Zig projects.
zig-env = {
# Overrideable nixpkgs.
nixpkgs ? self.inputs.nixpkgs,
# Zig version to use.
zig ? zigv.latest,
}: with pkgs.lib; let
#! --- Outputs of zig-env {} function.
#! access: (zig-env {}).thing
# Use provided nixpkgs in here.
pkgs = nixpkgs.outputs.legacyPackages.${system};
#! Tools for bridging zig and nix
zig2nix = zig2nix-for-version zig;
exec = cmd: args: pkgs.runCommandLocal cmd {} ''${zig2nix}/bin/zig2nix ${cmd} ${escapeShellArgs args} > $out'';
exec-path = cmd: path: args: pkgs.runCommandLocal cmd {} ''${zig2nix}/bin/zig2nix ${cmd} ${path} ${escapeShellArgs args} > $out'';
exec-json = cmd: args: fromJSON (readFile (exec cmd args));
exec-json-path = cmd: path: args: fromJSON (readFile (exec-path cmd path args));
#! Translates zig and nix compatible targets
target = system: (exec-json "target" [ system ]);
#! Reads zon file into a attribute set
fromZON = path: exec-json-path "zon2json" path [];
#! Creates derivation from zon2json-lock file
deriveLockFile = path: pkgs.callPackage (exec-path "zon2nix" path []);
# Provides small shell runtime
shell-runtime = pkgs.callPackage ./src/shell.nix { inherit system; };
#! Returns true if target is nix flake compatible.
#! <https://github.com/NixOS/nixpkgs/blob/master/lib/systems/flake-systems.nix>
isFlakeTarget = any: pkgs.lib.any (s: (systems.elaborate s).config == (target any).config) systems.flakeExposed;
#! Returns crossPkgs from nixpkgs for target string or system.
#! This will always cross-compile the package.
crossPkgsForTarget = any: let
crossPkgs = import nixpkgs { localSystem = system; crossSystem = { config = (target any).config; }; };
this-system = (systems.elaborate system).config == (target any).config;
in if this-system then pkgs else crossPkgs;
#! Returns pkgs from nixpkgs for target string or system.
#! This does not cross-compile and you'll get a error if package does not exist in binary cache.
binaryPkgsForTarget = any: let
binaryPkgs = import nixpkgs { localSystem = { config = (target any).config; }; };
this-system = (systems.elaborate system).config == (target any).config;
in if this-system then pkgs else binaryPkgs;
#! Returns either binaryPkgs or crossPkgs depending if the target is flake target or not.
pkgsForTarget = any:
if isFlakeTarget any then binaryPkgsForTarget any
else crossPkgsForTarget any;
# Package a Zig project
zigPackage = any: (crossPkgsForTarget any).callPackage (pkgs.callPackage ./src/package.nix {
inherit zig target fromZON deriveLockFile;
});
#! Cross-compile nixpkgs using zig :)
#! NOTE: This is an experimental feature, expect it not faring well
zigCrossPkgsForTarget = any: let
crossPkgs = pkgs.callPackage ./src/cross {
inherit zig zigPackage target;
nixCrossPkgs = pkgsForTarget any;
nixBinaryPkgs = binaryPkgsForTarget any;
localSystem = system;
crossSystem = { config = (target any).config; };
};
in warn "zigCross: ${(target any).zig}" crossPkgs;
_deps = [ zig zig2nix ];
in rec {
inherit pkgs pkgsForTarget crossPkgsForTarget zigCrossPkgsForTarget binaryPkgsForTarget;
inherit zig zig2nix target fromZON deriveLockFile;
#! Flake app helper (Without zig-env and root dir restriction).
app-bare-no-root = deps: script: {
type = "app";
program = toString (pkgs.writeShellApplication {
name = "app";
runtimeInputs = [] ++ deps;
text = ''
# shellcheck disable=SC2059
error() { printf -- "error: $1\n" "''${@:2}" 1>&2; exit 1; }
${script}
'';
}) + "/bin/app";
};
#! Flake app helper (Without zig-env).
app-bare = deps: script: app-bare-no-root deps ''
[[ -f ./flake.nix ]] || error 'Run this from the project root'
${script}
'';
#! Flake app helper (without root dir restriction).
app-no-root = deps: script: app-bare-no-root (deps ++ _deps) ''
${shell-runtime deps}
${script}
'';
#! Flake app helper.
app = deps: script: app-bare (deps ++ _deps) ''
${shell-runtime deps}
${script}
'';
#! Creates dev shell.
mkShell = pkgs.callPackage ({
nativeBuildInputs ? [],
...
} @attrs: pkgs.mkShellNoCC (attrs // {
nativeBuildInputs = optionals (attrs ? nativeBuildInputs) attrs.nativeBuildInputs ++ _deps;
shellHook = ''
${shell-runtime nativeBuildInputs}
${attrs.shellHook or ""}
'';
}));
#! Package for specific target supported by nix.
#! You can still compile to other platforms by using package and specifying zigTarget.
#! When compiling to non-nix supported targets, you can't rely on pkgsForTarget, but rather have to provide all the pkgs yourself.
#! NOTE: Even though target is supported by nix, cross-compiling to it might not be, in that case you should get an error.
packageForTarget = zigPackage;
#! Packages zig project.
#! NOTE: If your project has build.zig.zon you must first generate build.zig.zon2json-lock using zon2json-lock.
#! It is recommended to commit the build.zig.zon2json-lock to your repo.
#!
#! Additional attributes:
#! zigTarget: Specify target for zig compiler, defaults to stdenv.targetPlatform of given target.
#! zigPreferMusl: Prefer musl libc without specifying the target.
#! zigWrapperBins: Binaries available to the binary during runtime (PATH)
#! zigWrapperLibs: Libraries available to the binary during runtime (LD_LIBRARY_PATH)
#! zigWrapperArgs: Additional arguments to makeWrapper.
#! zigBuildZon: Path to build.zig.zon file, defaults to build.zig.zon.
#! zigBuildZonLock: Path to build.zig.zon2json-lock file, defaults to build.zig.zon2json-lock.
#!
#! <https://github.com/NixOS/nixpkgs/blob/master/doc/hooks/zig.section.md>
package = packageForTarget system;
#! Bundle a package into a zip
bundle.zip = pkgs.callPackage ./src/bundle/zip.nix { inherit packageForTarget; };
#! Bundle a package for running in AWS lambda
bundle.aws.lambda = pkgs.callPackage ./src/bundle/lambda.nix { bundleZip = bundle.zip; };
};
test-env = zig-env { zig = zigv.master; };
test-app = test-env.app-bare;
test = removeAttrs (pkgs.callPackage src/test.nix {
inherit test-app;
inherit (test-env) zig zig2nix target deriveLockFile;
zig-env = test-env;
}) [ "override" "overrideDerivation" "overrideAttrs" ];
flake-outputs = pkgs.callPackage (import ./src/zig/outputs.nix) {
inherit zigv zig-env;
};
in with pkgs.lib; {
#! --- Architecture dependent flake outputs.
#! access: `zig2nix.outputs.thing.${system}`
#! Helper functions for building and running Zig projects.
inherit zig-env;
#! Versioned Zig packages.
#! nix build .#zig-master
#! nix build .#zig-latest
#! nix run .#zig-0_13_0
packages = mapAttrs' (k: v: nameValuePair ("zig-" + k) v) zigv;
# Generates flake apps for all the zig versions.
apps = flake-outputs.apps // {
default = flake-outputs.apps.zig2nix-latest;
# Backwards compatibility
zon2json = flake-outputs.apps.zon2json-latest;
zon2json-lock = flake-outputs.apps.zon2json-lock-latest;
zon2nix = flake-outputs.apps.zon2nix-latest;
# nix run .#update-versions
update-versions = test-app [ test-env.zig2nix ] ''
tmp="$(mktemp)"
trap 'rm -f "$tmp"' EXIT
zig2nix versions "$@" > "$tmp"
cp -f "$tmp" src/zig/versions.nix
'';
# nix run .#update-templates
update-templates = with pkgs; test-app [ coreutils gnused ] ''
rm -rf templates/default
mkdir -p templates/default
sed 's#/[*]SED_ZIG_VER[*]/##' templates/flake.nix > templates/default/flake.nix
sed -i 's#@SED_ZIG_BIN@#default#' templates/default/flake.nix
cp -f templates/gitignore templates/default/.gitignore
cp -f .gitattributes templates/default/.gitattributes
(cd templates/default; ${zigv.latest}/bin/zig init)
(cd templates/default; nix flake check --override-input zig2nix ../..)
rm -rf templates/master
mkdir -p templates/master
# shellcheck disable=SC2016
sed 's#/[*]SED_ZIG_VER[*]/# zig = zig2nix.outputs.packages.''${system}.zig-master; #' templates/flake.nix > templates/master/flake.nix
sed -i 's#@SED_ZIG_BIN@#master#' templates/master/flake.nix
cp -f templates/gitignore templates/master/.gitignore
cp -f .gitattributes templates/master/.gitattributes
(cd templates/master; ${zigv.master}/bin/zig init)
(cd templates/master; nix flake check --override-input zig2nix ../..)
'';
# nix run .#readme
readme = let
project = "zig2nix flake";
in with pkgs; test-app [ gawk gnused ] (replaceStrings ["`"] ["\\`"] ''
cat <<EOF
# ${project}
Flake for packaging, building and running Zig projects.
https://ziglang.org/
* Cachix: `cachix use zig2nix`
---
[](https://opensource.org/licenses/MIT)
* Zig master: `${zigv.master.version} @ ${zigv.master.date}`
* Zig latest: `${zigv.latest.version} @ ${zigv.latest.date}`
## Examples
### Zig project template
```bash
nix flake init -t github:Cloudef/zig2nix
nix run .
# for more options check the flake.nix file
```
#### With master version of Zig
```bash
nix flake init -t github:Cloudef/zig2nix#master
nix run .
# for more options check the flake.nix file
```
### Build zig from source
```bash
nix build github:Cloudef/zig2nix#zig-src-master
nix build github:Cloudef/zig2nix#zig-src-latest
nix build github:Cloudef/zig2nix#zig-src-0_8_0
```
### Running zig compiler directly
```bash
nix run github:Cloudef/zig2nix#master -- version
nix run github:Cloudef/zig2nix#latest -- version
nix run github:Cloudef/zig2nix#0_8_0 -- version
```
#### Convenience zig for multimedia programs
> This sets (DY)LD_LIBRARY_PATH and PKG_CONFIG_PATH so that common libs are available
```bash
nix run github:Cloudef/zig2nix#multimedia-master -- version
nix run github:Cloudef/zig2nix#multimedia-latest -- version
nix run github:Cloudef/zig2nix#multimedia-0_8_0 -- version
```
### Shell for building and running a Zig project
```bash
nix develop github:Cloudef/zig2nix#master
nix develop github:Cloudef/zig2nix#latest
nix develop github:Cloudef/zig2nix#0_8_0
```
### Convert zon file to json
```bash
nix run github:Cloudef/zig2nix -- zon2json build.zig.zon
```
### Convert build.zig.zon to a build.zig.zon2json-lock
```bash
nix run github:Cloudef/zig2nix -- zon2lock build.zig.zon
```
### Convert build.zig.zon/2json-lock to a nix derivation
```bash
# calls zon2json-lock if build.zig.zon2json-lock does not exist (requires network access)
nix run github:Cloudef/zig2nix -- zon2nix build.zig.zon
# alternatively run against the lock file (no network access required)
nix run github:Cloudef/zig2nix -- zon2nix build.zig.zon2json-lock
```
## Crude documentation
Below is auto-generated dump of important outputs in this flake.
```nix
$(awk -f doc.awk flake.nix | sed "s/```/---/g")
```
EOF
'');
} // mapAttrs' (name: value: nameValuePair ("test-" + name) value) test;
#! Develop shell for building and running Zig projects.
#! nix develop .#zig_version
#! example: nix develop .#master
#! example: nix develop .#default
devShells = flake-outputs.devShells // {
default = flake-outputs.devShells.latest;
};
}));
welcome-template = description: ''
# ${description}
- Zig: https://ziglang.org/
## Build & Run
```
nix run .
```
See flake.nix for more options.
'';
in outputs // {
#! --- Generic flake outputs.
#! access: `zig2nix.outputs.thing`
#! Default project template
#! nix flake init -t templates
templates.default = rec {
path = ./templates/default;
description = "Default Zig project template";
welcomeText = welcome-template description;
};
#! Master project template
#! nix flake init -t templates#master
templates.master = rec {
path = ./templates/master;
description = "Master Zig project template";
welcomeText = welcome-template description;
};
};
}