It's Magix
Nov 22, 2024 · 2 minute read · CodingLong time no see! In this post I would like to present Magix, a tool to build, cache, and run possibly compiled scripts with dependencies using the Nix package manager.
First, I would like to draw your attention to a blog post by Brian Hicks, in
which he introduces nix-script. In brief, Magix is a slimmed down version of
nix-script:
- Magix is simple and stupid.
- Magix is a tiny wrapper around
nix-build. - Magix uses Nix derivation templates, and so, is easier to understand, modify, and enhance.
- Magix is heavily tested (only unit tests at the moment, but please drop a pull request).
Have a look at the following example:
#!/usr/bin/env magix
#!magix haskell
#!haskellPackages text
import Data.Text (pack)
import Data.Text.IO (putStrLn)
import Prelude hiding (putStrLn)
main :: IO ()
main = putStrLn $ pack "Hello, World!"
Magix is going to
- parse the directives,
- assemble a tiny Nix wrapper expression,
- use the Nix package manager to build this expression, and
- run the derivation result.
The first time doing so, takes around 0.9 seconds on my machine. That sucks. However, in step 1, Magix also hashes the input script, and saves a link to the derivation result. That is, if Magix knows a script, the next execution is much faster: 0.09 seconds. That is one order of magnitude faster!