Common issues with using GnuPG over SSH from a Mac

Usually, I work directly on my Mac, but sometimes, I have to log into it from another system. By default (at least on MacPorts), the GnuPG agent will default to using the graphical pinentry. Normally, this is good, but if you need to enter your key passphrase, it’ll open on the desktop instead. Oops. To fix this part, add this line (adjusting path as needed) to ~/.gnupg/gpg-agent.conf (maybe need to restart the agent too):

pinentry-program /opt/local/bin/pinentry-curses

Remove this line to go back to using graphical passphrase entry.

Sometimes with this, you might get an error like this with the curses version of pinetry:

gpg: signing failed: Inappropriate ioctl for device

It seems GnuPG might not pick up the right TTY, so you may need to explicitly override it:

GPG_TTY=$(tty)
export GPG_TTY

Making mailing lists nicer: Writing a mail extension for macOS with MailKit

As a developer, I need to work with mailing lists a lot. Developer-focused mailing lists tend to have certain conventions most usage of email has moved on from, as have the clients. While some people who trawl mailing lists configure some terminal email client, I tend to not like those. Most of the time, I use the stock Apple mail application (Mail.app). It otherwise works well and has platform integration.

However, I do want to avoid a lot of common mailing list faux pas, like top posting and not wrapping lines. Mail.app can write plain text emails, but it doesn’t push you in the right direction for mailing lists. However, Mail.app does provide an extension API. I figured I could write an extension to make my life on mailing lists easier. Turns out it’s possible!

The extension I wrote is called MailTools (tentative until a better name is found). Feel free to build or download it yourself; this article explains the challenges I faced.

Continue reading

Updating Core Data object ID URIs on store migrations

I found out recently that Core Data object IDs can change. This usually happens if you migrate a store, such during a heavyweight migration (lightweight migrations don’t count, as they mutate in place). If you were like me and persisting object IDs, then you’ll have to update those URIs, or you will have a very bad day trying to fetch objects from these IDs.

The easiest way to do this is to lean on NSMigrationManager, because it has both the source and destination model versions, and can get the destination object version from the source version. If you’re manually doing the migration for model upgrades, you easily have it already. If you want to update it from a NSEntityMigrationPolicy, then the easiest option is to stash the NSMigrationManager by overriding beginEntityMapping:manager:error:. You provide it the source NSManagedObjects, and out comes the destination version’s managed objects, as shown below, of which you can get the new object IDs to persist in the new version of the model:

var manager: NSMigrationManager!

private func destinationURIsFromSourceObjects(_ objects: [NSManagedObject]) -> [URL] {
    return manager.destinationInstances(forEntityMappingName: "TrackToTrack", sourceInstances: objects).map {
        $0.objectID.uriRepresentation()
    }
}

Note that you need to provide the entity mapping name – regardless if your entities heavily changed or not. For the Xcode mapping model generator, check the name – it’s usually something like TrackToTrack. If you need to get the managed objects from the source version’s URIs, you can use the the sourceContext property in the migration manager to get the source model version’s managed object context.

Sleeping through a decade of Cocoa: Retrospective from modernizing an old Mac app

A few years ago since I started using Macs more often, one annoying thing I dealt with was using my local music library. My usual solution was to just drag files from the file manager to a music player, but this wasn’t as nice on macOS (due to i.e. SMB latency). However, I did have a Subsonic server, which provides a nice music streaming server, complete with an API for clients to use for things like phones. Why not use this on my laptop too?

Of course, if I bought a Mac, I’m not going to put up with bad cross-platform solutions that suck everywhere, when I can instead run bad native software that sucks uniquely for my platform of choice. However, there weren’t too many clients available on Mac. Mostly all of them were unmaintained and had been abandoned in the Snow Leopard era. One of them was Submariner, and it was open-source after the developer (Rafaël Warnault) had stopped working on it. Writing my own seemed a bit daunting with no background, but what if I used the Submariner codebase, and started from there?

Now I’ve been maintaining Submariner for almost two years at this point (it even has a minimal website), adding features and mostly just focusing on modernizing the codebase. It’s been an interesting experience as my first Objective-C/Mac project. A lot of the lessons of modernizing legacy code are universally applicable, but I’ve learned a lot about the specifics of Apple platforms and how they compare. This article aims to be both a retrospective on what I had to learn, what I had to do, and the lessons I took from it, including a comparison of what the development culture is like between platforms.

Continue reading

Restoring images without the futility of Disk Utility

I was trying to put a Mac OS X 10.6 image onto a USB stick to install on another system. To do this, I attempted to do it from a Mac running 13.6 and failed with a message like this when restoring the image to the partition:

Could not validate source - Invalid argument
The operation couldn't be completed. (OSStatus error 22.)

It turns out that Disk Utility is pretty broken at this. What you’re better off doing is using asr. This utility is what Disk Utility does under the covers anyways.

Continue reading

Why Macs don’t have games, or: Why Vulkan or the Game Porting Toolkit isn’t going to save you

Recently, one of Apple’s focuses at the latest WWDC keynote has been gaming. Two important pillars for this are an HLSL to Metal shader compiler, and the Game Porting Toolkit. Both are important, since HLSL is dominant for shaders in the industry, but the GPTK has become popular outside its intended target audience, with many videos with end-users instead of developers using it to try the latest AAA games. This has led to a lot of speculation online about what obstacles exist for game developers looking to port their games to Mac. What challenges actually matter?

(This is a bit of a rougher post, more addressing common points I see from Mac gamers on why they don’t have games. I’m not a game developer or expert in 3D graphics APIs, but I do keep up with the space. There is also some Kremlinology on Apple, but that’s inevitable considering the secrecy.)

Continue reading

Opening System Settings/Preferences to a specific app’s notification settings

If you need to open System Settings (formerly System Preferences) to a specific preference pane, there’s a URL scheme for that. However, some panes seem to take options for opening specific things. In my case, I wanted open the notifications pane to a specific app. The system Maps application can already do this, so I popped it open in a disassembler and learned the secret – ?id=(app bundle ID). For example, opening the following URL will open the notifications settings for Firefox:

x-apple.systempreferences:com.apple.preference.notifications?id=org.mozilla.firefox

Debugging an x86 application in Rosetta for Linux

Rosetta on Linux has been great for me, as someone who switched to an ARM Mac and sometimes develops in a VM. This is because my dayjob often involves debugging a proprietary database driver that only has x86 and PowerPC versions available for Linux, not 64-bit ARM. However, while Rosetta can make it easy to run x86 binaries, it’s not as obvious how to debug them. If you naively try running GDB on your x86 program, you get errors like:

warning: `/lib64/ld-linux-x86-64.so.2': Shared library architecture unknown is not compatible with target architecture aarch64.
warning: `/lib64/ld-linux-x86-64.so.2': Shared library architecture unknown is not compatible with target architecture aarch64.
Warning:
Cannot insert breakpoint -1.
Cannot access memory at address 0x66ec48

This is because you’re attaching to the Rosetta binary, which causes all sorts of confusion for GDB. Turns out, Rosetta does actually have a way to handle this.

Continue reading

Building a static GraphViz for macOS

Note: This is a very old post from 2020 I had sitting in my drafts. I figured I would publish it in case it was useful for someone else. Notably, this predates Apple Silicon, so yo would want to build another version that’s ARM and use lipo to make a universal binary. This is left as an exercise to the reader.

I recently had the need for building GraphViz without any external dependencies other than what comes with macOS, as part of an application calling into it. While on Windows, I used their provided binaries, I was on my own for macOS since I wasn’t willing to ship half of Homebrew with my application. Instead, I took a simpler way.

Continue reading

Fixing files marked as stuck as partially transferred in Finder

Very rough note, but if I don’t write it down, no one else will find it. A friend was dealing with an issue where she interrupted a file transfer of many files and directories to an SMB share on macOS and transferred it via other means. However, macOS thought the files were still in a partially transferred state to be resumed. There was no obvious way to tell Finder that it was OK.

It seems that clearing a file’s extended attributes can clear this out. A quick way to remove all extended attributes from everything in the current directory:

shopt -s globstar
xattr -c **

Be careful with this – you might have files that do need the extended attributes. In this case, it wasn’t needed. Read on for the theory on why this might work and the specific attribute that might be a problem.

Continue reading