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