Testing for POSIX compliance with Chimera Linux
2023-03-10
You might be aware that calling sed with -i
option is a GNUism. Unix systems are described by the POSIX standard, which—uncharitably—is the lowest common denominator of what you can expect when you interact with a Unix box. Various Unix systems provide more than what POSIX specifies, through non-standard extensions. GNU userland, which is used by most… linuces? linuxes? whatever—it has a shit ton of these extensions, sed -i
being one of them.
Most of the time this is not a problem, but if you want to run a script full of these extensions on a different Unix, you’re gonna have a bad time. And how often is that? The other open source system family you might think about are BSDs. These are kinda niche, but there’s no need to make them even more niche by making lives of their users harder. But the system you probably thought about is macOS.
Yup, macOS is a Unix, and a certified one at that, and a lot of people in tech use it. sed -i
won’t run there, at least not by default*. No POSIX command even accepts --long-style-options
. It’d be difficult to enumerate all the GNUisms in a script and keep them in check by hand, and as far as I know there’s no tool to comprehensively† check for that. Until now.
Enter the Chimera Linux distribution. Long story short, Linux is the kernel, but the rest of the stack is LLVM, musl, and—most importantly for this post—FreeBSD userland. The project maintainers are even so kind to distribute it as a tarball of the file system. We are now one command away from creating an environment for testing our scripts for portability:
docker image import chimera-linux-x86_64-ROOTFS-20221115-core.tar.gz chimera
This will create a docker image from the tarball and tag it chimera
. You can now create a chimera.Dockerfile
in your project, starting with the following line:
FROM chimera
And you’re good to go. This is of course not a static analysis thing. You need to run your script inside the container. Errors might not come with nice messages and workaround suggestions, but it’s always something.
* You can install GNU coreutils on a Mac.
† ShellCheck is capable of catching some of these errors. Testing specifically for bashisms can be done with dash and posh shell—the latter is specifically made for this. See How to check your shell scripts for portability.