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

**URL:** https://discourse.nixos.org/t/how-to-make-a-nix-file-which-returns-a-derivation-into-an-executable-file/17846
**Category:** Help
**Created:** [February 22, 2022, 1:13pm UTC](https://discourse.nixos.org/t/how-to-make-a-nix-file-which-returns-a-derivation-into-an-executable-file/17846 "2022-02-22T13:13:23Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![luochen1990](https://discourse.nixos.org/user_avatar/discourse.nixos.org/luochen1990/32/3092_2.png) [@luochen1990](https://discourse.nixos.org/u/luochen1990)
#### Post date: [February 22, 2022, 1:13pm UTC](https://discourse.nixos.org/t/how-to-make-a-nix-file-which-returns-a-derivation-into-an-executable-file/17846/1 "2022-02-22T13:13:24Z")

</div>

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)

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

{ pkgs ? import (fetchTarball "https://github.com/NixOS/nixpkgs/archive/c9a97ff47bb0db44f4a504015a5d1cedb3094c85.tar.gz") {} }:

(pkgs.buildFHSUserEnv {
  name = "bash";
  targetPkgs = pkgs: (with pkgs; [
    python37
    python37Packages.pip
    python37Packages.virtualenv
    stdenv.cc.cc.lib
    pythonManylinuxPackages.manylinux2014Package
  ]);

  profile = ''
    run-some-test --with-some-args
  '';
}).env

```

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?

---

<div class="post-metadata">

### Author: ![luochen1990](https://discourse.nixos.org/user_avatar/discourse.nixos.org/luochen1990/32/3092_2.png) [@luochen1990](https://discourse.nixos.org/u/luochen1990)
#### Post date: [February 23, 2022, 6:49am UTC](https://discourse.nixos.org/t/how-to-make-a-nix-file-which-returns-a-derivation-into-an-executable-file/17846/2 "2022-02-23T06:49:06Z")

</div>

> <https://gist.github.com/luochen1990/3f37aaa0a6a2578918c10b18e6ecdeca>

Usage:

```auto
$ ./venv.nix bash -c "echo hi"

```

---

<div class="post-metadata">

### Author: ![sternenseemann](https://discourse.nixos.org/user_avatar/discourse.nixos.org/sternenseemann/32/3809_2.png) [@sternenseemann](https://discourse.nixos.org/u/sternenseemann)
#### Post date: [February 25, 2022, 11:52am UTC](https://discourse.nixos.org/t/how-to-make-a-nix-file-which-returns-a-derivation-into-an-executable-file/17846/3 "2022-02-25T11:52:05Z")

</div>

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:

> [@. / c u r s e d . n i x](https://discourse.nixos.org/t/c-u-r-s-e-d-n-i-x/12417):
>
> Edit: Want to execute a .nix like “./whatever”? Use this and add parameters to the inner arguments. There must always be at least one. #! /usr/bin/env nix-shell #! nix-shell -i "nix-shell -v" -p "" =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= This was edited nonlinearly and may contain inconsistencies. To my massive irritation, calling nix-shell on a nix file that isn’t in $PWD fails: [nix-shell:/run/user/1000/tmp.6T3BRBy0aN/lol]$ ../uncursed.nix evaluating file '/nix/store/ppgcg2ns9sqq5dqzm…
