Run scripts as executables with automatic dependency management via Nix.
Currently supports Haskell. More languages are planned.
Nix flakes cache evaluation results but do not cache nix shell --expr. We exploit this by using a flake to instantly bring in cached-nix-shell, which can cache arbitrary Nix expressions. The script is then compiled and cached on first run via cached-nix-shell.
Add a shebang to your Haskell file:
#!/usr/bin/env nix
#!nix shell --no-write-lock-file git+https://github.com/ymeister/nix-shebang?submodules=1#haskell.nix-haskell --command sh -c ``nix-haskell-shebang --opts -O2 --deps shh -- "$@"`` sh
{-# LANGUAGE ExtendedDefaultRules #-}
{-# LANGUAGE TemplateHaskell #-}
import System.Environment
import Shh
$(loadEnv SearchPath)
main :: IO ()
main = do
args <- getArgs
if null args then do
echo "Hello World!"
else do
echo $ "Hello" : argsThen make it executable and run it:
chmod +x script.hs
./script.hs| Option | Description |
|---|---|
--deps <packages...> |
Haskell package dependencies |
--opts <flags...> |
GHC compiler flags (e.g. -O2, -threaded) |
--with <packages...> |
Non-Haskell system dependencies from nixpkgs |
--module <expr> |
Nix-haskell module override as a raw Nix expression (repeatable) |
-- |
Separator between shebang args and script args |
Bare positional arguments are treated as --deps.
Two variants are available via the flake:
haskell.nix-haskell-- Uses haskell.nix for dependency resolution. Supports--modulefor overrides like custom compilers and cabal project settings.haskell.nixpkgs-- UseshaskellPackages.ghcWithPackagesfrom nixpkgs.
Each package provides two commands:
nix-haskell-shebang-- Compiles and runs the script.nix-haskell-repl-- Opens GHCi with the script and its dependencies loaded.