Skip to content

Instantly share code, notes, and snippets.

@datakurre
Last active August 29, 2015 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save datakurre/7a3c9eaf6ba51e9d8bd5 to your computer and use it in GitHub Desktop.
Save datakurre/7a3c9eaf6ba51e9d8bd5 to your computer and use it in GitHub Desktop.
Opinionated cli wrapper for nix expressions
###
# Simple 'nix-exec' shebang wrapper for using nix-expression as 'standalone'
# execs (outside NixOS, e.g. on OSX) without installing them with 'nix-env'.
#
# The original use case was to replace Python virtualenvs with nix expressions
# in Python development.
#
# Install:
#
# $ nix-env -i -f datakurre-nix-exec.nix
#
# Usage:
#
# $ echo "!/usr/bin/env nix-exec
# with import <nixpkgs> { };
#
# buildEnv {
# name = "python27";
# paths = [ python27 ];
# }" > python
# $ chmod u+x python
#
# $ ./python
# ➜ /nix/store/0gnln0ks3js26zc9p0575pvhnmpchywv-python27/bin/python
with import <nixpkgs> { };
stdenv.mkDerivation {
name = "datakurre-nix-exec-1.2.1";
builder = builtins.toFile "builder.sh" "
source $stdenv/setup
mkdir -p $out/bin
echo \"#!/bin/bash
build=\\`nix-build --no-out-link \\$1\\`
if [ \\$build ]; then
MY_TZ=\\\"\\$TZ\\\"
MY_PATH=\\\"\\$build/bin:\\$build/sbin:\\$build/libexec:\\$PATH\\\"
MY_http_proxy=\\\"\\$http_proxy\\\"
MY_ftp_proxy=\\\"\\$http_proxy\\\"
if [ -d \\$build/dev-envs ]; then
source \\\"\\$build/dev-envs/\\\"*
export TZ=\\\"\\$MY_TZ\\\"
export PATH=\\\"\\$PATH:\\$MY_PATH\\\"
export http_proxy=\\\"\\$MY_http_proxy\\\"
export ftp_proxy=\\\"\\$MY_ftp_proxy\\\"
export CFLAGS=\\`echo \\$NIX_CFLAGS_COMPILE|sed 's/-isystem /-I/g'\\`
export LDFLAGS=\\$NIX_LDFLAGS
else
export PATH=\\\"\\$MY_PATH\\\"
fi
cmd=\\$\{1##*/\}; cmd=\\$\{cmd%%@*\}; cmd=\\$\{cmd%.nix\}
paths=(\\\"\\$build/bin\\\" \\\"\\$build/sbin\\\" \\\"\\$build/libexec\\\")
for path in \\\"\\$\{paths[@]\}\\\"; do
if [ -f \\\"\\$path/\\$\{cmd\}\\\" ]; then
cmd=\\\"\\$path/\\$\{cmd\}\\\"
break
fi
done
if [ -t 1 ]; then echo \\\"➜\\\" \\$cmd \\\"\\$\{@:2\}\\\"; fi
\\\"\\$cmd\\\" \\\"\\$\{@:2\}\\\"
fi
\" > $out/bin/nix-exec
chmod a+x $out/bin/nix-exec
";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment