-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflakelight-zig.nix
120 lines (104 loc) · 3.19 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
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
# flakelight-zig -- Zig module for flakelight
# Copyright (C) 2023 Archit Gupta <archit@accelbread.com>
# SPDX-License-Identifier: MIT
{ config, lib, src, ... }:
let
inherit (builtins) attrValues deepSeq elemAt fetchGit listToAttrs match
pathExists toString;
inherit (lib) filter mkIf mkMerge mkOption pipe warnIf;
inherit (lib.types) functionTo lazyAttrsOf listOf package str;
inherit (lib.fileset) toSource unions;
inherit (config) zigToolchain zigFlags zigPackages zigSystemLibs;
strictEval = x: deepSeq x x;
readZon = import ./parseZon.nix lib;
buildZonFile = src + /build.zig.zon;
hasBuildZon = pathExists buildZonFile;
buildZon = strictEval (readZon buildZonFile);
pkgDir = pkgs: pkgs.linkFarm "zig-packages" (zigPackages pkgs);
makeZigCacheDir = pkgs: ''
export ZIG_GLOBAL_CACHE_DIR=$(mktemp -d)
ln -s ${pkgDir pkgs} $ZIG_GLOBAL_CACHE_DIR/p
'';
dependencies = buildZon.dependencies or { };
# zon deps with git urls can be automatically converted into nix drvs
gitDependencies = pkgs: pipe dependencies [
attrValues
(filter (d: d ? url))
(map (d: {
name = d.hash;
captures = match "git\\+(.*)#([a-z0-9]+)" d.url;
}))
(filter (d: d.captures != null))
(map (d: {
inherit (d) name;
value = fetchGit {
url = elemAt d.captures 0;
rev = elemAt d.captures 1;
shallow = true;
};
}))
listToAttrs
];
in
warnIf (! builtins ? readFileType) "Unsupported Nix version in use."
{
options = {
zigToolchain = mkOption {
type = functionTo (lazyAttrsOf package);
default = pkgs: { inherit (pkgs) zig zls; };
};
zigFlags = mkOption {
type = listOf str;
default = [ "--release=safe" "-Dcpu=baseline" ];
};
zigPackages = mkOption {
type = functionTo (lazyAttrsOf package);
default = gitDependencies;
};
zigSystemLibs = mkOption {
type = functionTo (listOf package);
default = _: [ ];
};
};
config = mkMerge [
(mkIf hasBuildZon {
pname = buildZon.name;
package = { stdenvNoCC, 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 = [ (zigToolchain pkgs).zig pkg-config ];
buildInputs = zigSystemLibs pkgs;
strictDeps = true;
dontConfigure = true;
dontInstall = true;
postPatch = ''
${makeZigCacheDir pkgs}
'';
buildPhase = ''
runHook preBuild
mkdir -p $out
zig build ${toString zigFlags} --prefix $out
runHook postBuild
'';
meta = defaultMeta;
};
checks.test = pkgs: ''
${makeZigCacheDir pkgs}
${(zigToolchain pkgs).zig}/bin/zig build test
'';
})
{
devShell.packages = pkgs: (with pkgs; [ pkg-config ])
++ attrValues (zigToolchain pkgs)
++ config.zigSystemLibs pkgs;
formatters = pkgs: {
"*.zig" = "${(zigToolchain pkgs).zig} fmt";
};
}
];
}