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