-
Notifications
You must be signed in to change notification settings - Fork 0
/
flakelight-zig.nix
83 lines (74 loc) · 2.29 KB
/
flakelight-zig.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
# flakelight-zig -- Zig module for flakelight
# Copyright (C) 2023 Archit Gupta <[email protected]>
# SPDX-License-Identifier: MIT
{ config, lib, src, flakelight, ... }:
let
inherit (builtins) deepSeq pathExists toString;
inherit (lib) mkIf mkOption warnIf;
inherit (lib.types) functionTo listOf package str;
inherit (lib.fileset) toSource unions;
inherit (flakelight.types) nullable;
readZon = import ./parseZon.nix lib;
buildZonFile = src + /build.zig.zon;
hasBuildZon = pathExists buildZonFile;
buildZon = readZon buildZonFile;
linkDeps = pkgs:
if config.zigDependencies != null then
"ln -s ${config.zigDependencies pkgs} $ZIG_GLOBAL_CACHE_DIR/p"
else "";
in
warnIf (! builtins ? readFileType) "Unsupported Nix version in use."
{
options = {
zigFlags = mkOption {
type = listOf str;
default = [ "--release=safe" "-Dcpu=baseline" ];
};
zigDependencies = mkOption {
type = nullable (functionTo package);
default = null;
};
zigSystemLibs = mkOption {
type = functionTo (listOf package);
default = _: [ ];
};
};
config = {
package = mkIf hasBuildZon
(deepSeq buildZon ({ stdenvNoCC, zig, pkg-config, pkgs, defaultMeta }:
stdenvNoCC.mkDerivation {
pname = buildZon.name;
version = buildZon.version;
src = toSource {
root = src;
fileset = unions (map (p: src + ("/" + p)) buildZon.paths);
};
nativeBuildInputs = [ zig pkg-config ];
buildInputs = config.zigSystemLibs pkgs;
strictDeps = true;
dontConfigure = true;
dontInstall = true;
postPatch = ''
export ZIG_GLOBAL_CACHE_DIR=$(mktemp -d)
${linkDeps pkgs}
'';
buildPhase = ''
runHook preBuild
mkdir -p $out
zig build ${toString config.zigFlags} --prefix $out
runHook postBuild
'';
meta = defaultMeta;
}));
devShell.packages = pkgs: (with pkgs; [ zig zls pkg-config ])
++ config.zigSystemLibs pkgs;
checks.test = pkgs: ''
export ZIG_GLOBAL_CACHE_DIR=$(mktemp -d)
${linkDeps pkgs}
${pkgs.zig}/bin/zig build test
'';
formatters = pkgs: {
"*.zig" = "${pkgs.zig} fmt";
};
};
}