-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflake.nix
203 lines (185 loc) · 7.32 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
{
description = ''
A garnix module for projects using Haskell.
Add dependencies, pick the GHC version, and optionally deploy a web server.
[Documentation](https://garnix.io/docs/modules/haskell) - [Source](https://github.com/garnix-io/haskell-module).
'';
outputs =
{ self }:
{
garnixModules.default =
{
pkgs,
lib,
config,
...
}:
let
webServerSubmodule.options = {
command =
lib.mkOption {
type = lib.types.nonEmptyStr;
description = "The command to run to start the server in production.";
example = "server --port \"$PORT\"";
}
// {
name = "server command";
};
port = lib.mkOption {
type = lib.types.port;
description = "Port to forward incoming HTTP requests to. This port has to be opened by the server command. This also sets the PORT environment variable for the server command.";
example = 7000;
default = 7000;
};
path =
lib.mkOption {
type = lib.types.nonEmptyStr;
description = "URL path your Haskell server will be hosted on.";
default = "/";
}
// {
name = "API path";
};
};
haskellSubmodule.options = {
src =
lib.mkOption {
type = lib.types.path;
description = "A path to the directory containing your cabal or hpack (`package.yaml`) file.";
example = "./.";
}
// {
name = "source directory";
};
ghcVersion =
lib.mkOption {
type = lib.types.enum [
"9.10"
"9.8"
"9.6"
"9.4"
"9.2"
"9.0"
];
description = "The major GHC version to use.";
default = "9.8";
}
// {
name = "GHC version";
};
webServer = lib.mkOption {
type = lib.types.nullOr (lib.types.submodule webServerSubmodule);
description = "Whether to build a server configuration based on this project and deploy it to the garnix cloud.";
default = null;
};
devTools =
lib.mkOption {
type = lib.types.listOf lib.types.package;
description = "A list of packages make available in the devshell for this project (and `default` devshell). This is useful for things like LSPs, formatters, etc.";
default = [ ];
}
// {
name = "development tools";
};
buildDependencies = lib.mkOption {
type = lib.types.listOf lib.types.package;
description = ''
A list of additional dependencies required to build this package. They are made available in the devshell, and at build time.
(It's not necessary to include library dependencies manually, these will be included automatically.)
'';
default = [ ];
};
runtimeDependencies = lib.mkOption {
type = lib.types.listOf lib.types.package;
description = "A list of dependencies required at runtime. They are made available in the devshell, at build time, and are available on the server at runtime.";
default = [ ];
};
};
ghcStr = ghc: "ghc${builtins.replaceStrings [ "." ] [ "" ] ghc}";
in
{
options = {
haskell = lib.mkOption {
type = lib.types.attrsOf (lib.types.submodule haskellSubmodule);
description = "An attrset of Haskell projects to generate.";
};
};
config = {
packages = builtins.mapAttrs (
name: projectConfig:
pkgs.haskell.packages."${ghcStr projectConfig.ghcVersion}".callCabal2nix "haskell-${name}"
projectConfig.src
{ }
) config.haskell;
devShells = builtins.mapAttrs (
name: projectConfig:
let
haskellPackages = pkgs.haskell.packages."${ghcStr projectConfig.ghcVersion}";
in
pkgs.mkShell {
inputsFrom = [
(haskellPackages.callCabal2nix "haskell-${name}" projectConfig.src { }).env
];
buildInputs = [
haskellPackages.cabal-install
haskellPackages.hpack
];
}
) config.haskell;
nixosConfigurations =
let
hasAnyWebServer = builtins.any (projectConfig: projectConfig.webServer != null) (
builtins.attrValues config.haskell
);
in
lib.mkIf hasAnyWebServer {
default =
# Global NixOS configuration
[
{
services.nginx = {
enable = true;
recommendedProxySettings = true;
recommendedOptimisation = true;
virtualHosts.default = {
default = true;
};
};
networking.firewall.allowedTCPPorts = [ 80 ];
}
]
++
# Per project NixOS configuration
(builtins.attrValues (
builtins.mapAttrs (
name: projectConfig:
lib.mkIf (projectConfig.webServer != null) {
environment.systemPackages = projectConfig.runtimeDependencies;
systemd.services.${name} = {
description = "${name} Haskell garnix module";
wantedBy = [ "multi-user.target" ];
after = [ "network-online.target" ];
wants = [ "network-online.target" ];
environment.PORT = toString projectConfig.webServer.port;
serviceConfig = {
Type = "simple";
DynamicUser = true;
ExecStart = lib.getExe (
pkgs.writeShellApplication {
name = "start-${name}";
runtimeInputs = [ config.packages.${name} ] ++ projectConfig.runtimeDependencies;
text = projectConfig.webServer.command;
}
);
};
};
services.nginx.virtualHosts.default.locations.${projectConfig.webServer.path}.proxyPass =
"http://localhost:${toString projectConfig.webServer.port}";
}
) config.haskell
));
};
};
};
};
}