# Load "nix develop" environment within a shell script

**URL:** https://discourse.nixos.org/t/load-nix-develop-environment-within-a-shell-script/46570
**Category:** Help
**Created:** [June 6, 2024, 12:47am UTC](https://discourse.nixos.org/t/load-nix-develop-environment-within-a-shell-script/46570 "2024-06-06T00:47:49Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![senorsmile](https://discourse.nixos.org/letter_avatar_proxy/v4/letter/s/278dde/32.png) [@senorsmile](https://discourse.nixos.org/u/senorsmile)
#### Post date: [June 6, 2024, 12:47am UTC](https://discourse.nixos.org/t/load-nix-develop-environment-within-a-shell-script/46570/1 "2024-06-06T00:47:49Z")

</div>

I’m writting a “runner script” of sorts to onboard a new team that is hesitant to try Nix. My goal is to have them only need to

1. install nix
2. run the runner script to run the application

This means additional installing direnv and configuring will not work.  
I have a flake.nix that sets up the whole environment.  
Running `nix develop` works, but that creates a subshell, which means I can’t just write in the run.sh:

```auto
nix develop
configure things
and more things
and even more
run special code

```

How can I load the develop environment from within a shell script to then run more shell?

---

<div class="post-metadata">

### Author: ![r33drichards](https://discourse.nixos.org/user_avatar/discourse.nixos.org/r33drichards/32/15273_2.png) [@r33drichards](https://discourse.nixos.org/u/r33drichards)
#### Post date: [June 6, 2024, 1:36am UTC](https://discourse.nixos.org/t/load-nix-develop-environment-within-a-shell-script/46570/2 "2024-06-06T01:36:03Z")

</div>

you could try

```auto

      · Run a series of script commands:

          | # nix develop --command bash -c "mkdir build && cmake .. && make"

```

from `nix develop --help`

but if you only need packages you could try something liek

```auto
#! /usr/bin/env nix-shell
#! nix-shell -i sh -p awscli2 jq coreutils ripgrep util-linux

configure things
and more things
and even more
run special code

```

in `script.sh` and then

```auto
chmod -x script.sh
./script.sh

```

---

<div class="post-metadata">

### Author: ![senorsmile](https://discourse.nixos.org/letter_avatar_proxy/v4/letter/s/278dde/32.png) [@senorsmile](https://discourse.nixos.org/u/senorsmile)
#### Post date: [June 6, 2024, 4:24am UTC](https://discourse.nixos.org/t/load-nix-develop-environment-within-a-shell-script/46570/3 "2024-06-06T04:24:20Z")

</div>

> nix develop --command bash -c "

I have used that before, but it unfortunately won’t work in this script for a couple of reasons, mainly that what follows is conditional and a few hundred lines of shell.

> #! /usr/bin/env nix-shell  
> #! nix-shell -i sh -p awscli2 jq coreutils ripgrep util-linux

This is interesting and could work, although I need something far more complex than I think you could put there:

- multiple inputs (which is another thing I don’t quite have working in this context yet)
- python libs that require a `python3.withPackages` context.

I see some references about pinning nixpkgs and multiline support with flakes here: [Nix-shell shebang - NixOS Wiki](https://nixos.wiki/wiki/Nix-shell_shebang). Time to dig in.

---

<div class="post-metadata">

### Author: ![r33drichards](https://discourse.nixos.org/user_avatar/discourse.nixos.org/r33drichards/32/15273_2.png) [@r33drichards](https://discourse.nixos.org/u/r33drichards)
#### Post date: [June 6, 2024, 5:53pm UTC](https://discourse.nixos.org/t/load-nix-develop-environment-within-a-shell-script/46570/4 "2024-06-06T17:53:04Z")

</div>

for

> - python libs that require a `python3.withPackages` context.

I use

```auto
#! /usr/bin/env nix-shell
#! nix-shell -i python3 -p "python3.withPackages(ps: with ps; [boto3 botocore])"

import argparse
import boto3

```

> - multiple inputs (which is another thing I don’t quite have working in this context yet)

wdym by multiple inputs?

---

<div class="post-metadata">

### Author: ![abathur](https://discourse.nixos.org/user_avatar/discourse.nixos.org/abathur/32/2719_2.png) [@abathur](https://discourse.nixos.org/u/abathur)
#### Post date: [June 6, 2024, 9:08pm UTC](https://discourse.nixos.org/t/load-nix-develop-environment-within-a-shell-script/46570/5 "2024-06-06T21:08:21Z")

</div>

Is this, say a demonstration script, or something they’d use every time they run this script (with this script being the primary means of entering the shell?)

If this is the latter, it sounds to me like whatever you’re doing in the shell script should just be part of the shellHook on the devShell.

---

<div class="post-metadata">

### Author: ![senorsmile](https://discourse.nixos.org/letter_avatar_proxy/v4/letter/s/278dde/32.png) [@senorsmile](https://discourse.nixos.org/u/senorsmile)
#### Post date: [June 6, 2024, 9:48pm UTC](https://discourse.nixos.org/t/load-nix-develop-environment-within-a-shell-script/46570/6 "2024-06-06T21:48:11Z")

</div>

This is something they’ll be running every single time they interact with the stack. Perhaps a little more details will help clarify the constraints (and the weirdness).

- This runner script is to run ansible, which itself is a python application.
- The script must choose which version of ansible to run based on which environment is calling it (currently via an environment variable).
- The script must also check out another repo for inventory at a specific git ref (inventory = a concept with Ansible to keep all nodes/things being managed along with their configuration).
- The script finally initializes the environment by defining environment variables that ansible will consume, parses and passes any command line arguments and finally calls ansible.

The previous method of doing most of the above was to use a mixture of pyenv, pipenv and way too much scripting. Assuming the person has direnv installed, the entire script works as is. But, I also want the script to be smart enough to tell the direnv is not present and to manually load the environment the same way that direnv would, and to continue along in the script.

Perhaps this is simply not possible without direnv?

---

<div class="post-metadata">

### Author: ![senorsmile](https://discourse.nixos.org/letter_avatar_proxy/v4/letter/s/278dde/32.png) [@senorsmile](https://discourse.nixos.org/u/senorsmile)
#### Post date: [June 6, 2024, 9:50pm UTC](https://discourse.nixos.org/t/load-nix-develop-environment-within-a-shell-script/46570/7 "2024-06-06T21:50:22Z")

</div>

> [@abathur](#):
>
> If this is the latter, it sounds to me like whatever you’re doing in the shell script should just be part of the shellHook on the devShell.

This is an interesting idea. In order to support multiple versions of ansible depending on which env is calling it, we have multiple flake.nix files. I’d want a way to not have to duplicate all that code somehow. There must be a way to load the contents of a file and pass to devShell?

---

<div class="post-metadata">

### Author: ![abathur](https://discourse.nixos.org/user_avatar/discourse.nixos.org/abathur/32/2719_2.png) [@abathur](https://discourse.nixos.org/u/abathur)
#### Post date: [June 6, 2024, 11:54pm UTC](https://discourse.nixos.org/t/load-nix-develop-environment-within-a-shell-script/46570/8 "2024-06-06T23:54:34Z")

</div>

I don’t (personally) feel clear enough on your exact requirements to say what I would or wouldn’t do. I’m reasonably confident there’s some way to do what you need, even it ends up being a kludge, but I’m not sure what it is.

Maybe you want to be using `nix print-dev-env`. Maybe you can get away with something like I did in [nix-shell, but make it lovely](https://t-ravis.com/post/nix/nix-make/). Maybe your shell script should be a runnable app in the flake. Maybe the inline expressions for a shell shebang given earlier are the right fit. There are probably a few more I am leaving out.

---

<div class="post-metadata">

### Author: ![senorsmile](https://discourse.nixos.org/letter_avatar_proxy/v4/letter/s/278dde/32.png) [@senorsmile](https://discourse.nixos.org/u/senorsmile)
#### Post date: [August 23, 2024, 2:43am UTC](https://discourse.nixos.org/t/load-nix-develop-environment-within-a-shell-script/46570/9 "2024-08-23T02:43:42Z")

</div>

I have found an example of what I want to do in the devshell project: [Continuous Integration setup - devshell](https://numtide.github.io/devshell/ci.html)

`nix-shell --run "$(nix-build shell.nix)/entrypoint make"`

This runs the make command from the environment using this shell.nix. I don’t grok what that file is doing at all. I’d like to repro this for a generic devShell environment.
