1{ writeScriptBin, lib, makeBinaryWrapper }:
2
3let
4 pListText = lib.generators.toPlist { } {
5 CFBundleDevelopmentRegion = "English";
6 CFBundleExecutable = "$name";
7 CFBundleIconFile = "$icon";
8 CFBundleIconFiles = [ "$icon" ];
9 CFBundleIdentifier = "org.nixos.$name";
10 CFBundleInfoDictionaryVersion = "6.0";
11 CFBundleName = "$name";
12 CFBundlePackageType = "APPL";
13 CFBundleSignature = "???";
14 };
15in writeScriptBin "write-darwin-bundle" ''
16 shopt -s nullglob
17
18 readonly prefix=$1
19 readonly name=$2
20 # TODO: support executables with spaces in their names
21 readonly execName=''${3%% *} # Before the first space
22 [[ $3 =~ " " ]] && readonly execArgs=''${3#* } # Everything after the first space
23 readonly icon=$4.icns
24 readonly squircle=''${5:-1}
25 readonly plist=$prefix/Applications/$name.app/Contents/Info.plist
26 readonly binary=$prefix/bin/$execName
27 readonly bundleExecutable=$prefix/Applications/$name.app/Contents/MacOS/$name
28
29 cat > "$plist" <<EOF
30${pListText}
31EOF
32
33 if [[ $squircle == 0 || $squircle == "false" ]]; then
34 sed '/CFBundleIconFiles/,\|</array>|d' -i "$plist"
35 fi
36
37 if [[ -n "$execArgs" ]]; then
38 (
39 source ${makeBinaryWrapper}/nix-support/setup-hook
40 # WORKAROUND: makeBinaryWrapper fails when -u is set
41 set +u
42 makeBinaryWrapper "$binary" "$bundleExecutable" --add-flags "$execArgs"
43 )
44 else
45 ln -s "$binary" "$bundleExecutable"
46 fi
47''