Remapping Keys on macOS with Nix-Darwin: My Battle with the EU Keyboard Layout
If you’ve ever used an EU keyboard on macOS, you know the struggle. The tilde (~
) is bizarrely placed next to the Z key, and getting to a backtick (`) feels like solving a puzzle involving Shift
and frustration.
This post is about how I swapped the tilde and backtick keys using Nix-Darwin, hidutil
, and some trial and error. If you’re also tired of your keyboard layout mocking you, read on.
Why Bother Remapping?
For anyone who spends time in the terminal, the tilde and backtick keys are essential. Yet, the EU keyboard layout seems designed to test your patience.
So here is my plan
- Swap the tilde (
~
) and backtick (`) keys. - Make the change permanent, so I never have to think about it again.
- Avoid third-party apps that might break with the next macOS update.
Finding the Key Codes
To remap keys, you need their codes. After digging through Apple documentation (and a lot of trial and error), I found the codes for the tilde and backtick:
• Tilde (~): 0x700000035
• Backtick (`): 0x700000064
Using hidutil to Remap Keys
Apple’s hidutil
tool lets you remap keys at a low level. Here’s the command I used to swap the tilde and backtick:
hidutil property --set '{
"UserKeyMapping":[
{"HIDKeyboardModifierMappingSrc":0x700000035,"HIDKeyboardModifierMappingDst":0x700000064},
{"HIDKeyboardModifierMappingSrc":0x700000064,"HIDKeyboardModifierMappingDst":0x700000035}
]
}'
Running this command instantly swapped the two keys. Problem solved, right? Not quite.
Making It Permanent with Nix-Darwin
Typing the hidutil
command every time I reboot isn’t practical. Since I use Nix-Darwin to manage my system configuration, I decided to make the remapping a permanent part of my setup.
Activation Script
{
activation = {
remapKeys = ''
/usr/bin/hidutil property --set '{
"UserKeyMapping":[
{"HIDKeyboardModifierMappingSrc":0x700000035,"HIDKeyboardModifierMappingDst":0x700000064},
{"HIDKeyboardModifierMappingSrc":0x700000064,"HIDKeyboardModifierMappingDst":0x700000035}
]
}'
'';
};
}
launchd Agent
For remapping that sticks across reboots, a launchd agent is the way to go. Here’s the configuration:
{
launchd.user.agents.remap-keys = {
serviceConfig = {
ProgramArguments = [
"/usr/bin/hidutil"
"property"
"--set"
''{
"UserKeyMapping":[
{"HIDKeyboardModifierMappingSrc":0x700000035,"HIDKeyboardModifierMappingDst":0x700000064},
{"HIDKeyboardModifierMappingSrc":0x700000064,"HIDKeyboardModifierMappingDst":0x700000035}
]
}''
];
RunAtLoad = true;
};
};
}
This ensures the remapping is applied every time I log in.
darwin-rebuild switch
And that was it. The tilde and backtick keys were swapped, and the change persisted across reboots. Mission accomplished.
And that was it. The tilde and backtick keys were swapped, and the change persisted across reboots. Mission accomplished.
hidutil property --set '{"UserKeyMapping":[]}'
Alternatively, just remove the script or launchd agent from your Nix-Darwin configuration and run darwin-rebuild switch
Why this setup?
- Clean and Declarative: The configuration lives in Nix, making it easy to manage and version control.
- Reproducible: Got a new Mac? Just copy your Nix configuration, and you’re good to go.
- Persistent: With a launchd agent, the remapping survives reboots.
Closing thoughts
Key remapping might seem like a small thing, but it’s one of those tweaks that makes using your computer just a little bit better. If you’re managing your macOS system with Nix-Darwin and you’ve got a keyboard layout that drives you nuts, give this a try.
And hey, if you’ve got other Nix-Darwin tricks up your sleeve, let me know. I’m always on the lookout for new ways to make my setup smarter (or at least less frustrating).