2013-01-07 16:52:42 +01:00
{ stdenv , xcodewrapper }:
{ name
, src
2018-01-05 11:40:34 +01:00
, sdkVersion ? " 1 1 . 2 "
2013-01-07 16:52:42 +01:00
, target ? null
, configuration ? null
, scheme ? null
, sdk ? null
, xcodeFlags ? " "
, release ? false
, codeSignIdentity ? null
, certificateFile ? null
, certificatePassword ? null
, provisioningProfile ? null
, generateIPA ? false
, generateXCArchive ? false
2014-08-01 18:34:18 +02:00
, enableWirelessDistribution ? false
, installURL ? null
, bundleId ? null
, version ? null
, title ? null
2013-01-07 16:52:42 +01:00
} :
assert release -> codeSignIdentity != null && certificateFile != null && certificatePassword != null && provisioningProfile != null ;
2014-08-01 18:34:18 +02:00
assert enableWirelessDistribution -> installURL != null && bundleId != null && version != null && title != null ;
2013-01-07 16:52:42 +01:00
let
# Set some default values here
_target = if target == null then name else target ;
_configuration = if configuration == null
then
if release then " R e l e a s e " else " D e b u g "
else configuration ;
_sdk = if sdk == null
then
2013-03-13 13:02:19 +01:00
if release then " i p h o n e o s " + sdkVersion else " i p h o n e s i m u l a t o r " + sdkVersion
2013-01-07 16:52:42 +01:00
else sdk ;
# The following is to prevent repetition
2014-10-27 17:24:35 +01:00
deleteKeychain = ''
security default-keychain - s login . keychain
security delete-keychain $ keychainName
'' ;
2013-01-07 16:52:42 +01:00
in
stdenv . mkDerivation {
2013-03-21 14:11:58 +01:00
name = stdenv . lib . replaceChars [ " " ] [ " " ] name ;
inherit src ;
2013-01-07 16:52:42 +01:00
buildInputs = [ xcodewrapper ] ;
buildPhase = ''
$ { stdenv . lib . optionalString release ''
export HOME = /Users / $ ( whoami )
keychainName = " $ ( b a s e n a m e $ o u t ) "
# Create a keychain
security create-keychain - p " " $ keychainName
security default-keychain - s $ keychainName
security unlock-keychain - p " " $ keychainName
# Import the certificate into the keychain
security import $ { certificateFile } - k $ keychainName - P " ${ certificatePassword } " - A
2017-01-27 10:50:36 +01:00
# Grant the codesign utility permissions to read from the keychain
security set-key-partition-list - S apple-tool:,apple: - s - k " " $ keychainName
2013-01-07 16:52:42 +01:00
# Determine provisioning ID
2014-07-18 16:40:15 +02:00
PROVISIONING_PROFILE = $ ( grep UUID - A1 - a $ { provisioningProfile } | grep - o " [ - A - Z a - z 0 - 9 ] \{ 3 6 \} " )
2013-01-07 16:52:42 +01:00
if [ ! - f " $ H O M E / L i b r a r y / M o b i l e D e v i c e / P r o v i s i o n i n g P r o f i l e s / $ P R O V I S I O N I N G _ P R O F I L E . m o b i l e p r o v i s i o n " ]
then
# Copy provisioning profile into the home directory
mkdir - p " $ H O M E / L i b r a r y / M o b i l e D e v i c e / P r o v i s i o n i n g P r o f i l e s "
cp $ { provisioningProfile } " $ H O M E / L i b r a r y / M o b i l e D e v i c e / P r o v i s i o n i n g P r o f i l e s / $ P R O V I S I O N I N G _ P R O F I L E . m o b i l e p r o v i s i o n "
fi
# Check whether the identity can be found
security find-identity - p codesigning $ keychainName
'' }
# Do the building
2018-01-05 11:40:34 +01:00
export LD = clang # To avoid problem with -isysroot parameter that is unrecognized by the stock ld. Comparison with an impure build shows that it uses clang instead. Ugly, but it works
2017-01-27 10:50:36 +01:00
xcodebuild - target $ { _target } - configuration $ { _configuration } $ { stdenv . lib . optionalString ( scheme != null ) " - s c h e m e ${ scheme } " } - sdk $ { _sdk } TARGETED_DEVICE_FAMILY = " 1 , 2 " ONLY_ACTIVE_ARCH = NO CONFIGURATION_TEMP_DIR = $ TMPDIR CONFIGURATION_BUILD_DIR = $ out $ { if generateXCArchive then " a r c h i v e " else " " } $ { xcodeFlags } $ { if release then '' " C O D E _ S I G N _ I D E N T I T Y = ${ codeSignIdentity } " P R O V I S I O N I N G _ P R O F I L E = $P R O V I S I O N I N G _ P R O F I L E O T H E R _ C O D E _ S I G N _ F L A G S = " - - k e y c h a i n $H O M E / L i b r a r y / K e y c h a i n s / $k e y c h a i n N a m e - d b " '' else " " }
2018-01-05 11:40:34 +01:00
2013-01-07 16:52:42 +01:00
$ { stdenv . lib . optionalString release ''
$ { stdenv . lib . optionalString generateIPA ''
# Produce an IPA file
2013-03-21 14:11:58 +01:00
xcrun - sdk iphoneos PackageApplication - v $ out /* . a p p - o " $ o u t / $ { n a m e } . i p a "
2013-03-15 16:18:45 +01:00
# Add IPA to Hydra build products
mkdir - p $ out/nix-support
2013-03-21 14:11:58 +01:00
echo " f i l e b i n a r y - d i s t \" $ ( e c h o $ o u t / * . i p a ) \" " > $ out/nix-support/hydra-build-products
2014-08-01 18:34:18 +02:00
$ { stdenv . lib . optionalString enableWirelessDistribution ''
appname = $ ( basename $ out /* . i p a . i p a )
sed - e " s | @ I N S T A L L _ U R L @ | ${ installURL } ? b u n d l e I d = ${ bundleId } \& a m p ; v e r s i o n = ${ version } \& a m p ; t i t l e = $ a p p n a m e | " $ { ./install.html.template } > $ out / $ appname . html
2014-08-04 11:23:25 +02:00
echo " d o c i n s t a l l \" $ o u t / $ a p p n a m e . h t m l \" " > > $ out/nix-support/hydra-build-products
2014-08-01 18:34:18 +02:00
'' }
2013-01-07 16:52:42 +01:00
'' }
# Delete our temp keychain
$ { deleteKeychain }
'' }
'' ;
failureHook = stdenv . lib . optionalString release deleteKeychain ;
installPhase = " t r u e " ;
}