on master help sign in
Merge branch 'master' into routes
commitf1d4c5ab4ad549336c9e11d965ec20e7fb3f4bdd authorLuigy Leon <luichi.19@gmail.com> dateThu 27 Sep 2018, 19:37 (8 years ago) parent8873de630ee14f
4 files changed+122−88
default.nix+92−59 lib/executable-config/lookup/src-android/Obelisk/ExecutableConfig.hs+1−1 lib/executable-config/lookup/src-ios/Obelisk/ExecutableConfig.hs+1−1 release.nix+28−27
default.nix+92−59
@@ -98,7 +98,7 @@ let
9898 obelisk-cliapp = self.callCabal2nix "obelisk-cliapp" (cleanSource ./lib/cliapp) {};
9999 obelisk-command = (self.callCabal2nix "obelisk-command" (cleanSource ./lib/command) {});
100100 obelisk-executable-config = executableConfig.haskellPackage self;
101− obelisk-executable-config-inject = executableConfig.platforms.web.inject self; # TODO handle platforms.{ios,android}
101+ obelisk-executable-config-inject = executableConfig.platforms.web.inject self;
102102 obelisk-frontend = self.callCabal2nix "obelisk-frontend" (cleanSource ./lib/frontend) {};
103103 obelisk-migration = self.callCabal2nix "obelisk-migration" (cleanSource ./lib/migration) {};
104104 obelisk-run = self.callCabal2nix "obelisk-run" (cleanSource ./lib/run) {};
@@ -120,8 +120,8 @@ rec {
120120 path = reflex-platform.filterGit ./.;
121121 obelisk = ghcObelisk;
122122 commandWithMigration = ghcObelisk.obelisk-command.overrideAttrs (drv: {
123− postInstall = (drv.postInstall or "") +
124− ''cp -r ${./migration} $out/migration;'';
123+ postInstall = (drv.postInstall or "") +
124+ ''cp -r ${./migration} $out/migration;'';
125125 });
126126 command = pkgs.runCommand commandWithMigration.name { nativeBuildInputs = [pkgs.makeWrapper]; } ''
127127 mkdir -p "$out/bin"
@@ -160,13 +160,80 @@ rec {
160160 '';
161161
162162 compressedJs = frontend: pkgs.runCommand "compressedJs" { buildInputs = [ pkgs.closurecompiler ]; } ''
163− mkdir $out
164− cd $out
165− ln -s "${justStaticExecutables frontend}/bin/frontend.jsexe/all.js" all.unminified.js
166− closure-compiler --externs "${reflex-platform.ghcjsExternsJs}" -O ADVANCED --jscomp_warning=checkVars --create_source_map="all.js.map" --source_map_format=V3 --js_output_file="all.js" all.unminified.js
167− echo "//# sourceMappingURL=all.js.map" >> all.js
163+ mkdir $out
164+ cd $out
165+ ln -s "${justStaticExecutables frontend}/bin/frontend.jsexe/all.js" all.unminified.js
166+ closure-compiler --externs "${reflex-platform.ghcjsExternsJs}" -O ADVANCED --jscomp_warning=checkVars --create_source_map="all.js.map" --source_map_format=V3 --js_output_file="all.js" all.unminified.js
167+ echo "//# sourceMappingURL=all.js.map" >> all.js
168168 '';
169169
170+ serverModules = {
171+ mkBaseEc2 = { hostName, routeHost, enableHttps, adminEmail, ... }: {...}: {
172+ imports = [
173+ (pkgs.path + /nixos/modules/virtualisation/amazon-image.nix)
174+ ];
175+ networking = {
176+ inherit hostName;
177+ firewall.allowedTCPPorts = if enableHttps then [ 80 443 ] else [ 80 ];
178+ };
179+ security.acme.certs = if enableHttps then {
180+ "${routeHost}".email = adminEmail;
181+ } else {};
182+ ec2.hvm = true;
183+ };
184+
185+ mkObeliskApp =
186+ { exe
187+ , routeHost
188+ , enableHttps
189+ , name ? "backend"
190+ , user ? name
191+ , group ? user
192+ , baseUrl ? "/"
193+ , internalPort ? 8000
194+ , backendArgs ? ""
195+ , ...
196+ }: {...}: {
197+ services.nginx = {
198+ enable = true;
199+ virtualHosts."${routeHost}" = {
200+ enableACME = enableHttps;
201+ forceSSL = enableHttps;
202+ locations.${baseUrl} = {
203+ proxyPass = "http://localhost:" + toString internalPort;
204+ };
205+ };
206+ };
207+ systemd.services.${name} = {
208+ wantedBy = [ "multi-user.target" ];
209+ after = [ "network.target" ];
210+ restartIfChanged = true;
211+ script = ''
212+ ln -sft . '${exe}'/*
213+ mkdir -p log
214+ exec ./backend --port=${toString internalPort} ${backendArgs} >>backend.out 2>>backend.err </dev/null
215+ '';
216+ serviceConfig = {
217+ User = user;
218+ KillMode = "process";
219+ WorkingDirectory = "~";
220+ Restart = "always";
221+ RestartSec = 5;
222+ };
223+ };
224+ users = {
225+ users.${user} = {
226+ description = "${user} service";
227+ home = "/var/lib/${user}";
228+ createHome = true;
229+ isSystemUser = true;
230+ group = group;
231+ };
232+ groups.${group} = {};
233+ };
234+ };
235+ };
236+
170237 serverExe = backend: frontend: assets: config:
171238 pkgs.runCommand "serverExe" {} ''
172239 mkdir $out
@@ -177,58 +244,16 @@ rec {
177244 ln -s ${mkAssets (compressedJs frontend)} $out/frontend.jsexe.assets
178245 '';
179246
180− server = { exe, hostName, adminEmail, routeHost, enableHttps, ... }:
181− let system = "x86_64-linux";
182− nixos = import (pkgs.path + /nixos);
183− backendPort = 8000;
247+ server = { exe, hostName, adminEmail, routeHost, enableHttps }@args:
248+ let
249+ nixos = import (pkgs.path + /nixos);
184250 in nixos {
185− inherit system;
186− configuration = args: {
251+ system = "x86_64-linux";
252+ configuration = {
187253 imports = [
188− (pkgs.path + /nixos/modules/virtualisation/amazon-image.nix)
254+ (serverModules.mkBaseEc2 args)
255+ (serverModules.mkObeliskApp args)
189256 ];
190− networking = {
191− inherit hostName;
192− firewall.allowedTCPPorts = if enableHttps then [ 80 443 ] else [ 80 ];
193− };
194− services.nginx = {
195− enable = true;
196− virtualHosts."${routeHost}" = {
197− enableACME = enableHttps;
198− forceSSL = enableHttps;
199− locations."/" = {
200− proxyPass = "http://127.0.0.1:" + toString backendPort;
201− };
202− };
203− };
204− security.acme.certs = if enableHttps then {
205− "${routeHost}".email = adminEmail;
206− } else { };
207− systemd.services.backend = {
208− wantedBy = [ "multi-user.target" ];
209− after = [ "network.target" ];
210− restartIfChanged = true;
211− script = ''
212− ln -sft . "${exe}"/*
213− mkdir -p log
214− exec ./backend >>backend.out 2>>backend.err </dev/null
215− '';
216− serviceConfig = {
217− User = "backend";
218− KillMode = "process";
219− WorkingDirectory = "~";
220− Restart = "always";
221− RestartSec = 5;
222− };
223− };
224− users.extraUsers.backend = {
225− description = "backend service";
226− home = "/var/lib/backend";
227− createHome = true;
228− isSystemUser = true;
229− group = "backend";
230− };
231− ec2.hvm = true;
232257 };
233258 };
234259
@@ -264,6 +289,12 @@ rec {
264289 ${backendName} = addBuildDepend super.${backendName} self.obelisk-run;
265290 };
266291 totalOverrides = composeExtensions (composeExtensions defaultHaskellOverrides projectOverrides) overrides;
292+ inherit (nixpkgs) lib;
293+ inherit (lib.strings) hasPrefix;
294+ privateConfigDirs = ["config/backend"];
295+ injectableConfig = builtins.filterSource (path: _:
296+ !(lib.lists.any (x: hasPrefix (toString base + "/" + toString x) (toString path)) privateConfigDirs)
297+ ) configPath;
267298 in {
268299 inherit shellToolOverrides tools withHoogle;
269300 overrides = totalOverrides;
@@ -282,13 +313,15 @@ rec {
282313 android = {
283314 ${if android == null then null else frontendName} = {
284315 executableName = "frontend";
285− ${if builtins.pathExists staticPath then "assets" else null} = processedStatic.symlinked;
316+ ${if builtins.pathExists staticPath then "assets" else null} =
317+ executableConfig.platforms.android.inject injectableConfig processedStatic.symlinked;
286318 } // android;
287319 };
288320 ios = {
289321 ${if ios == null then null else frontendName} = {
290322 executableName = "frontend";
291− ${if builtins.pathExists staticPath then "staticSrc" else null} = processedStatic.symlinked;
323+ ${if builtins.pathExists staticPath then "staticSrc" else null} =
324+ executableConfig.platforms.ios.inject injectableConfig processedStatic.symlinked;
292325 } // ios;
293326 };
294327 };
lib/executable-config/lookup/src-android/Obelisk/ExecutableConfig.hs+1−1
@@ -15,7 +15,7 @@ get :: Text -> IO (Maybe Text)
1515get name = bracket getAssets freeAssetManager $ \mgrObj -> do
1616 mgr <- assetManagerFromJava mgrObj
1717 let open = do
18− a <- withCString ("config" </> T.unpack name) $ \fn ->
18+ a <- withCString (T.unpack name) $ \fn ->
1919 assetManager_open mgr fn 3
2020 return $ if unAAsset a == nullPtr
2121 then Nothing
lib/executable-config/lookup/src-ios/Obelisk/ExecutableConfig.hs+1−1
@@ -14,7 +14,7 @@ import System.IO.Error
1414
1515get :: Text -> IO (Maybe Text)
1616get name = fmap join $ mainBundleResourcePath >>= \mp -> forM mp $ \p ->
17− catchDoesNotExist $ T.readFile $ T.unpack (T.decodeUtf8 p) </> "config" </> T.unpack name
17+ catchDoesNotExist $ T.readFile $ T.unpack (T.decodeUtf8 p) </> T.unpack name
1818
1919catchDoesNotExist :: IO a -> IO (Maybe a)
2020catchDoesNotExist f = catchJust doesNotExist (Just <$> f) $ const $ return Nothing
release.nix+28−27
@@ -1,12 +1,12 @@
1−{ self ? import ./. {}
2−, pkgs ? self.nixpkgs
1+{ local-self ? import ./. {}
2+, local-pkgs ? local-self.nixpkgs
33}:
44
5−with pkgs;
5+with local-pkgs.lib;
66
7−{
8− inherit (self) command;
9− built-skeleton = (import ./skeleton {}).all;
7+genAttrs [ "x86_64-linux" "x86_64-darwin" ] (system: {
8+ inherit (import ./. { inherit system; }) command;
9+ built-skeleton = (import ./skeleton { inherit system; }).all;
1010 tests = {
1111 #TODO: Doesn't work; see discussion in https://www.pivotaltracker.com/story/show/157265140
1212 # ob-init = runCommand "ob-init" {
@@ -20,25 +20,26 @@ with pkgs;
2020 # # ob init --symlink=${self.path}
2121 #'';
2222
23− verify-migration = runCommand "verify-migration" {
24− nativeBuildInputs = [
25− self.command
26− nix # obelisk uses `nix-hash`
27− ];
28− } ''
29− set -e
30− # Fix permissions so `removePathForcibly` works on copied-to-tmp paths
31− cp -a ${self.pathGit} $TMPDIR/obelisk
32− cd $TMPDIR/obelisk
33− chmod -R u+w .
34−
35− (set -x;
36− ob internal hash;
37− ob internal verify-migration;
38− )
39−
40− mkdir -p $out
41− touch $out/done
42− '';
23+ #TODO: Re-enable this
24+ #verify-migration = runCommand "verify-migration" {
25+ # nativeBuildInputs = [
26+ # self.command
27+ # nix # obelisk uses `nix-hash`
28+ # ];
29+ #} ''
30+ # set -e
31+ # # Fix permissions so `removePathForcibly` works on copied-to-tmp paths
32+ # cp -a ${self.pathGit} $TMPDIR/obelisk
33+ # cd $TMPDIR/obelisk
34+ # chmod -R u+w .
35+ #
36+ # (set -x;
37+ # ob internal hash;
38+ # ob internal verify-migration;
39+ # )
40+ #
41+ # mkdir -p $out
42+ # touch $out/done
43+ #'';
4344 };
44−}
45+})