Obelisk provides an easy way to develop and deploy your Reflex project as web apps and as mobile apps.
- Install Nix.
If you already have Nix installed, make sure you have version 2.0 or higher. To check your current version, run
nix-env --version
. - Set up nix caches
- If you are running NixOS, add this to
/etc/nixos/configuration.nix
:nix.binaryCaches = [ "https://cache.nixos.org/" "https://nixcache.reflex-frp.org" ]; nix.binaryCachePublicKeys = [ "ryantrinkle.com-1:JJiAKaRv9mWgpVAz8dwewnZe0AzzEAzPkagE9SP5NWI=" ];
- If you are using another operating system or linux distribution, ensure that these lines are present in
/etc/nix/nix.conf
:sandbox = true substituters = https://cache.nixos.org https://nixcache.reflex-frp.org trusted-public-keys = cache.nixos.org-1:6NCHdD59X431o0gWypbMrAURkbJ16ZPMQFGspcDShjY= ryantrinkle.com-1:JJiAKaRv9mWgpVAz8dwewnZe0AzzEAzPkagE9SP5NWI=
- If you are on MacOS, restart the nix daemon
sudo launchctl stop org.nixos.nix-daemon sudo launchctl start org.nixos.nix-daemon
- If you are running NixOS, add this to
- Install obelisk:
nix-env -f https://github.com/obsidiansystems/obelisk/archive/master.tar.gz -iA command
When developing on obelisk itself you may launch ghcid
for the corresponding project as follows. For example to launch ghcid for lib/backend
project:
nix-shell -A obelisk.obelisk-backend.env --run "cd lib/backend && ghcid -c 'cabal new-repl'"
Or to launch ghcid for lib/command
project:
nix-shell -A obelisk.obelisk-command.env --run "cd lib/command && ghcid -c 'cabal new-repl'"
To allow the Nix builder to access private git repositories, follow these steps:
- Get set up to connect to GitHub with SSH
- Create a GitHub personal access token
- Set environment variables:
- NixOS: Add this to
/etc/nixos/configuration.nix
:
nix.envVars = { NIX_GITHUB_PRIVATE_USERNAME = "your-github-username"; NIX_GITHUB_PRIVATE_PASSWORD = "your-github-personal-access-token"; };
- MacOS:
sudo launchctl setenv NIX_GITHUB_PRIVATE_USERNAME "your-github-username" sudo launchctl setenv NIX_GITHUB_PRIVATE_PASSWORD "your-github-personal-access" sudo launchctl stop org.nixos.nix-daemon sudo launchctl start org.nixos.nix-daemon
- NixOS: Add this to
nix-env -i hub
ORnix-env -iA nixos.gitAndTools.hub
hub clone yourusername/yourproject
- NOTE: you must authenticate with hub at least once, because the
ob
command useshub
for authentication #TODO: Make ob do this itself (either invoke hub automatically or not depend on hub)
To create a new Obelisk project, go to an empty directory and run:
ob init
Obelisk 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:
ob run
Now go to http://localhost:8000 (or the port specified in config/common/route
) to access your app.
Every 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.
In this section we will demonstrate how to deploy your Obelisk app to an Amazon EC2 instance.
First create a new EC2 instance:
- Launch a NixOS 17.09 EC2 instance (we recommend this AMI)
- In the instance configuration wizard ensure that your instance has at least 1GB RAM and 10GB disk space.
- When prompted save your AWS private key (
~/myaws.pem
) somewhere safe. We'll need it later during deployment. - Go to "Security Groups", select your instance's security group and under "Inbound" tab add a new rule for HTTP port 80.
At this stage your instance should be booting and become accessible shortly. Note down the hostname of your instance. It should look like this:
INSTANCE_HOSTNAME=ec2-??-??-??-??.ca-central-1.compute.amazonaws.com
Now go to your Obelisk project directory (~/code/myapp
), and initialize a deployment config (~/code/myapp-deploy
):
Your 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.
cd ~/code/myapp
ob deploy init --ssh-key ~/myaws.pem --hostname ${INSTANCE_HOSTNAME} ~/code/myapp-deploy
Then go to that created deployment configuration directory, and initiate the deployment:
cd ~/code/myapp-deploy
ob deploy push
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. It will also configure Nginx so that the public port 80 proxies to the running app.
At this point you are done. Your app will be accessible at http://${HOSTNAME}
!
If 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:
cd ~/code/myapp-deploy
ob deploy update
ob deploy push
Until Obelisk offers a ob deploy
equivalent for mobile apps, you are recommended to do it manually as follows.
Development on iOS requires a computer running macOS and an iOS developer account. Your 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).
- Connect the iPhone on which you'd like to run builds - this will open up iTunes.
- Click accept to authorize on both the computer and the iPhone.
Install Xcode 8.2 (contains iOS SDK 10.2) and open it so that it runs its post install tool setup.
These 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
More recent Xcodes should also work, as long as one of the SDKs mentioned above has been used. To add another SDK to your current Xcode, download the corresponding Xcode, extract it and copy its SDK folder next to the installed one, e.g.
open -W Xcode_9.2.xip
sudo 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
You can verify that you have correct versions by running
xcodebuild -showsdks
To deploy and/or package apps, you'll need to inform Apple of your development devices and permissions by adding credentials to the correct provisioning profile via the Apple Developer portal.
- Open up XCode and go to Preferences - Accounts. Select the organization Member role, click Manage Certificates, and add an iOS Development certificate.
- Go to developer portal - devices and add your device. To find your device's UDID, select it in iTunes and click the serial number.
- Go to developer portal - development profiles. Create a development profile and add your certificate and device. Click "Generate" and then download and open the profile.
- In your project's
default.nix
set values forios.bundleIdentifier
andios.bundleName
. Ensure thatbundleIdentifier
matches the App ID of the development profile, or that you are using a wildcard profile. - Run
nix-build -A ios.frontend -o result-ios
to build the app. Find it atresult-ios/frontend.app
- Connect the registered iPhone.
- Find your Apple Team ID in the developer portal.
- Run the deploy command with your Team ID:
result-ios/bin/deploy [TEAM_ID]
# or in debug mode via lldb:
result-ios/bin/deploy [TEAM_ID] -d
- Go to developer portal - distribution profiles. Create and download a distribution profile.
- Run the package script with your TEAM ID and your distribution profile to create a
.ipa
:
result-ios/bin/package [TEAM_ID] /path/to/output/.ipa /path/to/profile/file
It's also possible to inspect iOS WkWebView apps once they are installed in the iPhone:
- On the desktop, go to Safari > Preferences > Advanced and enable Develop menu.
- On the iPhone go to Settings > Safari > Advanced and enable Web Inspector.
- Open the app on the iPhone while it is connected to the desktop.
- In the desktop's Safari Develop menu, you should see your iPhone. Select the screen under the name of the app.
- In your project's
default.nix
set a suitable value forandroid.applicationId
andandroid.displayName
. - Run
nix-build -A android.frontend -o result-android
to build the Android app. - A debug version of the app should be generated at
result-android/android-app-debug.apk
Now deploy the built apk file to your Android device:
- Enable USB debugging in your Android device (instructions here)
- Connect the device using USB (be sure to confirm any security prompts on the device)
- Run the deploy script:
result-android/bin/deploy
This 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
.
The previous section would have generated a debug version of the app. In order to build a release version you will need to sign your app. Obelisk can automatically sign the app during build if you provide it with your keystore file in default.nix
.
First, if you do not already have a keystore, create it as follows (for more information, see the Android documentation):
nix-shell -p androidenv.platformTools --run "keytool -genkey -v -keystore myandroidkey.jks -keyalg RSA -keysize 2048 -validity 10000 -alias myandroidalias"
(Besure to give an appropriate keystore filename and key alias string above.)
The keytool
command will ask you for some details, including a keystore password and a key password (we will use these passwords further below). It will now have created a myandroidkey.jks
file under the current directory. Move that to somewhere safe, and note down its full path.
Now edit your project's default.nix
and tell Obelisk of your app's keystore file. Your default.nix
should look like this after the edit:
...
android.applicationId = "com.example.myapp";
android.displayName = "My App";
android.releaseKey =
{ storeFile = /path/to/myandroidkey.jks;
storePassword = "abcd1234";
keyAlias = "myandroidalias";
keyPassword = "abcd1234";
};
...
After 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 further above for exact instructions on building and deploying to your device.