-
Notifications
You must be signed in to change notification settings - Fork 0
/
flake.nix
257 lines (226 loc) · 8.61 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
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
{
description = "A garnix module for nodejs";
inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixos-24.11";
inputs.dream2nix = {
url = "github:jkarni/dream2nix";
inputs.nixpkgs.follows = "nixpkgs";
};
outputs =
{ self
, dream2nix
, nixpkgs
,
}:
let
lib = nixpkgs.lib;
webServerSubmodule.options = {
command = lib.mkOption {
type = lib.types.nonEmptyStr;
description = "The command to run to start the server in production";
example = "server --port 7000";
};
port = lib.mkOption {
type = lib.types.port;
description = "Port to forward incoming http requests to";
example = 7000;
};
path = lib.mkOption {
type = lib.types.nonEmptyStr;
description = "Path to host your rust server on";
default = "/";
};
};
nodejsSubmodule.options = {
src = lib.mkOption {
type = lib.types.path;
description = "A path to the directory containing package.json, package.lock, and src";
example = ./.;
};
prettier = lib.mkOption {
type = lib.types.bool;
description = "Whether to create a CI check with prettier, and add it to the devshells";
default = false;
};
devTools = lib.mkOption {
type = lib.types.listOf lib.types.package;
description = "A list of packages make available in the devshell for this project. This is useful for things like LSPs, formatters, etc.";
default = [ ];
};
buildDependencies = lib.mkOption {
type = lib.types.listOf lib.types.package;
description = "A list of dependencies required to build this package. They are made available in the devshell, and at build time";
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 = [ ];
};
testCommand = lib.mkOption {
type = lib.types.str;
description = "The command to run the test. Default: npm run test";
default = "npm run test";
};
webServer = lib.mkOption {
type = lib.types.nullOr (lib.types.submodule webServerSubmodule);
description = "If set, creates a server with this configuration.";
default = null;
};
};
in
{
garnixModules.default = { pkgs, config, ... }: {
options = {
nodejs = lib.mkOption {
type = lib.types.attrsOf (lib.types.submodule nodejsSubmodule);
description = "An attrset of rust projects to generate";
};
};
config =
let
theModule = projectConfig:
{ lib
, config
, dream2nix
, ...
}:
{
imports = [
dream2nix.modules.dream2nix.nodejs-package-lock-v3
dream2nix.modules.dream2nix.nodejs-granular-v3
];
mkDerivation = {
src = projectConfig.src;
buildInputs = projectConfig.buildDependencies;
};
deps = { nixpkgs, ... }: {
inherit
(nixpkgs)
fetchFromGitHub
stdenv
;
};
nodejs-package-lock-v3 = {
packageLockFile = "${config.mkDerivation.src}/package-lock.json";
};
name = "nodejs-app";
version = "0.1.0";
paths.projectRoot = ./.;
paths.projectRootFile = "flake.nix";
paths.package = ./.;
};
in
rec {
packages = builtins.mapAttrs
(name: projectConfig:
dream2nix.lib.evalModules {
packageSets.nixpkgs = pkgs;
modules = [
(theModule projectConfig)
];
}
)
config.nodejs;
checks = lib.foldlAttrs
(acc: name: projectConfig: acc //
{
"${name}-test" = pkgs.runCommand
"${name}-test"
{
buildInputs = [
pkgs.nodejs
] ++ projectConfig.buildDependencies;
}
''
GLOBIGNORE=".:.."
cp -r ${packages."${name}"}/lib/node_modules/nodejs-app/* .
chmod -R 755 .
export PATH=${packages."${name}"}/lib/node_modules/.bin:$PATH
# The .gitignore might be outside the dir. So we add some
# basic things since it influences e.g. ESLint
touch /build/.gitignore
echo build/ >> /build/.gitignore
${projectConfig.testCommand}
mkdir $out
'';
"${name}-prettier" = lib.mkIf projectConfig.prettier (pkgs.runCommand
"${name}-prettier"
{
buildInputs = [
pkgs.nodePackages.prettier
pkgs.coreutils
];
}
''
find ${projectConfig.src} -regex '.*\.\(js\|jsx\|ts\|tsx\)' |
xargs prettier --check
mkdir $out
''
);
})
{ }
config.nodejs;
devShells = builtins.mapAttrs
(name: projectConfig:
pkgs.mkShell {
inputsFrom = [ packages."${name}" ];
packages =
[ pkgs.nodejs ] ++
projectConfig.devTools ++
projectConfig.buildDependencies ++
projectConfig.runtimeDependencies ++
(if projectConfig.prettier then [ pkgs.nodePackages.prettier ] else [ ])
;
}
)
config.nodejs;
nixosConfigurations.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 = [ pkgs.nodejs ] ++
projectConfig.runtimeDependencies;
systemd.services.${name} =
let stateDirectoryBase = "${name}-nodejs-app/";
in {
description = "${name} nodejs garnix module";
wantedBy = [ "multi-user.target" ];
after = [ "network-online.target" ];
wants = [ "network-online.target" ];
serviceConfig = {
Type = "simple";
User = "nobody";
Group = "nobody";
StateDirectory = stateDirectoryBase;
WorkingDirectory = "${packages."${name}"}/lib/node_modules/nodejs-app/";
ExecStart = lib.getExe (pkgs.writeShellApplication {
name = "start-${name}";
runtimeInputs = [
pkgs.nodejs
pkgs.bash
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.nodejs);
};
};
};
}