diff options
author | William Carroll <wpcarro@gmail.com> | 2022-10-04T17·18-0700 |
---|---|---|
committer | wpcarro <wpcarro@gmail.com> | 2022-10-07T18·15+0000 |
commit | ba22b09f71b105d92149d30f2a026daaf95d3223 (patch) | |
tree | c62485e1acc3eb76546d352fbe01123c47cb0328 /users | |
parent | 4a06f5d3b177879fff807e72e3afd72a749b8a55 (diff) |
feat(wpcarro/blog): nix-shell (note to self) r/5052
Publishing another "note to self" Change-Id: If5d052f0360f3e1f371b0c1fdd3781e5bb846ea4 Reviewed-on: https://cl.tvl.fyi/c/depot/+/6861 Tested-by: BuildkiteCI Reviewed-by: wpcarro <wpcarro@gmail.com>
Diffstat (limited to 'users')
-rw-r--r-- | users/wpcarro/website/blog/posts.nix | 7 | ||||
-rw-r--r-- | users/wpcarro/website/blog/posts/nix-shell-note.md | 50 |
2 files changed, 57 insertions, 0 deletions
diff --git a/users/wpcarro/website/blog/posts.nix b/users/wpcarro/website/blog/posts.nix index 7766dabd6007..fcb2f5659e3b 100644 --- a/users/wpcarro/website/blog/posts.nix +++ b/users/wpcarro/website/blog/posts.nix @@ -57,4 +57,11 @@ content = ./posts/ssh-oddities.md; draft = false; } + { + key = "nix-shell"; + title = "nix-shell (note to self)"; + date = 1664902186; + content = ./posts/nix-shell-note.md; + draft = false; + } ] diff --git a/users/wpcarro/website/blog/posts/nix-shell-note.md b/users/wpcarro/website/blog/posts/nix-shell-note.md new file mode 100644 index 000000000000..6f709b4f1abe --- /dev/null +++ b/users/wpcarro/website/blog/posts/nix-shell-note.md @@ -0,0 +1,50 @@ +## Background + +I rarely use `nix-shell` for its originally intended purpose of "reproducing the +environment of a derivation for development". Instead, I often use it to put +some executable on my `PATH` for some ad hoc task. + +What's `nix-shell`'s "intended purpose"? Let's ask The Man (`man nix-shell`): + +> The command nix-shell will build the dependencies of the specified derivation, +> but not the derivation itself. It will then start an interactive shell in +> which all environment variables defined by the derivation path have been set +> to their corresponding values, and the script $stdenv/setup has been +> sourced. This is useful for reproducing the environment of a derivation for +> development. + +Because I'm abusing `nix-shell` in this way, I'm liable to forget that +`nix-shell` puts `buildInputs` on `PATH` and *not* the derivation itself. But I +often only want the derivation! + +## Solution + +Pass the Nix expression to `nix-shell -p`: + +```shell +λ nix-shell -p '(import /depot {}).tvix.eval' +``` + +## Explanation + +This works because Nix forwards the arguments passed to `-p` (i.e. `--packages`) +and interpolates them into this expression here: [source](nix-src) + +```nix +{ ... }@args: + +with import <nixpkgs> args; + +(pkgs.runCommandCC or pkgs.runCommand) "shell" { + buildInputs = [ + # --packages go here + ]; +} +``` + +So really you can pass-in *any* valid Nix expression that produces a derivation +and `nix-shell` will put their outputs on your `PATH`. + +Enjoy! + +[nix-src]: https://sourcegraph.com/github.com/NixOS/nix@3ae9467d57188f9db41f85b0e5c41c0c9d141955/-/blob/src/nix-build/nix-build.cc?L266 |