1{ system ? builtins.currentSystem
3, iosSdkVersion ? "10.2"
5, reflex-platform-func ? import ./dep/reflex-platform
8 reflex-platform = getReflexPlatform { inherit system; };
9 inherit (reflex-platform) hackGet nixpkgs;
12 inherit (import dep/gitignore.nix { inherit (nixpkgs) lib; }) gitignoreSource;
14 getReflexPlatform = { system, enableLibraryProfiling ? profiling }: reflex-platform-func {
15 inherit iosSdkVersion config system enableLibraryProfiling;
18 (import ./nixpkgs-overlays)
22 (import ./haskell-overlays/misc-deps.nix { inherit hackGet; })
23 pkgs.obeliskExecutableConfig.haskellOverlay
24 (import ./haskell-overlays/obelisk.nix)
25 (import ./haskell-overlays/tighten-ob-exes.nix)
29 # The haskell environment used to build Obelisk itself, e.g. the 'ob' command
30 ghcObelisk = reflex-platform.ghc;
32 # Development environments for obelisk packages.
33 ghcObeliskEnvs = pkgs.lib.mapAttrs (n: v: reflex-platform.workOn ghcObelisk v) ghcObelisk;
35 inherit (import ./lib/asset/assets.nix { inherit nixpkgs; }) mkAssets;
37 haskellLib = pkgs.haskell.lib;
40 inherit reflex-platform;
41 inherit (reflex-platform) nixpkgs pinBuildInputs;
42 inherit (nixpkgs) lib;
43 pathGit = ./.; # Used in CI by the migration graph hash algorithm to correctly ignore files.
44 path = reflex-platform.filterGit ./.;
46 obeliskEnvs = pkgs.lib.filterAttrs (k: _: pkgs.lib.strings.hasPrefix "obelisk-" k) ghcObeliskEnvs;
47 command = ghcObelisk.obelisk-command;
48 shell = pinBuildInputs "obelisk-shell" ([command] ++ command.commandRuntimeDeps);
50 selftest = pkgs.writeScript "selftest" ''
51 #!${pkgs.runtimeShell}
54 PATH="${command}/bin:$PATH"
56 "${ghcObelisk.obelisk-selftest}/bin/obelisk-selftest" +RTS -N -RTS "$@"
58 skeleton = pkgs.runCommand "skeleton" {
59 dir = builtins.filterSource (path: type: builtins.trace path (baseNameOf path != ".obelisk")) ./skeleton;
63 nullIfAbsent = p: if lib.pathExists p then p else null;
64 #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.
65 processAssets = { src, packageName ? "obelisk-generated-static", moduleName ? "Obelisk.Generated.Static" }: pkgs.runCommand "asset-manifest" {
67 outputs = [ "out" "haskellManifest" "symlinked" ];
68 nativeBuildInputs = [ ghcObelisk.obelisk-asset-manifest ];
73 obelisk-asset-manifest-generate "$src" "$haskellManifest" ${packageName} ${moduleName} "$symlinked"
76 compressedJs = frontend: optimizationLevel: pkgs.runCommand "compressedJs" {} ''
79 # TODO profiling + static shouldn't break and need an ad-hoc workaround like that
80 ln -s "${haskellLib.justStaticExecutables frontend}/bin/frontend.jsexe/all.js" all.unminified.js
81 ${if optimizationLevel == null then ''
82 ln -s all.unminified.js all.js
84 ${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
85 echo "//# sourceMappingURL=all.js.map" >> all.js
90 mkBaseEc2 = { nixosPkgs, ... }: {...}: {
92 (nixosPkgs.path + /nixos/modules/virtualisation/amazon-image.nix)
97 mkDefaultNetworking = { adminEmail, enableHttps, hostName, routeHost, ... }: {...}: {
100 firewall.allowedTCPPorts = if enableHttps then [ 80 443 ] else [ 80 ];
103 # `amazon-image.nix` already sets these but if the user provides their own module then
104 # forgetting these can cause them to lose access to the server!
105 # https://github.com/NixOS/nixpkgs/blob/fab05f17d15e4e125def4fd4e708d205b41d8d74/nixos/modules/virtualisation/amazon-image.nix#L133-L136
106 services.openssh.enable = true;
107 services.openssh.permitRootLogin = "prohibit-password";
109 security.acme.certs = if enableHttps then {
110 "${routeHost}".email = adminEmail;
122 , internalPort ? 8000
123 , backendArgs ? "--port=${toString internalPort}"
128 virtualHosts."${routeHost}" = {
129 enableACME = enableHttps;
130 forceSSL = enableHttps;
131 locations.${baseUrl} = {
132 proxyPass = "http://127.0.0.1:" + toString internalPort;
133 proxyWebsockets = true;
137 systemd.services.${name} = {
138 wantedBy = [ "multi-user.target" ];
139 after = [ "network.target" ];
140 restartIfChanged = true;
141 path = [ pkgs.gnutar ];
145 exec ./backend ${backendArgs} </dev/null
149 KillMode = "process";
150 WorkingDirectory = "~";
157 description = "${user} service";
158 home = "/var/lib/${user}";
163 groups.${group} = {};
170 serverExe = { backend, frontend, assets, optimizationLevel, version, bundledConfig }:
171 pkgs.runCommand "serverExe" {} ''
174 ln -s "${if profiling then backend else haskellLib.justStaticExecutables backend}"/bin/* $out/
175 ln -s "${mkAssets assets}" $out/static.assets
176 ln -s ${mkAssets (compressedJs frontend optimizationLevel)} $out/frontend.jsexe.assets
177 ${pkgs.lib.optionalString (bundledConfig != null) "ln -s ${bundledConfig} $out/config"}
178 echo ${version} > $out/version
181 mkNixOS = imports: import (pkgs.path + /nixos) {
182 system = "x86_64-linux";
183 configuration = { inherit imports; };
186 serverImports = args: [
187 (serverModules.mkDefaultNetworking args)
188 (serverModules.mkObeliskApp args)
191 server = { exe, hostName, adminEmail, routeHost, enableHttps, version, module ? serverModules.mkBaseEc2 }@args:
192 mkNixOS (serverImports args ++ [
193 (module { inherit exe hostName adminEmail routeHost enableHttps version; nixosPkgs = pkgs; })
196 mkVM = { exe, hostName, adminEmail, routeHost, enableHttps, version, module }@args:
197 mkNixOS (serverImports args ++ [
198 (pkgs.path + /nixos/modules/virtualisation/virtualbox-image.nix)
199 (module { inherit exe hostName adminEmail routeHost enableHttps version; nixosPkgs = pkgs; })
202 # An Obelisk project is a reflex-platform project with a predefined layout and role for each component
203 project = base': projectDefinition:
205 projectOut = { system, enableLibraryProfiling ? profiling }: let reflexPlatformProject = (getReflexPlatform { inherit system enableLibraryProfiling; }).project; in reflexPlatformProject (args@{ nixpkgs, ... }:
207 inherit (lib.strings) hasPrefix;
209 { android ? null #TODO: Better error when missing
210 , ios ? null #TODO: Better error when missing
212 , overrides ? _: _: {}
215 , shellToolOverrides ? _: _: {}
216 , withHoogle ? false # Setting this to `true` makes shell reloading far slower
217 , __closureCompilerOptimizationLevel ? "ADVANCED" # Set this to `null` to skip the closure-compiler step
218 , __withGhcide ? false
221 allConfig = nixpkgs.lib.makeExtensible (self: {
225 inherit android ios packages overrides tools shellToolOverrides withHoogle __closureCompilerOptimizationLevel __withGhcide;
226 staticFiles = if staticFiles == null then self.base + /static else staticFiles;
228 frontendName = "frontend";
229 backendName = "backend";
230 commonName = "common";
231 staticName = "obelisk-generated-static";
232 staticFilesImpure = let fs = self.userSettings.staticFiles; in if lib.isDerivation fs then fs else toString fs;
233 processedStatic = processAssets { src = self.userSettings.staticFiles; };
234 # The packages whose names and roles are defined by this package
235 predefinedPackages = lib.filterAttrs (_: x: x != null) {
236 ${self.frontendName} = nullIfAbsent (self.base + "/frontend");
237 ${self.commonName} = nullIfAbsent (self.base + "/common");
238 ${self.backendName} = nullIfAbsent (self.base + "/backend");
241 combinedPackages = self.predefinedPackages // self.userSettings.packages // self.shellPackages;
242 projectOverrides = self': super': {
243 ${self.staticName} = haskellLib.dontHaddock (self'.callCabal2nix self.staticName self.processedStatic.haskellManifest {});
244 ${self.backendName} = haskellLib.addBuildDepend super'.${self.backendName} self'.obelisk-run;
246 totalOverrides = lib.composeExtensions self.projectOverrides self.userSettings.overrides;
247 privateConfigDirs = ["config/backend"];
248 injectableConfig = builtins.filterSource (path: _:
249 !(lib.lists.any (x: hasPrefix (toString self.base + "/" + toString x) (toString path)) self.privateConfigDirs)
251 __androidWithConfig = configPath: {
252 ${if self.userSettings.android == null then null else self.frontendName} = {
253 executableName = "frontend";
254 ${if builtins.pathExists self.userSettings.staticFiles then "assets" else null} =
255 nixpkgs.obeliskExecutableConfig.platforms.android.inject
256 (self.injectableConfig configPath)
257 self.processedStatic.symlinked;
258 } // self.userSettings.android;
260 __iosWithConfig = configPath: {
261 ${if self.userSettings.ios == null then null else self.frontendName} = {
262 executableName = "frontend";
263 ${if builtins.pathExists self.userSettings.staticFiles then "staticSrc" else null} =
264 nixpkgs.obeliskExecutableConfig.platforms.ios.inject
265 (self.injectableConfig configPath)
266 self.processedStatic.symlinked;
267 } // self.userSettings.ios;
270 shells-ghc = builtins.attrNames (self.predefinedPackages // self.shellPackages);
277 shells-ghcSavedSplices = [
282 shellToolOverrides = lib.composeExtensions
283 self.userSettings.shellToolOverrides
284 (if self.userSettings.__withGhcide
285 then (import ./haskell-overlays/ghcide.nix)
289 project = reflexPlatformProject ({...}: self.projectConfig);
291 inherit (self) shellToolOverrides;
292 inherit (self.userSettings) tools withHoogle;
293 overrides = self.totalOverrides;
294 packages = self.combinedPackages;
296 ${if self.userSettings.android == null && self.userSettings.ios == null then null else "ghcSavedSplices"} =
297 lib.filter (x: lib.hasAttr x self.combinedPackages) self.shells-ghcSavedSplices;
298 ghc = lib.filter (x: lib.hasAttr x self.combinedPackages) self.shells-ghc;
299 ghcjs = lib.filter (x: lib.hasAttr x self.combinedPackages) self.shells-ghcjs;
301 android = self.__androidWithConfig (self.base + "/config");
302 ios = self.__iosWithConfig (self.base + "/config");
305 __unstable__.self = allConfig;
307 staticFilesImpure processedStatic
308 __iosWithConfig __androidWithConfig
310 inherit (self.userSettings)
311 android ios overrides packages shellToolOverrides staticFiles tools withHoogle
312 __closureCompilerOptimizationLevel
318 in (mkProject (projectDefinition args)).projectConfig);
319 mainProjectOut = projectOut { inherit system; };
320 serverOn = { projectInst, bundledConfig ? null, version } : serverExe {
321 inherit bundledConfig version;
322 inherit (projectInst.ghc) backend;
323 inherit (mainProjectOut.ghcjs) frontend;
324 assets = projectInst.passthru.staticFiles;
325 optimizationLevel = projectInst.passthru.__closureCompilerOptimizationLevel;
327 linuxExe = version: serverOn {
329 projectInst = projectOut { system = "x86_64-linux"; };
331 ovaExe = version: serverOn {
333 projectInst = projectOut { system = "x86_64-linux"; };
334 bundledConfig = base' + /config;
336 dummyVersion = "Version number is only available for deployments";
337 vm = args@{ hostName, adminEmail, routeHost, enableHttps, version, module ? (_:_:{}) }:
338 mkVM (args // { inherit module; exe = ovaExe version; });
340 in mainProjectOut // {
343 ova = args@{ hostName, adminEmail, routeHost, enableHttps, version, module ? (_: _: {}) }:
344 (vm args).config.system.build.virtualBoxOVA;
346 profiled = projectOut { inherit system; enableLibraryProfiling = true; };
347 exeSource = builtins.toFile "ob-run.hs" ''
350 import Control.Exception
351 import Reflex.Profiled
352 import System.Environment
354 import qualified Obelisk.Run
355 import qualified Frontend
356 import qualified Backend
361 let port = read $ args !! 0
363 profileFile = (args !! 2) <> ".rprof"
364 Obelisk.Run.run port (Obelisk.Run.runServeAsset assets) Backend.backend Frontend.frontend `finally` writeProfilingData profileFile
366 in nixpkgs.runCommand "ob-run" {
367 buildInputs = [ (profiled.ghc.ghcWithPackages (p: [ p.backend p.frontend])) ];
369 cp ${exeSource} $PWD/ob-run.hs
371 ghc -x hs -prof -fno-prof-auto -threaded ob-run.hs -o $out/bin/ob-run
375 linuxExeConfigurable = linuxExe;
376 linuxExe = linuxExe dummyVersion;
378 projectInst = mainProjectOut;
379 version = dummyVersion;
381 server = args@{ hostName, adminEmail, routeHost, enableHttps, version, module ? serverModules.mkBaseEc2 }:
382 server (args // { exe = linuxExe version; });
383 obelisk = import (base' + "/.obelisk/impl") {};
385 haskellPackageSets = {
386 inherit (reflex-platform) ghc ghcjs;
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list