on easier-r-slash-import help sign in
obelisk/README.md
348 lines 17 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 Packages](#adding-packages)
9 - [Adding Package Overrides](#adding-package-overrides)
10 - [Running over https](#running-over-https)
11- [Deploying](#deploying)
12 - [Locally](#locally)
13 - [EC2](#ec2)
14 - [From macOS](#from-macos)
15 - [Deploying an updated version](#deploying-an-updated-version)
16- [Mobile](#mobile)
17 - [iOS](#ios)
18 - [Android](#android)
20## Installing Obelisk
211. [Install Nix](https://nixos.org/nix/).
22 If you already have Nix installed, make sure you have version 2.0 or higher. To check your current version, run `nix-env --version`.
231. Set up nix caches
24 1. If you are running NixOS, add this to `/etc/nixos/configuration.nix`:
25 ```
26 nix.binaryCaches = [ "https://nixcache.reflex-frp.org" ];
27 nix.binaryCachePublicKeys = [ "ryantrinkle.com-1:JJiAKaRv9mWgpVAz8dwewnZe0AzzEAzPkagE9SP5NWI=" ];
28 ```
29 and rebuild your NixOS configuration (e.g. `sudo nixos-rebuild switch`).
30 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)):
31 ```
32 binary-caches = https://cache.nixos.org https://nixcache.reflex-frp.org
33 binary-cache-public-keys = cache.nixos.org-1:6NCHdD59X431o0gWypbMrAURkbJ16ZPMQFGspcDShjY= ryantrinkle.com-1:JJiAKaRv9mWgpVAz8dwewnZe0AzzEAzPkagE9SP5NWI=
34 binary-caches-parallel-connections = 40
35 ```
36 * 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)
37 ```
38 sandbox = true
39 ```
40 * MacOS: disable sandboxing (there are still some impure dependencies for now)
41 ```
42 sandbox = false
43 ```
44 then restart the nix daemon
45 ```
46 sudo launchctl stop org.nixos.nix-daemon
47 sudo launchctl start org.nixos.nix-daemon
48 ```
491. Install obelisk: `nix-env -f https://github.com/obsidiansystems/obelisk/archive/master.tar.gz -iA command`
51### Contributing to Obelisk
53When developing on obelisk itself you may launch `ghcid` for the corresponding project as follows. For example to launch ghcid for `lib/backend` project:
55```
56nix-shell -A obeliskEnvs.obelisk-backend --run "cd lib/backend && ghcid -c 'cabal new-repl'"
57```
59Or to launch ghcid for `lib/command` project:
61```
62nix-shell -A obeliskEnvs.obelisk-command --run "cd lib/command && ghcid -c 'cabal new-repl'"
63```
65To re-install `ob` from source do
66```
67nix-env -f /path/to/obelisk -iA command
68```
70Note that `ob` will defer to the version found in your project's `.obelisk/impl` directory. To update that version specifically:
72```shell
73ob thunk unpack ./.obelisk/impl
74cd ./.obelisk/impl
75# apply your changes
76```
78If you want to commit your changes, first push them to your fork of obelisk and then
80```shell
81cd /your/project/root
82ob thunk pack .obelisk/impl
83git add .obelisk/impl
84git commit -m "Bump obelisk"
85```
87### Accessing private repositories
89To allow the Nix builder to access private git repositories, you must be set up
90to access them via SSH. Follow these steps depending on the platform you need
91access to:
93- [GitHub](https://help.github.com/articles/connecting-to-github-with-ssh/)
94- [GitLab](https://docs.gitlab.com/ee/gitlab-basics/create-your-ssh-keys.html)
96## Developing an Obelisk project
98To create a new Obelisk project, go to an empty directory and run:
100```
101ob init
102```
104Obelisk 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:
106```
107ob run
108```
110Now, with an appropriate browser, go to http://localhost:8000 (or the address/port specified in `config/common/route`) to access your app.
111Firefox will not be able to properly run the development website due to [issue 460](https://github.com/obsidiansystems/obelisk/issues/460). Fortunately, this problem does not occur on a fully built website.
113Every 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.
115### Hoogle
117To enter a nix-shell from which you can run the hoogle command-line client or a hoogle server for your project:
119`nix-shell -A shells.ghc --arg withHoogle true`
121### Adding packages
123In order to add package dependencies, declare them under the build-depends field in the appropriate cabal files (backend, common, and frontend each have their own). The corresponding Nix packages will automatically be selected when building.
125### Adding package overrides
127To 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 fetched from GitHub and a specific version of the `waargonaut` package fetched from Hackage, your `default.nix` will look like:
129```nix
130# ...
131project ./. ({ pkgs, ... }: {
132# ...
133 overrides = self: super: let
134 aesonSrc = pkgs.fetchFromGitHub {
135 owner = "obsidiansystems";
136 repo = "aeson-gadt-th";
137 rev = "ed573c2cccf54d72aa6279026752a3fecf9c1383";
138 sha256 = "08q6rnz7w9pn76jkrafig6f50yd0f77z48rk2z5iyyl2jbhcbhx3";
139 };
140 in
141 {
142 aeson = self.callCabal2nix "aeson" aesonSrc {};
143 waargonaut = self.callHackageDirect {
144 pkg = "waargonaut";
145 ver = "0.8.0.1";
146 sha256 = "1zv28np3k3hg378vqm89v802xr0g8cwk7gy3mr77xrzy5jbgpa39";
147 } {};
148 };
149# ...
150```
152For further information see [the Haskell section](https://nixos.org/nixpkgs/manual/#users-guide-to-the-haskell-infrastructure) of nixpkgs Contributors Guide.
154### Running over https
156To run your app locally over https, update the protocol in `config/common/route` to `https`, and then use `ob run` as normal.
158Since Obelisk generates a self-signed certificate for running https, the browser will issue a warning about using an invalid certificate. On Chrome, you can go to `chrome://flags/#allow-insecure-localhost` to enable invalid certificates for localhost.
160## Deploying
162### Locally
164Build everything:
166```
167nix-build -A exe --no-out-link
168```
170Copy the result to a new directory, add configuration, and run!
172```
173mkdir test-app
174ln -s $(nix-build -A exe --no-out-link)/* test-app/
175cp -r config test-app
176(cd test-app && ./backend)
177```
179### EC2
181In this section we will demonstrate how to deploy your Obelisk app to an Amazon EC2 instance.
183First create a new EC2 instance:
1851. 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))
1861. In the instance configuration wizard ensure that your instance has at least 1GB RAM and 10GB disk space.
1871. When prompted save your AWS private key (`~/myaws.pem`) somewhere safe. We'll need it later during deployment.
1881. Go to "Security Groups", select your instance's security group and under "Inbound" tab add a new rule for HTTP port 80 and 443.
190At this stage your instance should be booting and become accessible shortly. Note down the hostname of your EC2 instance.
192Now go to your Obelisk project directory (`~/code/myapp`), and initialize a deployment config (`~/code/myapp-deploy`):
193Your 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.
195```
196cd ~/code/myapp
197SERVER=ec2-35-183-22-197.ca-central-1.compute.amazonaws.com
198ROUTE=https://myapp.com # Publicly accessible route to your app
199EMAIL=myname@myapp.com
200ob deploy init \
201 --ssh-key ~/myaws.pem \
202 --hostname $SERVER \
203 --route $ROUTE \
204 --admin-email $EMAIL \
205 ~/code/myapp-deploy
206```
208HTTPS is enabled by default; to disable https, pass `--disable-https` to the `ob deploy init` command above.
210This step will also require that you manually verify the authenticity of the host `$SERVER`. Obelisk will save the fingerprint in a deployment-specific configuration. **Obelisk deployments do *not* rely on the `known_hosts` of your local machine.** This is because, in the event that you need to switch from one deploy machine / bastion host to another, you want to be absolutely sure that you're still connecting to the machines you think you are, even if that deploy machine / bastion host has never connected to them before. Obelisk explicitly avoids a workflow that encourages people to accept host keys without checking them, since that could result in leaking production secrets to anyone who manages to MITM you, e.g. via DNS spoofing or cache poisoning. (Note that an active attack is a circumstance where you may need to quickly switch bastion hosts, e.g. because the attacker has taken one down or you have taken it down in case it was compromised. In this circumstance you might need to deploy to production to fix an exploit or rotate keys, etc.) When you run `ob deploy` later it will rely on the saved verification in this step.
212Next, go to the deployment directory that you just initialized and deploy!
214```
215cd ~/code/myapp-deploy
216ob deploy push
217```
219`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`.
221At 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`.
223### From macOS
225Deploying from macOS requires some extra setup:
227- [Install nix-darwin](https://github.com/LnL7/nix-darwin)
228- [Install docker](https://docs.docker.com/)
230Running `ob deploy push` will give you additional setup instructions.
232### Deploying an updated version
234If 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:
236```
237cd ~/code/myapp-deploy
238ob deploy update
239ob deploy push
240```
242## Mobile
243Until Obelisk offers a `ob deploy` equivalent for mobile apps, you are recommended to do it manually as follows.
245### iOS
247#### First time setup
248Development on iOS requires a computer running macOS and an iOS developer account.
249Your 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).
251##### iPhone
2521. Connect the iPhone on which you'd like to run builds - this will open up iTunes.
2531. Click accept to authorize on both the computer and the iPhone.
255##### Xcode
256Install Xcode 8.2 (contains iOS SDK 10.2) and open it so that it runs its post install tool setup.
258These 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`
260More recent Xcodes should also work, as long as one of the SDKs mentioned above has been used.
261To 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.
262```
263open -W Xcode_9.2.xip
264sudo 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
265```
268You can verify that you have correct versions by running
269```
270xcodebuild -showsdks
271```
273##### Certificates
274To deploy and/or package apps, you'll need to inform Apple of your development devices and permissions by
275adding credentials to the correct provisioning profile via the Apple Developer portal.
2771. Open up XCode and go to Preferences - Accounts. Select the organization
278Member role, click Manage Certificates, and add an iOS Development
279certificate.
2801. Go to [developer portal - devices](https://developer.apple.com/account/ios/device/) and add your device.
281To find your device's UDID, select it in iTunes and click the serial number.
2821. Go to [developer portal - development profiles](https://developer.apple.com/account/ios/profile/limited).
283Create a development profile and add your certificate and device.
284Click "Generate" and then download and open the profile.
286#### Building
2871. In your project's `default.nix` set values for `ios.bundleIdentifier` and `ios.bundleName`.
288Ensure that `bundleIdentifier` matches the App ID of the development profile, or that you are using a wildcard profile.
2891. Run `nix-build -A ios.frontend -o result-ios` to build the app. Find it at `result-ios/frontend.app`
291#### Deploying
2921. Connect the registered iPhone.
2931. Find your Apple Team ID in the [developer portal](https://developer.apple.com/account/#/membership).
2941. Run the deploy command with your Team ID:
295```
296result-ios/bin/deploy [TEAM_ID]
297# or in debug mode via lldb:
298result-ios/bin/deploy [TEAM_ID] -d
299```
301#### Packaging
3021. Go to [developer portal - distribution profiles](https://developer.apple.com/account/ios/profile/production).
303Create and download a distribution profile.
3041. Run the package script with your TEAM ID and your distribution profile to create a `.ipa`:
305```
306result-ios/bin/package [TEAM_ID] /path/to/output/.ipa /path/to/profile/file
307```
309#### Debugging
310It's also possible to inspect iOS WkWebView apps once they are installed in the iPhone:
3111. On the desktop, go to Safari > Preferences > Advanced and enable Develop menu.
3121. On the iPhone go to Settings > Safari > Advanced and enable Web Inspector.
3131. Open the app on the iPhone while it is connected to the desktop.
3141. In the desktop's Safari Develop menu, you should see your iPhone. Select the screen under the name of the app.
316### Android
318NOTE: Currently Android builds are only supported on Linux.
3201. In your project's `default.nix` set a suitable value for `android.applicationId` and `android.displayName`.
3211. In your project's `default.nix` pass `config.android_sdk.accept_license = true;` in the arguments to the import of of `.obelisk/impl` to indicate your acceptance of the [Android Software Development Kit License Agreement](https://developer.android.com/studio/terms), which is required to build Android apps.
3221. Run `nix-build -A android.frontend -o result-android` to build the Android app.
3231. A debug version of the app should be generated at `result-android/android-app-debug.apk`
325Now deploy the built apk file to your Android device:
3271. Enable *USB debugging* in your Android device ([instructions here](https://developer.android.com/studio/debug/dev-options))
3281. Connect the device using USB (be sure to confirm any security prompts on the device)
3291. Run the deploy script: `result-android/bin/deploy`
331Alternatively, you can deploy from an obelisk deployment directory (a directory generated post `ob deploy init ...` command) using the `ob deploy test android` command.
332This command will accomplish the following:
3341. Create a key store and apk signing key (`android_keystore.jks`)
3351. Build a Signed Android apk for your application
3361. Deploy the Signed apk to your connected Android device
338In 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`.
340Additional documentation on Java key stores can be found [here](https://docs.oracle.com/javase/8/docs/technotes/tools/unix/keytool.html).
342This 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`.
344#### Releasing to Play Store
346##### Build a release version
348After 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.