Development environment with nix-shell: Difference between revisions
imported>Wucke13 Added note about Building locally in a nix-shell |
imported>Amanjeev m Some grammatical fixes and sentence formations |
||
Line 1: | Line 1: | ||
Nix can be used to provides some kind of '''virtual environment''' through the <code>nix-shell</code> command. | Nix can be used to provides some kind of '''virtual environment''' through the <code>nix-shell</code> command. | ||
If you already have a nix package definition of | If you already have a nix package definition of your project it's easy: Just use <code>nix-shell</code> instead of <code>nix-build</code> and you will end up in a bash shell that reproduce the build-environment of your package. You can also override[https://nixos.org/nixpkgs/manual/#sec-pkg-override] your package in a <code>shell.nix</code> file to add test and coverage dependencies, that are not necessary for the actual build of the package, but that you want for your development environment. | ||
But, if you don't (or you don't want to) have a package definition you can still use a nix-shell to provide a reproducible development environment. To do so, you have to create a <code>shell.nix</code> file at root of your repository. For example, if you want to have Ruby 2.3 and not | But, if you don't (or you don't want to) have a package definition you can still use a nix-shell to provide a reproducible development environment. To do so, you have to create a <code>shell.nix</code> file at the root of your repository. For example, if you want to have Ruby 2.3 and not one provided by your system you can write: | ||
<syntaxHighlight lang=nix> | <syntaxHighlight lang=nix> | ||
{ pkgs ? import <nixpkgs> {} }: | { pkgs ? import <nixpkgs> {} }: | ||
Line 27: | Line 27: | ||
== Using Direnv == | == Using Direnv == | ||
One of the | One of the limitations of nix-shell is that you can't use a shell other than bash. Thankfully, there is Direnv [[https://direnv.net/]] with the support of Nix[[https://github.com/direnv/direnv/wiki/Nix]] to overcome this limitation. Also, Direnv provides some nice features like loading the environment automatically when you enter your project directory and show the loaded variables to you (explicit is always better;-)). | ||
First, install Direnv: | First, install Direnv: | ||
Line 71: | Line 71: | ||
= note: impure path `/[...]' used in link | = note: impure path `/[...]' used in link | ||
</syntaxHighlight> | </syntaxHighlight> | ||
This happens due to a specialty in nix: <code>ld</code> is wrapped in a shell script which refuses to link against files not residing in the nix store, to ensure purity of builds. Obviously this is not useful when building locally, for example in your home directory. To disable this behavior simply set | This happens due to a specialty in nix: <code>ld</code> is wrapped in a shell script which refuses to link against files not residing in the nix store, to ensure the purity of builds. Obviously this is not useful when building locally, for example in your home directory. To disable this behavior simply set | ||
<syntaxHighlight lang=bash> | <syntaxHighlight lang=bash> | ||
NIX_ENFORCE_PURITY=0 | NIX_ENFORCE_PURITY=0 | ||
</syntaxHighlight> | </syntaxHighlight> | ||
in the nix-shell. | in the nix-shell. |