Skip to content

Build, cache, and run possibly compiled scripts with dependencies using the Nix package manager

License

Notifications You must be signed in to change notification settings

dschrempf/magix

Repository files navigation

Magix

Build, cache, and run possibly compiled scripts with dependencies using the Nix package manager.

  • Magix is simple and stupid.
  • Magix is a tiny wrapper around nix-build.
  • Magix uses a Nix expression 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).

Supported languages

Bash

#!/usr/bin/env magix
#!magix bash
#!runtimeInputs jq

jq --help

Haskell

#!/usr/bin/env magix
#!magix haskell
#!haskellPackages bytestring
#!ghcFlags -threaded

{-# LANGUAGE OverloadedStrings #-}

import Data.ByteString qualified as BS

main :: IO ()
main = BS.putStr "Hello, World!\n"

Python

#!/usr/bin/env magix
#!magix python
#!pythonPackages numpy

from numpy import array

xs = array([1,2,3])
print(xs)

Try Magix

Try Magix without installation on a Bash script called =empty=

wget https://github.com/dschrempf/magix/raw/refs/heads/main/test-scripts/bash/empty
nix run github:dschrempf/magix#magix empty
empty

Use

Install Magix, and, for example, run the =args= test script for Bash

./test-scripts/bash/args one two three -h
Command basename is: args
Command line arguments are: one two three -h

Get help

magix -h
Usage: magix [-v|--verbose] [-f|--force-build] [-c|--cache-path CACHE_PATH]
             [-n|--nixpkgs-path NIXPKGS_PATH] SCRIPT_FILE_PATH [SCRIPT_ARGS]

  Build, cache, and run possibly compiled scripts with dependencies using the
  Nix package manager

Available options:
  -h,--help                Show this help text
  -v,--verbose             Print debug messages
  -f,--force-build         Force build, even when cached build exists
  -c,--cache-path CACHE_PATH
                           Path of cache directory to use for builds (default:
                           '$XDG_CACHE_HOME/magix')
  -n,--nixpkgs-path NIXPKGS_PATH
                           Path of Nixpkgs repository to use (default: extracted
                           from '$NIX_PATH')
  SCRIPT_FILE_PATH         File path of script to build, cache and run
  SCRIPT_ARGS              Arguments passed on to the script

Next steps

  • Property-based testing (e.g., generate arbitrary directives or even scripts). When creating arbitrary directives, one could test if the resulting Nix expressions are syntactically correct.
  • We create random caches and hashes during tests. We could write an Arbitrary instance for Config to simplify this process.
  • CICD pipeline.

Notes about performance

I have performed basic benchmarks and have recorded some profiles. When a script is cached, Magix has a runtime cost of around 20ms.

The runtime cost depends on the used language because the Nix expressions used to wrap the script are different.

Similar tools that I know of

I want magix to be a simple and fast solution that just works. If you are looking for a wrapper with more extras, try the Nix package manager ;-).

About

Build, cache, and run possibly compiled scripts with dependencies using the Nix package manager

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published