How to make a nix file which returns a derivation into an executable file?

Hi, guys, I’d like to make my nix script file into executable file via some shebang.

My first solution is use the nix-shell shebang like following:

(the file is named test.nix and chmod +x test.nix is executed)

#!/usr/bin/env nix-shell
#! nix-shell test.nix

{ pkgs ? import (fetchTarball "") {} }:

(pkgs.buildFHSUserEnv {
  name = "bash";
  targetPkgs = pkgs: (with pkgs; [

  profile = ''
    run-some-test --with-some-args

But I cannot pass extra command line argument to bash this way.

So I think I can pass CLI arguments like:

$(nix-build --pure xxx.nix)/bin/bash --some-args

But I want to make it simple for users who need to run this test file and might not familier with nix. So the best way is to write the above line into the shebang of the nix file, is it possible?


$ ./venv.nix bash -c "echo hi"
1 Like

You can also achieve this without a wrapper, but I wouldn’t recommend it, as it’s not officially supported and probably won’t remain stable.

There was some exploration in this direction (including some positively disgusting shebangs) in this thread:

Hosted by Flying Circus.