on v0.1.0.0 help sign in
obelisk/README.md
301 lines 13 kB history raw
1# Obelisk
3Obelisk provides an easy way to develop and deploy your [Reflex](https://github.com/reflex-frp/reflex) project as web apps and as mobile apps.
5- [Installing Obelisk](#installing-obelisk)
6- [Developing an Obelisk project](#developing-an-obelisk-project)
7 - [Hoogle](#hoogle)
8 - [Adding Package Overrides](#adding-package-overrides)
9- [Deploying](#deploying)
10 - [Locally](#locally)
11 - [EC2](#ec2)
12 - [From macOS](#from-macos)
13 - [Deploying an updated version](#deploying-an-updated-version)
14- [Mobile](#mobile)
15 - [iOS](#ios)
16 - [Android](#android)
18## Installing Obelisk
191. [Install Nix](https://nixos.org/nix/).
20 If you already have Nix installed, make sure you have version 2.0 or higher. To check your current version, run `nix-env --version`.
211. Set up nix caches
22 1. If you are running NixOS, add this to `/etc/nixos/configuration.nix`:
23 ```
24 nix.binaryCaches = [ "https://cache.nixos.org/" "https://nixcache.reflex-frp.org" ];
25 nix.binaryCachePublicKeys = [ "ryantrinkle.com-1:JJiAKaRv9mWgpVAz8dwewnZe0AzzEAzPkagE9SP5NWI=" ];
26 ```
27 1. If you are using another operating system or linux distribution, ensure that these lines are present in your Nix configuration file (`/etc/nix/nix.conf` on most systems; [see full list](https://nixos.org/nix/manual/#sec-conf-file)):
28 ```
29 substituters = https://cache.nixos.org https://nixcache.reflex-frp.org
30 trusted-public-keys = cache.nixos.org-1:6NCHdD59X431o0gWypbMrAURkbJ16ZPMQFGspcDShjY= ryantrinkle.com-1:JJiAKaRv9mWgpVAz8dwewnZe0AzzEAzPkagE9SP5NWI=
31 ```
32 * other Linux: enable sandboxing (see these [issue172](https://github.com/obsidiansystems/obelisk/issues/172#issuecomment-411507818) or [issue6](https://github.com/obsidiansystems/obelisk/issues/6) if you run into build problems)
33 ```
34 sandbox = true
35 ```
36 * MacOS: disable sandboxing (there are still some impure dependencies for now)
37 ```
38 sandbox = false
39 ```
40 then restart the nix daemon
41 ```
42 sudo launchctl stop org.nixos.nix-daemon
43 sudo launchctl start org.nixos.nix-daemon
44 ```
451. Install obelisk: `nix-env -f https://github.com/obsidiansystems/obelisk/archive/master.tar.gz -iA command`
47### Contributing to Obelisk
49When developing on obelisk itself you may launch `ghcid` for the corresponding project as follows. For example to launch ghcid for `lib/backend` project:
51```
52nix-shell -A obeliskEnvs.obelisk-backend --run "cd lib/backend && ghcid -c 'cabal new-repl'"
53```
55Or to launch ghcid for `lib/command` project:
57```
58nix-shell -A obeliskEnvs.obelisk-command --run "cd lib/command && ghcid -c 'cabal new-repl'"
59```
61### Accessing private repositories
63To allow the Nix builder to access private git repositories, you must be set up
64to access them via SSH. Follow these steps depending on the platform you need
65access to:
67- [GitHub](https://help.github.com/articles/connecting-to-github-with-ssh/)
68- [GitLab](https://docs.gitlab.com/ee/gitlab-basics/create-your-ssh-keys.html)
70## Developing an Obelisk project
72To create a new Obelisk project, go to an empty directory and run:
74```
75ob init
76```
78Obelisk leverages ghcid to provide a live-reloading server that handles both frontend and backend. To run your Obelisk app and monitor the source for changes:
80```
81ob run
82```
84Now go to http://localhost:8000 (or the port specified in `config/common/route`) to access your app.
86Every time you change the Haskell source files in frontend, common or backend, `ob run` will automatically recompile the modified files and reload the server. Furthermore, it will display on screen compilation errors and warnings if any.
88### Hoogle
90To enter a nix-shell from which you can run the hoogle command-line client or a hoogle server for your project:
92`nix-shell -A shells.ghc --arg withHoogle true`
94### Adding package overrides
96To add a version override to any Haskell package, or to add a Haskell package that doesn't exist in the nixpkgs used by Obelisk, use the `overrides` attribute in your project's `default.nix`. For example, to use a specific version of the `aeson` package, your `default.nix` will look like:
98```nix
99# ...
100project ./. ({ pkgs, ... }: {
101# ...
102 overrides = self: super: let
103 aeson = pkgs.fetchFromGitHub {
104 owner = "obsidiansystems";
105 repo = "aeson-gadt-th";
106 rev = "ed573c2cccf54d72aa6279026752a3fecf9c1383";
107 sha256 = "08q6rnz7w9pn76jkrafig6f50yd0f77z48rk2z5iyyl2jbhcbhx3";
108 };
109 in
110 {
111 aeson = self.callCabal2nix "aeson" aeson {};
112 };
113# ...
114```
116For further information see [the Haskell section](https://nixos.org/nixpkgs/manual/#users-guide-to-the-haskell-infrastructure) of nixpkgs Contributors Guide.
118## Deploying
120### Locally
122Build everything:
124```
125nix-build -A exe -o result-exe
126```
128Run the server:
130```
131cd result-exe
132./backend
133```
135### EC2
137In this section we will demonstrate how to deploy your Obelisk app to an Amazon EC2 instance.
139First create a new EC2 instance:
1411. Launch a NixOS 18.09 EC2 instance (we recommend [this AMI](https://console.aws.amazon.com/ec2/v2/home?region=us-east-1#LaunchInstanceWizard:ami=ami-009c9c3f1af480ff3))
1421. In the instance configuration wizard ensure that your instance has at least 1GB RAM and 10GB disk space.
1431. When prompted save your AWS private key (`~/myaws.pem`) somewhere safe. We'll need it later during deployment.
1441. Go to "Security Groups", select your instance's security group and under "Inbound" tab add a new rule for HTTP port 80 and 443.
146At this stage your instance should be booting and become accessible shortly. Note down the hostname of your EC2 instance.
148Now go to your Obelisk project directory (`~/code/myapp`), and initialize a deployment config (`~/code/myapp-deploy`):
149Your project directory must be "thunkable", i.e. something on which `ob thunk pack` can be called. Usually it will be a git repository whose current revision has been pushed upstream.
151```
152cd ~/code/myapp
153SERVER=ec2-35-183-22-197.ca-central-1.compute.amazonaws.com
154ROUTE=https://myapp.com # Publicly accessible route to your app
155EMAIL=myname@myapp.com
156ob deploy init \
157 --ssh-key ~/myaws.pem \
158 --hostname $SERVER \
159 --route $ROUTE \
160 --admin-email $EMAIL \
161 ~/code/myapp-deploy
162```
164NOTE: HTTPS is enabled by default; to disable https, pass `--disable-https` to the `ob deploy init` command above.
166Then go to that created deployment configuration directory, and initiate the deployment:
168```
169cd ~/code/myapp-deploy
170ob deploy push
171```
173`ob deploy push` will locally build your app and then transfer it, along with all the Nix package dependencies, via ssh to the EC2 instance. The backend will live in `/var/lib/backend`.
175At this point you are done. Your app will be accessible at `${ROUTE}`. The currently deployed version - the git commit hash of the source repo - can be found at `${ROUTE}/version`.
177### From macOS
179Deploying from macOS requires some extra setup:
181- [Install nix-darwin](https://github.com/LnL7/nix-darwin)
182- [Install docker](https://docs.docker.com/)
184Running `ob deploy push` will give you additional setup instructions.
186### Deploying an updated version
188If you'd like to deploy an updated version (with new commits) of your Obelisk app: simply go to the configuration directory, update the source thunk and push:
190```
191cd ~/code/myapp-deploy
192ob deploy update
193ob deploy push
194```
196## Mobile
197Until Obelisk offers a `ob deploy` equivalent for mobile apps, you are recommended to do it manually as follows.
199### iOS
201#### First time setup
202Development on iOS requires a computer running macOS and an iOS developer account.
203Your account must also belong to an Apple Developer Team, if you want to access developer portal links (otherwise they'll redirect to your account page).
205##### iPhone
2061. Connect the iPhone on which you'd like to run builds - this will open up iTunes.
2071. Click accept to authorize on both the computer and the iPhone.
209##### Xcode
210Install Xcode 8.2 (contains iOS SDK 10.2) and open it so that it runs its post install tool setup.
212These versions will work out of the box but iOS SDKs prior to 11.3 should also work. You can choose another installed version in `default.nix`
214More recent Xcodes should also work, as long as one of the SDKs mentioned above has been used.
215To add another SDK to your current Xcode, [download](https://developer.apple.com/download/more/) the corresponding Xcode, extract it and copy its SDK folder next to the installed one, e.g.
216```
217open -W Xcode_9.2.xip
218sudo cp -R Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS.sdk /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS11.2.sdk
219```
222You can verify that you have correct versions by running
223```
224xcodebuild -showsdks
225```
227##### Certificates
228To deploy and/or package apps, you'll need to inform Apple of your development devices and permissions by
229adding credentials to the correct provisioning profile via the Apple Developer portal.
2311. Open up XCode and go to Preferences - Accounts. Select the organization
232Member role, click Manage Certificates, and add an iOS Development
233certificate.
2341. Go to [developer portal - devices](https://developer.apple.com/account/ios/device/) and add your device.
235To find your device's UDID, select it in iTunes and click the serial number.
2361. Go to [developer portal - development profiles](https://developer.apple.com/account/ios/profile/limited).
237Create a development profile and add your certificate and device.
238Click "Generate" and then download and open the profile.
240#### Building
2411. In your project's `default.nix` set values for `ios.bundleIdentifier` and `ios.bundleName`.
242Ensure that `bundleIdentifier` matches the App ID of the development profile, or that you are using a wildcard profile.
2431. Run `nix-build -A ios.frontend -o result-ios` to build the app. Find it at `result-ios/frontend.app`
245#### Deploying
2461. Connect the registered iPhone.
2471. Find your Apple Team ID in the [developer portal](https://developer.apple.com/account/#/membership).
2481. Run the deploy command with your Team ID:
249```
250result-ios/bin/deploy [TEAM_ID]
251# or in debug mode via lldb:
252result-ios/bin/deploy [TEAM_ID] -d
253```
255#### Packaging
2561. Go to [developer portal - distribution profiles](https://developer.apple.com/account/ios/profile/production).
257Create and download a distribution profile.
2581. Run the package script with your TEAM ID and your distribution profile to create a `.ipa`:
259```
260result-ios/bin/package [TEAM_ID] /path/to/output/.ipa /path/to/profile/file
261```
263#### Debugging
264It's also possible to inspect iOS WkWebView apps once they are installed in the iPhone:
2651. On the desktop, go to Safari > Preferences > Advanced and enable Develop menu.
2661. On the iPhone go to Settings > Safari > Advanced and enable Web Inspector.
2671. Open the app on the iPhone while it is connected to the desktop.
2681. In the desktop's Safari Develop menu, you should see your iPhone. Select the screen under the name of the app.
270### Android
272NOTE: Currently Android builds are only supported on Linux.
2741. In your project's `default.nix` set a suitable value for `android.applicationId` and `android.displayName`.
2751. Run `nix-build -A android.frontend -o result-android` to build the Android app.
2761. A debug version of the app should be generated at `result-android/android-app-debug.apk`
278Now deploy the built apk file to your Android device:
2801. Enable *USB debugging* in your Android device ([instructions here](https://developer.android.com/studio/debug/dev-options))
2811. Connect the device using USB (be sure to confirm any security prompts on the device)
2821. Run the deploy script: `result-android/bin/deploy`
284Alternatively, you can deploy from an obelisk deployment directory (a directory generated post `ob deploy init ...` command) using the `ob deploy test android` command.
285This command will accomplish the following:
2871. Create a key store and apk signing key (`android_keystore.jks`)
2881. Build a Signed Android apk for your application
2891. Deploy the Signed apk to your connected Android device
291In the event that you change your key or keystore password, you will have to update your credentials within the JSON object found in `android_keytool_config.json`
293Additional documentation on java key stores can be found [here] (https://docs.oracle.com/javase/8/docs/technotes/tools/unix/keytool.html)
295This should copy over and install the application on your device (if you see a "*signatures do not match*" error, simply uninstall the previous app from the device before retrying the deploy). The name of the installed application will be what you have specified for `android.displayName` in the `default.nix`.
297#### Releasing to Play Store
299##### Build a release version
301After having configured signing for your app, you may proceed to build a release version of the app. This is no different to how you build the non-release version, so consult the section [Android](#android) further above for exact instructions on building and deploying to your device.