# Why use \`exec\` when creating a custom script in the Nix store (and how does \`shellHook\` work exactly)?

**URL:** https://discourse.nixos.org/t/why-use-exec-when-creating-a-custom-script-in-the-nix-store-and-how-does-shellhook-work-exactly/30213
**Category:** Help
**Created:** [July 9, 2023, 11:04pm UTC](https://discourse.nixos.org/t/why-use-exec-when-creating-a-custom-script-in-the-nix-store-and-how-does-shellhook-work-exactly/30213 "2023-07-09T23:04:18Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![toraritte](https://discourse.nixos.org/user_avatar/discourse.nixos.org/toraritte/32/6777_2.png) [@toraritte](https://discourse.nixos.org/u/toraritte)
#### Post date: [July 9, 2023, 11:04pm UTC](https://discourse.nixos.org/t/why-use-exec-when-creating-a-custom-script-in-the-nix-store-and-how-does-shellhook-work-exactly/30213/1 "2023-07-09T23:04:19Z")

</div>

Continuing the discussion from [How to add local files into nginx derivation for nix-shell?](https://discourse.nixos.org/t/how-to-add-local-files-into-nginx-derivation-for-nix-shell/6603/3):

Here’s the slightly modified `shell.nix` from the post linked above (with a couple more questions in tow):

```nix
{ pkgs ? import <nixpkgs> {} }:
with pkgs;

let
  nginx-with-config = pkgs.writeScriptBin "my_nginx" ''
  # VVVV
    exec ${pkgs.nginx}/bin/nginx -c ${./nginx.conf} "$@"
  # ^^^^
  '';

in mkShell {
  name = "my-shell";
  buildInputs = [
    nginx-with-config 
  ];

  shellHook = ''
    my_nginx
    trap "my_nginx -s quit" EXIT
  '';
}

```

### 1. Why use `exec`?

The Nix expression works even after deleting `exec`. Are there any benefits?

### 2. Are commands in a `shellHook` executed in the same shell created by the Nix expression?

When [`exec`](https://ss64.com/bash/exec.html) is invoked with a command, “_it replaces the shell without creating a new process_”. For example, running

```bash
exec watch -n 1 "ls -al"

```

would replace the current shell, and CTRL+C would simply close the terminal window (e.g., in `tmux`).

Now, running the Nix expression above with `nix-shell` will drop into an interactive shell where `nginx` is already running, instead of `my_nginx` “consuming” the current shell. `trap` wouldn’t even have a chance to run, and yet it does because `nginx` will get shut down upon exit from the Nix shell. `nginx` will fork itself in the background (is that the right way to say it?) so this may changes things, and in this case my ignorance is more about Linux than Nix itself.

I thought that this is a sign that each command is being run in a new shell, but this may not be the case because the `trap` command works too, and if that would run in its own shell, then it would trap it’s own exit, and `nginx` would be stopped right after starting up. the `nginx` executable will fork itself in the background (not sure what the right way to say this).

Anyway, currently I see that `shellHook` working only if these commands are simply dropped into the newly generated Nix shell, because the alternative (where each commands will run in its own shell) doesn’t make sense,

```bash
bash -c 'my_nginx"'; bash -c 'trap "my_nginx -s quit" EXIT'

```

because the `trap` would simply detect the exit of its own shell (and shut down the newly started `nginx` instance right away).

---

<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: [July 10, 2023, 12:59am UTC](https://discourse.nixos.org/t/why-use-exec-when-creating-a-custom-script-in-the-nix-store-and-how-does-shellhook-work-exactly/30213/2 "2023-07-10T00:59:21Z")

</div>

> [@toraritte](#):
>
> Why use `exec`? Are there any benefits?  
> …  
> “it replaces the shell without creating a new process”

Avoiding a new process is the benefit.

> [@toraritte](#):
>
> Are commands in a `shellHook` executed in the same shell created by the Nix expression?

Yep. Configuring that shell is the point of the hook. [nix/src/nix-build/nix-build.cc at 8d871e18225d39a4c256b5416cc275137b8769b9 · NixOS/nix · GitHub](https://github.com/NixOS/nix/blob/8d871e18225d39a4c256b5416cc275137b8769b9/src/nix-build/nix-build.cc#L511-L573)

---

<div class="post-metadata">

### Author: ![TLATER](https://discourse.nixos.org/user_avatar/discourse.nixos.org/tlater/32/4257_2.png) [@TLATER](https://discourse.nixos.org/u/TLATER)
#### Post date: [July 10, 2023, 1:27am UTC](https://discourse.nixos.org/t/why-use-exec-when-creating-a-custom-script-in-the-nix-store-and-how-does-shellhook-work-exactly/30213/3 "2023-07-10T01:27:02Z")

</div>

> [@toraritte](#):
>
> Now, running the Nix expression above with `nix-shell` will drop into an interactive shell where `nginx` is already running, instead of `my_nginx` “consuming” the current shell. `trap` wouldn’t even have a chance to run, and yet it does because `nginx` will get shut down upon exit from the Nix shell.

`my_nginx` is a separate script, and therefore runs in a subprocess. `trap` will behave as you expect it to (never execute) if you run nginx like so:

```auto
shellHook = ''
    exec ${pkgs.nginx}/bin/nginx -c ${./nginx.conf} "$@"
    trap "my_nginx -s quit" EXIT
  '';

```

To be even more clear, you can kind of read your current script as:

```auto
shellHook = ''
    bash /nix/store/<hash>-my_nginx # This spawns a new process, which gets immediately eaten by `exec nginx`. nginx forks *another* process and closes itself (but not the second nginx process, which "goes to the background"), allowing its parent to continue 
    trap "my_nginx -s quit" EXIT # This then runs because the original (=parent) process is still there, and in fact your interactive shell
  '';

```
