Skip to content

Instantly share code, notes, and snippets.

@mitchellh
Last active December 5, 2023 05:38
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mitchellh/450cecda9477fa0d7bb2c72f1b672928 to your computer and use it in GitHub Desktop.
Save mitchellh/450cecda9477fa0d7bb2c72f1b672928 to your computer and use it in GitHub Desktop.
Playdate SDK on Nix on aarch64
let
# Playdate distributes their SDK as precompiled x86_64 binaries
# currently so we have to import cross-compiled packages for it
# if we're not on an x86_64 system.
pkgsIntel = import <nixpkgs> {
crossSystem = {
config = "x86_64-unknown-linux-gnu";
};
};
in
final: prev: rec {
inherit pkgsIntel;
# This always outputs x86_64 binaries. I setup binfmt_misc in my
# system config to run x86_64 binaries on aarch64 via qemu. This
# package properly patches all the binaries so they have the right
# x86_64 libs and linker cross-compiled so they'll just work if
# you have binfmt_misc setup.
playdate-sdk = prev.callPackage ./playdate-sdk.nix {};
# Playdate requires gcc-arm-embedded v11. I haven't tested with later
# versions but earlier versions do NOT work. At the time of gisting,
# stable is 22.05 and only has gcc-arm-embedded v10. I overlay
# nixpkgs-unstable to get v11.
gcc-arm-embedded = final.pkgs-unstable.gcc-arm-embedded-11;
}
{
stdenv,
lib,
fetchurl,
pkgsIntel,
}: let
# TODO: eventually, I want to do a check here if we're natively
# on x86_64 to not cross-compile these and just use the built-ins.
# Build inputs for `pdc`
pdcInputs = with pkgsIntel; [
libpng
zlib
];
# Build inputs for the simulator (excluding those from pdc)
pdsInputs = with pkgsIntel; [
udev
];
# For native, if and when we support that:
# "$(cat $NIX_CC/nix-support/dynamic-linker)"
dynamicLinker = "${pkgsIntel.glibc}/lib/ld-linux-x86-64.so.2";
in
stdenv.mkDerivation rec {
pname = "playdate_sdk";
version = "1.12.3";
src = fetchurl {
url = "https://download.panic.com/playdate_sdk/Linux/PlaydateSDK-${version}.tar.gz";
sha256 = "6QZb7Ie6LaSAa5fK8qjDGSWt4AzgCimFo2IGp685XWo=";
};
buildInputs = pdcInputs;
dontFixup = true;
installPhase = ''
runHook preInstall
# Get our new root
root=$out/opt/playdate-sdk-${version}
# Everything else
mkdir -p $out/opt/playdate-sdk-${version}
cp -r ./ $out/opt/playdate-sdk-${version}
ln -s $root $out/opt/playdate-sdk
# Setup dependencies and interpreter
patchelf \
--set-interpreter "${dynamicLinker}" \
--set-rpath "${lib.makeLibraryPath pdcInputs}" \
$root/bin/pdc
patchelf \
--set-interpreter "${dynamicLinker}" \
$root/bin/pdutil
patchelf \
--set-interpreter "${dynamicLinker}" \
$root/bin/PlaydateSimulator
# Binaries
mkdir -p $out/bin
cp $root/bin/pdc $out/bin/pdc
cp $root/bin/pdutil $out/bin/pdutil
cp $root/bin/PlaydateSimulator $out/bin/PlaydateSimulator
runHook postInstall
'';
}
@RegularTetragon
Copy link

RegularTetragon commented Sep 13, 2023

Do you license this gist?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment