2018-11-17 23:55:49 +03:00
{ stdenv , composeXcodeWrapper }:
2013-01-07 19:52:42 +04:00
{ name
, src
2019-11-11 02:17:47 +03:00
, sdkVersion ? " 1 3 . 1 "
2013-01-07 19:52:42 +04:00
, target ? null
, configuration ? null
, scheme ? null
, sdk ? null
, xcodeFlags ? " "
, release ? false
, certificateFile ? null
, certificatePassword ? null
, provisioningProfile ? null
2019-02-05 00:43:48 +03:00
, codeSignIdentity ? null
2018-03-22 16:33:15 +03:00
, signMethod ? null
2013-01-07 19:52:42 +04:00
, generateIPA ? false
, generateXCArchive ? false
2014-08-01 20:34:18 +04:00
, enableWirelessDistribution ? false
, installURL ? null
, bundleId ? null
2018-11-17 23:55:49 +03:00
, appVersion ? null
, . . .
} @ args :
2013-01-07 19:52:42 +04:00
2019-02-05 00:43:48 +03:00
assert release -> certificateFile != null && certificatePassword != null && provisioningProfile != null && signMethod != null && codeSignIdentity != null ;
2018-11-17 23:55:49 +03:00
assert enableWirelessDistribution -> installURL != null && bundleId != null && appVersion != null ;
2013-01-07 19:52:42 +04:00
let
# Set some default values here
2018-11-17 23:55:49 +03:00
2013-01-07 19:52:42 +04:00
_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 ;
2018-11-17 23:55:49 +03:00
2013-01-07 19:52:42 +04:00
_sdk = if sdk == null
then
2013-03-13 16:02:19 +04: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 19:52:42 +04:00
else sdk ;
# The following is to prevent repetition
2014-10-27 19:24:35 +03:00
deleteKeychain = ''
security default-keychain - s login . keychain
security delete-keychain $ keychainName
'' ;
2018-11-17 23:55:49 +03:00
xcodewrapperFormalArgs = builtins . functionArgs composeXcodeWrapper ;
xcodewrapperArgs = builtins . intersectAttrs xcodewrapperFormalArgs args ;
xcodewrapper = composeXcodeWrapper xcodewrapperArgs ;
extraArgs = removeAttrs args ( [ " n a m e " " s c h e m e " " x c o d e F l a g s " " r e l e a s e " " c e r t i f i c a t e F i l e " " c e r t i f i c a t e P a s s w o r d " " p r o v i s i o n i n g P r o f i l e " " s i g n M e t h o d " " g e n e r a t e I P A " " g e n e r a t e X C A r c h i v e " " e n a b l e W i r e l e s s D i s t r i b u t i o n " " i n s t a l l U R L " " b u n d l e I d " " v e r s i o n " ] ++ builtins . attrNames xcodewrapperFormalArgs ) ;
2013-01-07 19:52:42 +04:00
in
2018-11-17 23:55:49 +03:00
stdenv . mkDerivation ( {
name = stdenv . lib . replaceChars [ " " ] [ " " ] name ; # iOS app names can contain spaces, but in the Nix store this is not allowed
2013-01-07 19:52:42 +04:00
buildPhase = ''
2019-02-05 00:43:48 +03:00
# Be sure that the Xcode wrapper has priority over everything else.
# When using buildInputs this does not seem to be the case.
export PATH = $ { xcodewrapper } /bin : $ PATH
2013-01-07 19:52:42 +04:00
$ { stdenv . lib . optionalString release ''
2018-11-17 23:55:49 +03:00
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
# Grant the codesign utility permissions to read from the keychain
security set-key-partition-list - S apple-tool:,apple: - s - k " " $ keychainName
# Determine provisioning ID
PROVISIONING_PROFILE = $ ( grep UUID - A1 - a $ { provisioningProfile } | grep - o " [ - A - Z a - z 0 - 9 ] \{ 3 6 \} " )
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
'' }
2013-01-07 19:52:42 +04:00
# Do the building
2019-02-05 00:43:48 +03:00
export LD = /usr/bin/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
2018-01-05 13:40:34 +03:00
2019-02-05 00:43:48 +03: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 generateIPA || generateXCArchive then " - a r c h i v e P a t h \" ${ name } . x c a r c h i v e \" a r c h i v e " else " " } $ { if release then '' 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 " " } $ { xcodeFlags }
2018-01-05 13:40:34 +03:00
2013-01-07 19:52:42 +04:00
$ { stdenv . lib . optionalString release ''
$ { stdenv . lib . optionalString generateIPA ''
2018-03-22 16:33:15 +03:00
# Create export plist file
cat > " ${ name } . p l i s t " < < EOF
< ? xml version = " 1 . 0 " encoding = " U T F - 8 " ? >
< ! DOCTYPE plist PUBLIC " - / / A p p l e / / D T D P L I S T 1 . 0 / / E N " " h t t p : / / w w w . a p p l e . c o m / D T D s / P r o p e r t y L i s t - 1 . 0 . d t d " >
< plist version = " 1 . 0 " >
<dict>
2019-02-05 00:43:48 +03:00
<key> signingCertificate < /key >
<string> $ { codeSignIdentity } < /string >
2018-03-22 16:33:15 +03:00
<key> provisioningProfiles < /key >
<dict>
2019-02-21 23:17:15 +03:00
<key> $ { bundleId } < /key >
2018-03-22 16:33:15 +03:00
<string> $ PROVISIONING_PROFILE < /string >
< /dict >
2019-02-05 00:43:48 +03:00
<key> signingStyle < /key >
<string> manual < /string >
2018-03-22 16:33:15 +03:00
<key> method < /key >
<string> $ { signMethod } < /string >
$ { stdenv . lib . optionalString ( signMethod == " e n t e r p r i s e " || signMethod == " a d - h o c " ) ''
<key> compileBitcode < /key >
< false / >
'' }
< /dict >
< /plist >
EOF
2013-01-07 19:52:42 +04:00
# Produce an IPA file
2018-03-22 16:33:15 +03:00
xcodebuild - exportArchive - archivePath " ${ name } . x c a r c h i v e " - exportOptionsPlist " ${ name } . p l i s t " - exportPath $ out
2013-03-15 19:18:45 +04:00
# Add IPA to Hydra build products
mkdir - p $ out/nix-support
2013-03-21 17:11:58 +04: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
2018-11-17 23:55:49 +03:00
2014-08-01 20:34:18 +04:00
$ { stdenv . lib . optionalString enableWirelessDistribution ''
2018-11-17 23:55:49 +03:00
# Add another hacky build product that enables wireless adhoc installations
2019-02-21 23:42:05 +03:00
appname = " $ ( b a s e n a m e " $ ( echo $ 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 = ${ appVersion } \& a m p ; t i t l e = $ a p p n a m e | " $ { ./install.html.template } > $ out / '' ${ appname } . h t m l
echo " d o c i n s t a l l \" $ o u t / ' ' ${ appname } . h t m l \" " > > $ out/nix-support/hydra-build-products
2014-08-01 20:34:18 +04:00
'' }
2013-01-07 19:52:42 +04:00
'' }
2018-03-22 17:02:09 +03:00
$ { stdenv . lib . optionalString generateXCArchive ''
mkdir - p $ out
mv " ${ name } . x c a r c h i v e " $ out
'' }
2018-11-17 23:55:49 +03:00
2013-01-07 19:52:42 +04:00
# Delete our temp keychain
$ { deleteKeychain }
'' }
'' ;
2018-11-17 23:55:49 +03:00
2013-01-07 19:52:42 +04:00
failureHook = stdenv . lib . optionalString release deleteKeychain ;
2018-11-17 23:55:49 +03:00
2013-01-07 19:52:42 +04:00
installPhase = " t r u e " ;
2018-11-17 23:55:49 +03:00
} // extraArgs )