on cg-tabulation-nodevelop help sign in
obelisk/default.nix
363 lines 16 kB history raw
1{ system ? builtins.currentSystem
2, profiling ? false
3, iosSdkVersion ? "10.2"
4, __useLegacyCompilers ? false
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 = getReflexPlatform' __useLegacyCompilers;
20 getReflexPlatform' = __useLegacyCompilers: sys: import ./dep/reflex-platform {
21 inherit iosSdkVersion __useLegacyCompilers;
22 system = sys;
23 enableLibraryProfiling = profiling;
25 nixpkgsOverlays = [
26 (self: super: {
27 obeliskExecutableConfig = import ./lib/executable-config { nixpkgs = pkgs; filterGitSource = cleanSource; };
28 })
29 ];
31 haskellOverlays = [
33 # Fix misc upstream packages
34 (self: super: let
35 pkgs = self.callPackage ({ pkgs }: pkgs) {};
36 in {
37 hnix = pkgs.haskell.lib.dontCheck (self.callCabal2nix "hnix" (pkgs.fetchFromGitHub {
38 owner = "haskell-nix";
39 repo = "hnix";
40 rev = "42afdc21da5d9e076eab57eaa42bfdde938192b8";
41 sha256 = "0psw384dx9bw2dp93xrzw8rd9amvcwgzn64jzzwby7sfspj6k349";
42 }) {});
43 # Need 8.0.2 build support
44 # PR: https://github.com/dmwit/universe/pull/33
45 universe-template = self.callCabal2nix "universe-template" (pkgs.fetchFromGitHub {
46 owner = "obsidiansystems";
47 repo = "universe";
48 rev = "6a71119bfa5db2b9990a2491c941469ff8ef5d13";
49 sha256 = "0z8smyainnlzcglv3dlx6x1n9j6d2jv48aa8f2421iayfkxg3js5";
50 } + /template) {};
51 })
53 # Add obelisk packages
54 (self: super: let
55 pkgs = self.callPackage ({ pkgs }: pkgs) {};
56 in {
57 obelisk-executable-config = pkgs.obeliskExecutableConfig.haskellPackage self;
58 obelisk-executable-config-inject = pkgs.obeliskExecutableConfig.platforms.web.inject self;
60 obelisk-asset-manifest = self.callCabal2nix "obelisk-asset-manifest" (hackGet ./lib/asset + "/manifest") {};
61 obelisk-asset-serve-snap = self.callCabal2nix "obelisk-asset-serve-snap" (hackGet ./lib/asset + "/serve-snap") {};
62 obelisk-backend = self.callCabal2nix "obelisk-backend" (cleanSource ./lib/backend) {};
63 obelisk-cliapp = self.callCabal2nix "obelisk-cliapp" (cleanSource ./lib/cliapp) {};
64 obelisk-command = self.callCabal2nix "obelisk-command" (cleanSource ./lib/command) {};
65 obelisk-frontend = self.callCabal2nix "obelisk-frontend" (cleanSource ./lib/frontend) {};
66 obelisk-run = self.callCabal2nix "obelisk-run" (cleanSource ./lib/run) {};
67 obelisk-route = self.callCabal2nix "obelisk-route" (cleanSource ./lib/route) {};
68 obelisk-selftest = self.callCabal2nix "obelisk-selftest" (cleanSource ./lib/selftest) {};
69 obelisk-snap-extras = self.callCabal2nix "obelisk-snap-extras" (cleanSource ./lib/snap-extras) {};
70 tabulation = self.callCabal2nix "tabulation" (cleanSource ./lib/tabulation) {};
71 })
73 (self: super: let
74 pkgs = self.callPackage ({ pkgs }: pkgs) {};
75 haskellLib = pkgs.haskell.lib;
76 #TODO: Upstream
77 # Modify a Haskell package to add completion scripts for the given
78 # executable produced by it. These completion scripts will be picked up
79 # automatically if the resulting derivation is installed, e.g. by
80 # `nix-env -i`.
81 addOptparseApplicativeCompletionScripts = exeName: pkg: haskellLib.overrideCabal pkg (drv: {
82 postInstall = (drv.postInstall or "") + ''
83 BASH_COMP_DIR="$out/share/bash-completion/completions"
84 mkdir -p "$BASH_COMP_DIR"
85 "$out/bin/${exeName}" --bash-completion-script "$out/bin/${exeName}" >"$BASH_COMP_DIR/ob"
87 ZSH_COMP_DIR="$out/share/zsh/vendor-completions"
88 mkdir -p "$ZSH_COMP_DIR"
89 "$out/bin/${exeName}" --zsh-completion-script "$out/bin/${exeName}" >"$ZSH_COMP_DIR/_ob"
91 FISH_COMP_DIR="$out/share/fish/vendor_completions.d"
92 mkdir -p "$FISH_COMP_DIR"
93 "$out/bin/${exeName}" --fish-completion-script "$out/bin/${exeName}" >"$FISH_COMP_DIR/ob.fish"
94 '';
95 });
96 in {
97 # Dynamic linking with split objects dramatically increases startup time (about
98 # 0.5 seconds on a decent machine with SSD), so we do `justStaticExecutables`.
99 obelisk-command = haskellLib.overrideCabal
100 (addOptparseApplicativeCompletionScripts "ob"
101 (haskellLib.justStaticExecutables super.obelisk-command))
102 (drv: {
103 buildTools = (drv.buildTools or []) ++ [ pkgs.buildPackages.makeWrapper ];
104 postFixup = ''
105 ${drv.postFixup or ""}
106 # Make `ob` reference its runtime dependencies.
107 wrapProgram "$out"/bin/ob --prefix PATH : ${pkgs.lib.makeBinPath (commandRuntimeDeps pkgs)}
108 '';
109 });
110 obelisk-selftest = haskellLib.justStaticExecutables super.obelisk-selftest;
111 })
113 ];
114 };
116 reflex-platform = getReflexPlatform' false system;
117 inherit (reflex-platform) hackGet nixpkgs;
118 pkgs = nixpkgs;
120 # The haskell environment used to build Obelisk itself, e.g. the 'ob' command
121 ghcObelisk = reflex-platform.ghc;
123 # Development environments for obelisk packages.
124 ghcObeliskEnvs = pkgs.lib.mapAttrs (n: v: reflex-platform.workOn ghcObelisk v) ghcObelisk;
126 inherit (import ./lib/asset/assets.nix { inherit nixpkgs; }) mkAssets;
128 haskellLib = pkgs.haskell.lib;
130in rec {
131 inherit reflex-platform;
132 inherit (reflex-platform) nixpkgs pinBuildInputs;
133 inherit (nixpkgs) lib;
134 pathGit = ./.; # Used in CI by the migration graph hash algorithm to correctly ignore files.
135 path = reflex-platform.filterGit ./.;
136 obelisk = ghcObelisk;
137 obeliskEnvs = ghcObeliskEnvs;
138 command = ghcObelisk.obelisk-command;
139 shell = pinBuildInputs "obelisk-shell" ([command] ++ commandRuntimeDeps pkgs) [];
141 selftest = pkgs.writeScript "selftest" ''
142 #!/usr/bin/env bash
143 set -euo pipefail
145 PATH="${command}/bin:$PATH"
146 export OBELISK_IMPL="${hackGet ./.}"
147 "${ghcObelisk.obelisk-selftest}/bin/obelisk-selftest" +RTS -N -RTS "$@"
148 '';
149 #TODO: Why can't I build ./skeleton directly as a derivation? `nix-build -E ./.` doesn't work
150 skeleton = pkgs.runCommand "skeleton" {
151 dir = builtins.filterSource (path: type: builtins.trace path (baseNameOf path != ".obelisk")) ./skeleton;
152 } ''
153 ln -s "$dir" "$out"
154 '';
155 nullIfAbsent = p: if lib.pathExists p then p else null;
156 #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.
157 processAssets = { src, packageName ? "obelisk-generated-static", moduleName ? "Obelisk.Generated.Static" }: pkgs.runCommand "asset-manifest" {
158 inherit src;
159 outputs = [ "out" "haskellManifest" "symlinked" ];
160 nativeBuildInputs = [ ghcObelisk.obelisk-asset-manifest ];
161 } ''
162 set -euo pipefail
163 touch "$out"
164 mkdir -p "$symlinked"
165 obelisk-asset-manifest-generate "$src" "$haskellManifest" ${packageName} ${moduleName} "$symlinked"
166 '';
168 compressedJs = frontend: optimizationLevel: pkgs.runCommand "compressedJs" { buildInputs = [ pkgs.closurecompiler ]; } ''
169 mkdir $out
170 cd $out
171 ln -s "${haskellLib.justStaticExecutables frontend}/bin/frontend.jsexe/all.js" all.unminified.js
172 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
173 echo "//# sourceMappingURL=all.js.map" >> all.js
174 '';
176 serverModules = {
177 mkBaseEc2 = { hostName, routeHost, enableHttps, adminEmail, ... }: {...}: {
178 imports = [
179 (pkgs.path + /nixos/modules/virtualisation/amazon-image.nix)
180 ];
181 networking = {
182 inherit hostName;
183 firewall.allowedTCPPorts = if enableHttps then [ 80 443 ] else [ 80 ];
184 };
185 security.acme.certs = if enableHttps then {
186 "${routeHost}".email = adminEmail;
187 } else {};
188 ec2.hvm = true;
189 };
191 mkObeliskApp =
192 { exe
193 , routeHost
194 , enableHttps
195 , name ? "backend"
196 , user ? name
197 , group ? user
198 , baseUrl ? "/"
199 , internalPort ? 8000
200 , backendArgs ? "--port=${toString internalPort}"
201 , ...
202 }: {...}: {
203 services.nginx = {
204 enable = true;
205 virtualHosts."${routeHost}" = {
206 enableACME = enableHttps;
207 forceSSL = enableHttps;
208 locations.${baseUrl} = {
209 proxyPass = "http://localhost:" + toString internalPort;
210 proxyWebsockets = true;
211 };
212 };
213 };
214 systemd.services.${name} = {
215 wantedBy = [ "multi-user.target" ];
216 after = [ "network.target" ];
217 restartIfChanged = true;
218 script = ''
219 ln -sft . '${exe}'/*
220 mkdir -p log
221 exec ./backend ${backendArgs} >>backend.out 2>>backend.err </dev/null
222 '';
223 serviceConfig = {
224 User = user;
225 KillMode = "process";
226 WorkingDirectory = "~";
227 Restart = "always";
228 RestartSec = 5;
229 };
230 };
231 users = {
232 users.${user} = {
233 description = "${user} service";
234 home = "/var/lib/${user}";
235 createHome = true;
236 isSystemUser = true;
237 group = group;
238 };
239 groups.${group} = {};
240 };
241 };
242 };
244 serverExe = backend: frontend: assets: optimizationLevel: version:
245 pkgs.runCommand "serverExe" {} ''
246 mkdir $out
247 set -eux
248 ln -s "${haskellLib.justStaticExecutables backend}"/bin/* $out/
249 ln -s "${mkAssets assets}" $out/static.assets
250 ln -s ${mkAssets (compressedJs frontend optimizationLevel)} $out/frontend.jsexe.assets
251 echo ${version} > $out/version
252 '';
254 server = { exe, hostName, adminEmail, routeHost, enableHttps, version }@args:
255 let
256 nixos = import (pkgs.path + /nixos);
257 in nixos {
258 system = "x86_64-linux";
259 configuration = {
260 imports = [
261 (serverModules.mkBaseEc2 args)
262 (serverModules.mkObeliskApp args)
263 ];
264 };
265 };
268 # An Obelisk project is a reflex-platform project with a predefined layout and role for each component
269 project = base: projectDefinition:
270 let projectOut = sys: (getReflexPlatform sys).project (args@{ nixpkgs, ... }:
271 let mkProject = { android ? null #TODO: Better error when missing
272 , ios ? null #TODO: Better error when missing
273 , packages ? {}
274 , overrides ? _: _: {}
275 , staticFiles ? base + /static
276 , tools ? _: []
277 , shellToolOverrides ? _: _: {}
278 , withHoogle ? false # Setting this to `true` makes shell reloading far slower
279 , __closureCompilerOptimizationLevel ? "ADVANCED"
280 }:
281 let frontendName = "frontend";
282 backendName = "backend";
283 commonName = "common";
284 staticName = "obelisk-generated-static";
285 staticFilesImpure = if lib.isDerivation staticFiles then staticFiles else toString staticFiles;
286 processedStatic = processAssets { src = staticFiles; };
287 # The packages whose names and roles are defined by this package
288 predefinedPackages = lib.filterAttrs (_: x: x != null) {
289 ${frontendName} = nullIfAbsent (base + "/frontend");
290 ${commonName} = nullIfAbsent (base + "/common");
291 ${backendName} = nullIfAbsent (base + "/backend");
292 };
293 combinedPackages = predefinedPackages // packages;
294 projectOverrides = self: super: {
295 ${staticName} = haskellLib.dontHaddock (self.callCabal2nix staticName processedStatic.haskellManifest {});
296 ${backendName} = haskellLib.addBuildDepend super.${backendName} self.obelisk-run;
297 };
298 totalOverrides = lib.composeExtensions projectOverrides overrides;
299 inherit (lib.strings) hasPrefix;
300 privateConfigDirs = ["config/backend"];
301 injectableConfig = builtins.filterSource (path: _:
302 !(lib.lists.any (x: hasPrefix (toString base + "/" + toString x) (toString path)) privateConfigDirs)
303 );
304 __android = configPath: {
305 ${if android == null then null else frontendName} = {
306 executableName = "frontend";
307 ${if builtins.pathExists staticFiles then "assets" else null} =
308 (if configPath == null then lib.id else nixpkgs.obeliskExecutableConfig.platforms.android.inject (injectableConfig configPath))
309 processedStatic.symlinked;
310 } // android;
311 };
312 __ios = configPath: {
313 ${if ios == null then null else frontendName} = {
314 executableName = "frontend";
315 ${if builtins.pathExists staticFiles then "staticSrc" else null} =
316 (if configPath == null then lib.id else nixpkgs.obeliskExecutableConfig.platforms.ios.inject (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 = __android null;
340 ios = __ios null;
341 passthru = { inherit android ios packages overrides tools shellToolOverrides withHoogle staticFiles staticFilesImpure __closureCompilerOptimizationLevel processedStatic __ios __android; };
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 };