-
Notifications
You must be signed in to change notification settings - Fork 200
/
flake-module.nix
310 lines (270 loc) · 12.6 KB
/
flake-module.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
/*
flake-parts module for IHP apps
can be imported in a flake inside an IHP app repo
*/
# with ihpFlake we receive arguments from the IHP flake in this repo itself
ihpFlake:
# these arguments on the other hand are from the flake where this module is imported
# i.e. from the flake of any particular IHP app
{ inputs, flake-parts-lib, lib, config, ... }:
{
imports = [
inputs.devenv.flakeModule
];
# the app can configure IHP using these options from its flake
options.perSystem = flake-parts-lib.mkPerSystemOption (
{ config, pkgs, system, ...}: {
options.ihp = {
enable = lib.mkEnableOption "Enable IHP support";
ghcCompiler = lib.mkOption {
description = ''
The GHC compiler to use for IHP.
'';
default = pkgs.haskell.packages.ghc98;
};
packages = lib.mkOption {
description = ''
List of packages that should be included in the IHP environment.
'';
type = lib.types.listOf (lib.types.package);
default = [];
};
haskellPackages = lib.mkOption {
description = ''
Function returning a list of Haskell packages to be installed in the IHP environment.
The set of Haskell packages is passed to the function.
'';
default = p: [
p.cabal-install
p.base
p.wai
p.text
p.hlint
p.ihp
];
};
appName = lib.mkOption {
description = ''
The derivation name.
'';
type = lib.types.str;
default = "app";
};
projectPath = lib.mkOption {
description = ''
Path to the IHP project. You likely want to set this to `./.`.
'';
type = lib.types.path;
};
withHoogle = lib.mkOption {
description = ''
Enable Hoogle support. Adds `hoogle` command to PATH.
'';
type = lib.types.bool;
default = false;
};
dontCheckPackages = lib.mkOption {
description = ''
List of Haskell package names whose tests are skipped during build.
'';
type = lib.types.listOf (lib.types.str);
default = [];
};
doJailbreakPackages = lib.mkOption {
description = ''
List of Haskell package names who are jailbreaked before build.
'';
type = lib.types.listOf (lib.types.str);
default = [];
};
dontHaddockPackages = lib.mkOption {
description = ''
List of Haskell package names whose haddock is not built during app build.
'';
type = lib.types.listOf (lib.types.str);
default = [];
};
rtsFlags = lib.mkOption {
description = ''
GHC RTS Flags used for compiled binaries (unoptimized-prod-server and optimized-prod-server)
'';
type = lib.types.str;
default = "-A96m -N";
};
optimizationLevel = lib.mkOption {
description = ''
With optimizationLevel = 2, will pass -O2 to GHC when compiling optimized-prod-server
'';
type = lib.types.str;
default = "1";
};
};
}
);
config = {
perSystem = { self', lib, pkgs, system, config, ... }: let
cfg = config.ihp;
ihp = ihpFlake.inputs.self;
ghcCompiler = import "${ihp}/NixSupport/mkGhcCompiler.nix" {
inherit pkgs;
inherit (cfg) ghcCompiler dontCheckPackages doJailbreakPackages dontHaddockPackages;
ihp = ihp;
haskellPackagesDir = cfg.projectPath + "/Config/nix/haskell-packages";
filter = ihpFlake.inputs.nix-filter.lib;
};
in lib.mkIf cfg.enable {
# release build package
packages = {
default = self'.packages.unoptimized-prod-server;
optimized-prod-server = import "${ihp}/NixSupport/default.nix" {
ihp = ihp;
haskellDeps = cfg.haskellPackages;
otherDeps = p: cfg.packages;
projectPath = cfg.projectPath;
# Set optimized = true to get more optimized binaries, but slower build times
optimized = true;
ghc = ghcCompiler;
pkgs = pkgs;
rtsFlags = cfg.rtsFlags;
optimizationLevel = cfg.optimizationLevel;
appName = cfg.appName;
filter = ihpFlake.inputs.nix-filter.lib;
};
unoptimized-prod-server = import "${ihp}/NixSupport/default.nix" {
ihp = ihp;
haskellDeps = cfg.haskellPackages;
otherDeps = p: cfg.packages;
projectPath = cfg.projectPath;
optimized = false;
ghc = ghcCompiler;
pkgs = pkgs;
rtsFlags = cfg.rtsFlags;
optimizationLevel = "0";
appName = cfg.appName;
filter = ihpFlake.inputs.nix-filter.lib;
};
unoptimized-docker-image = pkgs.dockerTools.buildImage {
name = "ihp-app";
config = { Cmd = [ "${self'.packages.unoptimized-prod-server}/bin/RunProdServer" ]; };
};
optimized-docker-image = pkgs.dockerTools.buildImage {
name = "ihp-app";
config = { Cmd = [ "${self'.packages.optimized-prod-server}/bin/RunProdServer" ]; };
};
migrate = pkgs.writeScriptBin "migrate" ''
${ghcCompiler.ihp}/bin/migrate
'';
ihp-schema = pkgs.stdenv.mkDerivation {
name = "ihp-schema";
src = ihp;
phases = [ "unpackPhase" "installPhase" ];
nativeBuildInputs = [ihp.ihp-ide];
installPhase = ''
mkdir $out
cp ${ihp.ihp-ide}/lib/IHP/IHPSchema.sql $out/
'';
allowedReferences = [];
};
schema = pkgs.stdenv.mkDerivation {
name = "schema";
src = cfg.projectPath;
phases = [ "unpackPhase" "installPhase" ];
installPhase = ''
mkdir $out
cp Application/Schema.sql $out/
'';
};
};
devenv.shells.default = lib.mkIf cfg.enable {
packages = [ ghcCompiler.ihp ghcCompiler.ihp-ide pkgs.gnumake ]
++ cfg.packages
++ [pkgs.mktemp] # Without this 'make build/bin/RunUnoptimizedProdServer' fails on macOS
++ [(let cfg = config.devenv.shells.default.services.postgres; in
if cfg.extensions != null
then
if builtins.hasAttr "withPackages" cfg.package
then cfg.package.withPackages cfg.extensions
else
builtins.throw ''
Cannot add extensions to the PostgreSQL package.
`services.postgres.package` is missing the `withPackages` attribute. Did you already add extensions to the package?
''
else cfg.package
)]
;
/*
we currently don't use devenv containers, and they break nix flake show
without the proper inputs set
https://github.com/cachix/devenv/issues/528
*/
containers = lib.mkForce {};
languages.haskell.enable = true;
languages.haskell.package = (if cfg.withHoogle
then ghcCompiler.ghc.withHoogle
else ghcCompiler.ghc.withPackages) cfg.haskellPackages;
languages.haskell.languageServer = ghcCompiler.haskell-language-server;
languages.haskell.stack = null; # Stack is not used in IHP
scripts.start.exec = ''
${ghcCompiler.ihp-ide}/bin/RunDevServer
'';
processes.devServer.exec = "start";
# Disabled for now
# Can be re-enabled once postgres is provided by devenv instead of IHP
# env.IHP_DEVENV = "1";
# env.DATABASE_URL = "postgres:///app?host=${config.env.PGHOST}";
# Disabled for now
# As the devenv postgres uses a different location for the socket
# this would break lots of known commands such as `make db`
services.postgres.enable = false;
services.postgres.package = pkgs.postgresql_13;
services.postgres.initialDatabases = [
{
name = "app";
schema = pkgs.runCommand "ihp-schema" {} ''
touch $out
echo "-- IHPSchema.sql" >> $out
echo "" >> $out
cat ${./lib/IHP/IHPSchema.sql} | sed -e s'/--.*//' | sed -e s'/$/\\/' >> $out
echo "" >> $out
echo "-- Application/Schema.sql" >> $out
echo "" >> $out
cat ${cfg.projectPath + "/Application/Schema.sql"} | sed -e s'/--.*//' | sed -e s'/$/\\/' >> $out
echo "" >> $out
echo "-- Application/Fixtures.sql" >> $out
echo "" >> $out
cat ${cfg.projectPath + "/Application/Fixtures.sql"} | sed -e s'/--.*//' | sed -e s'/$/\\/' >> $out
'';
}
];
env.IHP_LIB = "${ghcCompiler.ihp-ide}/lib/IHP";
env.IHP = "${ghcCompiler.ihp-ide}/lib/IHP"; # Used in the Makefile
scripts.deploy-to-nixos.exec = ''
if [[ $# -eq 0 || $1 == "--help" ]]; then
echo "usage: deploy-to-nixos <target-host>"
echo "example: deploy-to-nixos staging.example.com"
exit 0
fi
${pkgs.nixos-rebuild}/bin/nixos-rebuild switch \
-j auto \
--use-substitutes \
--fast \
--flake .#$1 \
--target-host $1 \
--build-host $1 \
--option sandbox false \
--option extra-substituters "https://digitallyinduced.cachix.org" \
--option extra-trusted-public-keys "digitallyinduced.cachix.org-1:y+wQvrnxQ+PdEsCt91rmvv39qRCYzEgGQaldK26hCKE="
ssh $1 systemctl start migrate
'';
};
};
# Nix requires this to be interactively allowed on first use for security reasons
# This can be automated by using the --accept-flake-config flag
# E.g. nix develop --accept-flake-config
# Or by putting accept-flake-config = true into the system's nix.conf
flake.nixConfig = {
extra-substituters = "https://digitallyinduced.cachix.org";
extra-trusted-public-keys = "digitallyinduced.cachix.org-1:y+wQvrnxQ+PdEsCt91rmvv39qRCYzEgGQaldK26hCKE=";
};
};
}