nixfiles/lib/darwin/apps/common.nix

100 lines
2.6 KiB
Nix
Raw Permalink Normal View History

{ wget, lib }:
2024-05-01 06:01:46 +03:00
rec {
download = {
url,
params ? "",
var ? "DOWNLOADED_FILE"
}: ''
echo "Downloading" ${lib.escapeShellArg url}"..."
${var}=$(mktemp)
${wget}/bin/wget ${lib.escapeShellArg url} ${params} -q --show-progress -O "${"$" + var}"
2024-05-01 06:01:46 +03:00
'';
withMountedDmg = path: shell: ''
_result=$(hdiutil mount "${path}" | tail -n1)
2024-05-01 06:01:46 +03:00
DMG_DEVICE=$(echo "$_result" | awk '{print $1}')
2024-05-01 07:43:04 +03:00
DMG_MOUNTPOINT=$(echo "$_result" | perl -lane 'print "@F[2..$#F]"')
2024-05-01 06:01:46 +03:00
unset _result
function _unmount {
hdiutil unmount "$DMG_DEVICE" > /dev/null
2024-05-01 06:01:46 +03:00
}
2024-05-01 07:43:04 +03:00
# trap _unmount ERR exit
2024-05-01 06:01:46 +03:00
${shell}
_unmount
trap - ERR exit
unset DMG_DEVICE
unset DMG_MOUNTPOINT
'';
installAppFromDmg = { dmg, filename }: withMountedDmg dmg ''
if [ ! -d "$DMG_MOUNTPOINT/"${lib.escapeShellArg filename} ]; then
echo "Error: file not found:" ${lib.escapeShellArg filename}
exit 1
fi
cp -r "$DMG_MOUNTPOINT/"${lib.escapeShellArg filename} /Applications
'';
downloadAndInstallDmgApp = {
url,
filename,
params ? "",
}: ''
if [ ! -d "/Applications/"${lib.escapeShellArg (builtins.baseNameOf filename)} ]; then
${download { inherit url params; }}
${installAppFromDmg { dmg = "$DOWNLOADED_FILE"; inherit filename; }}
rm -rf "$DOWNLOADED_FILE"
2024-05-01 06:01:46 +03:00
fi
'';
downloadAndInstallDmgPkg = {
url,
filename,
condition,
params ? "",
}: ''
if [ ${condition} ]; then
${download { inherit url params; }}
${withMountedDmg "$DOWNLOADED_FILE" ''
sudo /usr/sbin/installer -pkg "$DMG_MOUNTPOINT/"${lib.escapeShellArg filename} -target /
2024-05-01 06:01:46 +03:00
''}
rm -rf "$DOWNLOADED_FILE"
2024-05-01 06:01:46 +03:00
fi
'';
2024-05-01 07:43:04 +03:00
downloadAndInstallZipApp = {
url,
filename,
params ? "",
renameTo ? null,
afterInstall ? ""
}: let
conditionFile = if renameTo == null then filename else renameTo;
in ''
if [ ! -d "/Applications/"${lib.escapeShellArg (builtins.baseNameOf conditionFile)} ]; then
${download { inherit url params; }}
tmpdir=$(mktemp -d)
unzip -q "$DOWNLOADED_FILE" -d "$tmpdir"
2024-05-01 07:43:04 +03:00
if [ ! -d "$tmpdir/"${lib.escapeShellArg filename} ]; then
echo "Error: file not found:" ${lib.escapeShellArg filename}
rm -rf "$DOWNLOADED_FILE" "$tmpdir"
2024-05-01 07:43:04 +03:00
exit 1
fi
${if (renameTo != null) then ''
mv "$tmpdir/"${lib.escapeShellArg filename} "$tmpdir/"${lib.escapeShellArg renameTo}
mv "$tmpdir/"${lib.escapeShellArg renameTo} /Applications
'' else ''
mv "$tmpdir/"${lib.escapeShellArg filename} /Applications
''}
rm -rf "$tmpdir" "$DOWNLOADED_FILE"
2024-05-01 07:43:04 +03:00
${afterInstall}
fi
'';
2024-05-01 06:01:46 +03:00
}