-
Notifications
You must be signed in to change notification settings - Fork 0
/
flake.nix
175 lines (156 loc) · 6.36 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
# Make sure to run "nix flake update" at least once to generate your flake.lock.
# Run your main function from Nix by importing this flake as follows:
#
# let
# yourProject = builtins.getFlake "URL"; # for example
# mainResult = (yourProject.lib.loadGlistixPackage {}).main {};
# in doSomethingWith mainResult;
#
# See below to customize your flake.
{
description = "json - Work with JSON in Glistix - Port of gleam_json to Nix";
inputs = {
nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable";
systems.url = "github:nix-systems/default";
# Used for default.nix and shell.nix.
flake-compat = {
url = "github:edolstra/flake-compat";
flake = false;
};
# Pick your Glistix version here.
glistix.url = "github:glistix/glistix/v0.1.0";
# Submodules
# Add any submodules which you use as dependencies here,
# and then add them to "submodules = [ ... ]" below.
stdlib = {
url = "github:glistix/stdlib";
flake = false;
};
};
outputs =
inputs@{ self, nixpkgs, flake-parts, systems, glistix, stdlib, ... }:
let
# --- CUSTOMIZATION PARAMETERS ---
# Add any source files to keep when building your Glistix package here.
# This includes anything which might be necessary at build time.
sourceFiles = [
"gleam.toml"
"manifest.toml" # make sure to build locally at least once so Glistix generates this file
"src"
"test"
"external" # cloned package repositories
"output" # checked-in build output, if it exists
"priv" # assets and other relevant files
".gitignore"
".gitmodules"
];
# Submodules have to be specified here, even if they are cloned locally.
# Add them as inputs to the flake, and then specify where to clone them to
# during build below.
submodules = [
{
src = stdlib;
dest = "external/stdlib";
}
];
# If you cache your build output, this will specify the path
# 'loadGlistixPackage' will check to find compiled Nix files in by default.
# You usually don't have to change this, unless you cache your output in a
# folder other than ./output, or if your output is fetched through a derivation
# (e.g. `builtins.fetchGit`).
# Don't forget to add the path to "sourceFiles" above if it's in the repo!
outputPath = src + "/output";
# Set this to 'true' if you created an 'output' folder where you're storing
# build outputs and you'd like to ensure Nix consumers will use it.
# It will be used even if this is 'false', but Glistix will fallback to
# building your package from scratch upon load if the output folder is
# missing. If you set this to 'true', it will error instead.
forceLoadFromOutput = false;
# --- IMPLEMENTATION ---
inherit (nixpkgs) lib;
glistixLib = glistix.lib;
# Filter source files to only include the given files and folders.
src = lib.cleanSourceWith {
filter = path: type:
builtins.any (accepted: lib.path.hasPrefix (./. + "/${accepted}") (/. + path)) sourceFiles;
src = ./.;
};
# Prepare call to 'buildGlistixPackage', allowing for overrides.
buildGlistixPackage =
args@{ system, ... }:
let
inherit (glistix.builders.${system}) buildGlistixPackage;
overrides = builtins.removeAttrs args [ "system" ];
builderArgs = {
inherit src submodules;
} // overrides;
in
buildGlistixPackage builderArgs;
# Prepare the call to 'loadGlistixPackage'. This is used to
# easily run your Gleam code compiled to Nix from within Nix.
#
# This will try to read compiled code at 'output/dev/nix',
# thus not invoking the Glistix compiler at all if possible,
# avoiding the need to compile, install and run it.
# If that path does not exist, however, uses Glistix to build your
# Gleam package from scratch instead, using the derivation
# created further below (exported by this flake under
# the 'packages' output).
#
# Specify 'forceLoadFromOutput = true;' above to opt into erroring
# if the 'output/dev/nix' folder isn't found instead of invoking
# the Glistix compiler.
#
# Pass 'system' to use the derivation for that system for compilation.
# Pass 'glistix' to override the glistix derivation used for compilation.
# Other arguments are passed through to Glistix's 'loadGlistixPackage'.
# For example, 'lib.loadGlistixPackage { module = "ops/create"; }'
# will load what's exported by your package's 'ops/create' module
# as an attribute set.
loadGlistixPackage =
args@{
system ? builtins.currentSystem or null,
glistix ? null,
...
}:
let
derivation =
if forceLoadFromOutput || system == null then
null
else if glistix != null then
buildGlistixPackage { inherit system glistix; }
else
self.packages.${system}.default or null;
overrides = builtins.removeAttrs args [
"system"
"glistix"
];
loaderArgs = {
inherit src derivation;
output = outputPath;
} // overrides;
in
glistixLib.loadGlistixPackage loaderArgs;
in flake-parts.lib.mkFlake { inherit inputs; } {
systems = import systems;
flake = { lib = { inherit loadGlistixPackage; }; };
perSystem = { self', pkgs, lib, system, ... }:
let
# This derivation will build Glistix itself if needed
# (using Rust), and then use Glistix to build this particular
# package into Nix files.
# The derivation's "out" output will contain the resulting
# 'build' directory. You can use
# "${derivation}/${derivation.glistixMain}"
# for a path to this package's main '.nix' file, which can
# be imported through Nix's `import`.
package = buildGlistixPackage { inherit system; };
in {
packages.default = package;
# Run 'nix develop' to create a shell where 'glistix' is available.
devShells.default = pkgs.mkShell {
nativeBuildInputs = [ glistix.packages.${system}.default ];
};
};
};
}