on ls-expose-routeLink-events help sign in
obelisk/default.nix
363 lines 15 kB history raw
1{ system ? builtins.currentSystem
2, profiling ? false
3, iosSdkVersion ? "10.2"
4, config ? {}
5}:
6let
7 cleanSource = builtins.filterSource (name: _: let baseName = builtins.baseNameOf name; in !(
8 builtins.match "^\\.ghc\\.environment.*" baseName != null ||
9 baseName == "cabal.project.local"
10 ));
12 commandRuntimeDeps = pkgs: with pkgs; [
13 coreutils
14 git
15 nix-prefetch-git
16 openssh
17 ];
19 getReflexPlatform = sys: import ./dep/reflex-platform {
20 inherit iosSdkVersion config;
21 system = sys;
22 enableLibraryProfiling = profiling;
24 nixpkgsOverlays = [
25 (self: super: {
26 obeliskExecutableConfig = import ./lib/executable-config { nixpkgs = pkgs; filterGitSource = cleanSource; };
27 })
28 ];
30 haskellOverlays = [
32 # Fix misc upstream packages
33 (self: super: let
34 pkgs = self.callPackage ({ pkgs }: pkgs) {};
35 haskellLib = pkgs.haskell.lib;
36 in {
37 hnix = haskellLib.overrideCabal (self.callHackage "hnix" "0.6.1" { these = self.these_0_8; }) (_: {
38 jailbreak = true;
39 doCheck = false;
40 });
41 hnix-store-core = self.callHackage "hnix-store-core" "0.1.0.0" {};
43 ghcid = self.callCabal2nix "ghcid" (hackGet ./dep/ghcid) {};
44 })
46 pkgs.obeliskExecutableConfig.haskellOverlay
48 # Add obelisk packages
49 (self: super: let
50 pkgs = self.callPackage ({ pkgs }: pkgs) {};
51 onLinux = pkg: f: if pkgs.stdenv.isLinux then f pkg else pkg;
52 in {
53 obelisk-executable-config-inject = pkgs.obeliskExecutableConfig.platforms.web.inject self;
55 obelisk-asset-manifest = self.callCabal2nix "obelisk-asset-manifest" (cleanSource ./lib/asset/manifest) {};
56 obelisk-asset-serve-snap = self.callCabal2nix "obelisk-asset-serve-snap" (cleanSource ./lib/asset/serve-snap) {};
57 obelisk-backend = self.callCabal2nix "obelisk-backend" (cleanSource ./lib/backend) {};
58 obelisk-cliapp = self.callCabal2nix "obelisk-cliapp" (cleanSource ./lib/cliapp) {};
59 obelisk-command = haskellLib.overrideCabal (self.callCabal2nix "obelisk-command" (cleanSource ./lib/command) {}) {
60 librarySystemDepends = [
61 pkgs.nix
62 (haskellLib.justStaticExecutables self.ghcid)
63 ];
64 };
65 obelisk-frontend = self.callCabal2nix "obelisk-frontend" (cleanSource ./lib/frontend) {};
66 obelisk-run = onLinux (self.callCabal2nix "obelisk-run" (cleanSource ./lib/run) {}) (pkg:
67 haskellLib.overrideCabal pkg (drv: { librarySystemDepends = [ pkgs.iproute ]; })
68 );
69 obelisk-route = self.callCabal2nix "obelisk-route" (cleanSource ./lib/route) {};
70 obelisk-selftest = self.callCabal2nix "obelisk-selftest" (cleanSource ./lib/selftest) {};
71 obelisk-snap-extras = self.callCabal2nix "obelisk-snap-extras" (cleanSource ./lib/snap-extras) {};
72 tabulation = self.callCabal2nix "tabulation" (cleanSource ./lib/tabulation) {};
73 })
75 (self: super: let
76 pkgs = self.callPackage ({ pkgs }: pkgs) {};
77 haskellLib = pkgs.haskell.lib;
78 in {
79 # Dynamic linking with split objects dramatically increases startup time (about
80 # 0.5 seconds on a decent machine with SSD), so we do `justStaticExecutables`.
81 obelisk-command = haskellLib.overrideCabal
82 (haskellLib.generateOptparseApplicativeCompletion "ob"
83 (haskellLib.justStaticExecutables super.obelisk-command))
84 (drv: {
85 buildTools = (drv.buildTools or []) ++ [ pkgs.buildPackages.makeWrapper ];
86 postFixup = ''
87 ${drv.postFixup or ""}
88 # Make `ob` reference its runtime dependencies.
89 wrapProgram "$out"/bin/ob --prefix PATH : ${pkgs.lib.makeBinPath (commandRuntimeDeps pkgs)}
90 '';
91 });
92 obelisk-selftest = haskellLib.justStaticExecutables super.obelisk-selftest;
93 })
95 ];
96 };
98 reflex-platform = getReflexPlatform system;
99 inherit (reflex-platform) hackGet nixpkgs;
100 pkgs = nixpkgs;
102 # The haskell environment used to build Obelisk itself, e.g. the 'ob' command
103 ghcObelisk = reflex-platform.ghc;
105 # Development environments for obelisk packages.
106 ghcObeliskEnvs = pkgs.lib.mapAttrs (n: v: reflex-platform.workOn ghcObelisk v) ghcObelisk;
108 inherit (import ./lib/asset/assets.nix { inherit nixpkgs; }) mkAssets;
110 haskellLib = pkgs.haskell.lib;
112in rec {
113 inherit reflex-platform;
114 inherit (reflex-platform) nixpkgs pinBuildInputs;
115 inherit (nixpkgs) lib;
116 pathGit = ./.; # Used in CI by the migration graph hash algorithm to correctly ignore files.
117 path = reflex-platform.filterGit ./.;
118 obelisk = ghcObelisk;
119 obeliskEnvs = ghcObeliskEnvs;
120 command = ghcObelisk.obelisk-command;
121 shell = pinBuildInputs "obelisk-shell" ([command] ++ commandRuntimeDeps pkgs);
123 selftest = pkgs.writeScript "selftest" ''
124 #!/usr/bin/env bash
125 set -euo pipefail
127 PATH="${command}/bin:$PATH"
128 export OBELISK_IMPL="${hackGet ./.}"
129 "${ghcObelisk.obelisk-selftest}/bin/obelisk-selftest" +RTS -N -RTS "$@"
130 '';
131 #TODO: Why can't I build ./skeleton directly as a derivation? `nix-build -E ./.` doesn't work
132 skeleton = pkgs.runCommand "skeleton" {
133 dir = builtins.filterSource (path: type: builtins.trace path (baseNameOf path != ".obelisk")) ./skeleton;
134 } ''
135 ln -s "$dir" "$out"
136 '';
137 nullIfAbsent = p: if lib.pathExists p then p else null;
138 #TODO: Avoid copying files within the nix store. Right now, obelisk-asset-manifest-generate copies files into a big blob so that the android/ios static assets can be imported from there; instead, we should get everything lined up right before turning it into an APK, so that copies, if necessary, only exist temporarily.
139 processAssets = { src, packageName ? "obelisk-generated-static", moduleName ? "Obelisk.Generated.Static" }: pkgs.runCommand "asset-manifest" {
140 inherit src;
141 outputs = [ "out" "haskellManifest" "symlinked" ];
142 nativeBuildInputs = [ ghcObelisk.obelisk-asset-manifest ];
143 } ''
144 set -euo pipefail
145 touch "$out"
146 mkdir -p "$symlinked"
147 obelisk-asset-manifest-generate "$src" "$haskellManifest" ${packageName} ${moduleName} "$symlinked"
148 '';
150 compressedJs = frontend: optimizationLevel: pkgs.runCommand "compressedJs" {} ''
151 mkdir $out
152 cd $out
153 ln -s "${haskellLib.justStaticExecutables frontend}/bin/frontend.jsexe/all.js" all.unminified.js
154 ${if optimizationLevel == null then ''
155 ln -s all.unminified.js all.js
156 '' else ''
157 ${pkgs.closurecompiler}/bin/closure-compiler --externs "${reflex-platform.ghcjsExternsJs}" -O ${optimizationLevel} --jscomp_warning=checkVars --create_source_map="all.js.map" --source_map_format=V3 --js_output_file="all.js" all.unminified.js
158 echo "//# sourceMappingURL=all.js.map" >> all.js
159 ''}
160 '';
162 serverModules = {
163 mkBaseEc2 = { hostName, routeHost, enableHttps, adminEmail, ... }: {...}: {
164 imports = [
165 (pkgs.path + /nixos/modules/virtualisation/amazon-image.nix)
166 ];
167 networking = {
168 inherit hostName;
169 firewall.allowedTCPPorts = if enableHttps then [ 80 443 ] else [ 80 ];
170 };
171 security.acme.certs = if enableHttps then {
172 "${routeHost}".email = adminEmail;
173 } else {};
174 ec2.hvm = true;
175 };
177 mkObeliskApp =
178 { exe
179 , routeHost
180 , enableHttps
181 , name ? "backend"
182 , user ? name
183 , group ? user
184 , baseUrl ? "/"
185 , internalPort ? 8000
186 , backendArgs ? "--port=${toString internalPort}"
187 , ...
188 }: {...}: {
189 services.nginx = {
190 enable = true;
191 virtualHosts."${routeHost}" = {
192 enableACME = enableHttps;
193 forceSSL = enableHttps;
194 locations.${baseUrl} = {
195 proxyPass = "http://127.0.0.1:" + toString internalPort;
196 proxyWebsockets = true;
197 };
198 };
199 };
200 systemd.services.${name} = {
201 wantedBy = [ "multi-user.target" ];
202 after = [ "network.target" ];
203 restartIfChanged = true;
204 path = [ pkgs.gnutar ];
205 script = ''
206 ln -sft . '${exe}'/*
207 mkdir -p log
208 exec ./backend ${backendArgs} </dev/null
209 '';
210 serviceConfig = {
211 User = user;
212 KillMode = "process";
213 WorkingDirectory = "~";
214 Restart = "always";
215 RestartSec = 5;
216 };
217 };
218 users = {
219 users.${user} = {
220 description = "${user} service";
221 home = "/var/lib/${user}";
222 createHome = true;
223 isSystemUser = true;
224 group = group;
225 };
226 groups.${group} = {};
227 };
228 };
229 };
231 serverExe = backend: frontend: assets: optimizationLevel: version:
232 pkgs.runCommand "serverExe" {} ''
233 mkdir $out
234 set -eux
235 ln -s "${haskellLib.justStaticExecutables backend}"/bin/* $out/
236 ln -s "${mkAssets assets}" $out/static.assets
237 ln -s ${mkAssets (compressedJs frontend optimizationLevel)} $out/frontend.jsexe.assets
238 echo ${version} > $out/version
239 '';
241 server = { exe, hostName, adminEmail, routeHost, enableHttps, version }@args:
242 let
243 nixos = import (pkgs.path + /nixos);
244 in nixos {
245 system = "x86_64-linux";
246 configuration = {
247 imports = [
248 (serverModules.mkBaseEc2 args)
249 (serverModules.mkObeliskApp args)
250 ./acme.nix
251 ];
252 disabledModules = [
253 (pkgs.path + /nixos/modules/security/acme.nix)
254 ];
255 nixpkgs.overlays = [
256 (self: super: let
257 nixos1909 = import (hackGet ./dep/nixpkgs-19.09) {};
258 in {
259 inherit (nixos1909) simp_le;
260 })
261 ];
262 };
263 };
266 # An Obelisk project is a reflex-platform project with a predefined layout and role for each component
267 project = base: projectDefinition:
268 let projectOut = sys: (getReflexPlatform sys).project (args@{ nixpkgs, ... }:
269 let mkProject = { android ? null #TODO: Better error when missing
270 , ios ? null #TODO: Better error when missing
271 , packages ? {}
272 , overrides ? _: _: {}
273 , staticFiles ? base + /static
274 , tools ? _: []
275 , shellToolOverrides ? _: _: {}
276 , withHoogle ? false # Setting this to `true` makes shell reloading far slower
277 , __closureCompilerOptimizationLevel ? "ADVANCED" # Set this to `null` to skip the closure-compiler step
278 }:
279 let frontendName = "frontend";
280 backendName = "backend";
281 commonName = "common";
282 staticName = "obelisk-generated-static";
283 staticFilesImpure = if lib.isDerivation staticFiles then staticFiles else toString staticFiles;
284 processedStatic = processAssets { src = staticFiles; };
285 # The packages whose names and roles are defined by this package
286 predefinedPackages = lib.filterAttrs (_: x: x != null) {
287 ${frontendName} = nullIfAbsent (base + "/frontend");
288 ${commonName} = nullIfAbsent (base + "/common");
289 ${backendName} = nullIfAbsent (base + "/backend");
290 };
291 combinedPackages = predefinedPackages // packages;
292 projectOverrides = self: super: {
293 ${staticName} = haskellLib.dontHaddock (self.callCabal2nix staticName processedStatic.haskellManifest {});
294 ${backendName} = haskellLib.addBuildDepend super.${backendName} self.obelisk-run;
295 };
296 totalOverrides = lib.composeExtensions projectOverrides overrides;
297 inherit (lib.strings) hasPrefix;
298 privateConfigDirs = ["config/backend"];
299 injectableConfig = builtins.filterSource (path: _:
300 !(lib.lists.any (x: hasPrefix (toString base + "/" + toString x) (toString path)) privateConfigDirs)
301 );
302 __androidWithConfig = configPath: {
303 ${if android == null then null else frontendName} = {
304 executableName = "frontend";
305 ${if builtins.pathExists staticFiles then "assets" else null} =
306 nixpkgs.obeliskExecutableConfig.platforms.android.inject
307 (injectableConfig configPath)
308 processedStatic.symlinked;
309 } // android;
310 };
311 __iosWithConfig = configPath: {
312 ${if ios == null then null else frontendName} = {
313 executableName = "frontend";
314 ${if builtins.pathExists staticFiles then "staticSrc" else null} =
315 nixpkgs.obeliskExecutableConfig.platforms.ios.inject
316 (injectableConfig configPath)
317 processedStatic.symlinked;
318 } // ios;
319 };
320 in {
321 inherit shellToolOverrides tools withHoogle;
322 overrides = totalOverrides;
323 packages = combinedPackages;
324 shells = {
325 ${if android == null && ios == null then null else "ghcSavedSplices"} = (lib.filter (x: lib.hasAttr x combinedPackages) [
326 commonName
327 frontendName
328 ]);
329 ghc = (lib.filter (x: lib.hasAttr x combinedPackages) [
330 backendName
331 commonName
332 frontendName
333 ]);
334 ghcjs = lib.filter (x: lib.hasAttr x combinedPackages) [
335 frontendName
336 commonName
337 ];
338 };
339 android = __androidWithConfig (base + "/config");
340 ios = __iosWithConfig (base + "/config");
341 passthru = { inherit android ios packages overrides tools shellToolOverrides withHoogle staticFiles staticFilesImpure __closureCompilerOptimizationLevel processedStatic __iosWithConfig __androidWithConfig; };
342 };
343 in mkProject (projectDefinition args));
344 serverOn = sys: version: serverExe
345 (projectOut sys).ghc.backend
346 (projectOut system).ghcjs.frontend
347 (projectOut sys).passthru.staticFiles
348 (projectOut sys).passthru.__closureCompilerOptimizationLevel
349 version;
350 linuxExe = serverOn "x86_64-linux";
351 dummyVersion = "Version number is only available for deployments";
352 in projectOut system // {
353 linuxExeConfigurable = linuxExe;
354 linuxExe = linuxExe dummyVersion;
355 exe = serverOn system dummyVersion;
356 server = args@{ hostName, adminEmail, routeHost, enableHttps, version }:
357 server (args // { exe = linuxExe version; });
358 obelisk = import (base + "/.obelisk/impl") {};
359 };
360 haskellPackageSets = {
361 inherit (reflex-platform) ghc ghcjs;
362 };