Development environment with nix-shell: Difference between revisions

Artturin (talk | contribs)
Typo
Cdro (talk | contribs)
m Correct a typo
 
(5 intermediate revisions by 3 users not shown)
Line 22: Line 22:
       hello
       hello
     ];
     ];
     # compilers & linkers & dependecy finding programs
     # compilers & linkers & dependency finding programs
     nativeBuildInputs = [
     nativeBuildInputs = [
       rustc
       rustc
Line 42: Line 42:
{{Commands|$ nix-shell shell.nix}}
{{Commands|$ nix-shell shell.nix}}


Now you have ruby 3.2 available in your shell:
Now you have rustc available in your shell:
<syntaxhighlight lang="bash">
<syntaxhighlight lang="bash">
$ rustc --version
$ rustc --version
Line 82: Line 82:
we replace <code>nix-shell</code> with <code>nix develop</code>
we replace <code>nix-shell</code> with <code>nix develop</code>


Example: Building Nix in a development shell, to get [[Incremental builds]] = faster recompiles
Example: Building Nix in a development shell, to get [[Incremental builds]] = faster recompiles. This is because Nix evaluations are cached.


<pre>
<pre>
Line 175: Line 175:
</syntaxHighlight>
</syntaxHighlight>
and <code>nativeBuildInputs</code> would be for the native platform, while <code>buildInputs</code> would be for the foreign platform. That's a much more practical distinction: any tool that's miscategorized one won't be able to run, and any library that's miscategorized one won't be able to link!
and <code>nativeBuildInputs</code> would be for the native platform, while <code>buildInputs</code> would be for the foreign platform. That's a much more practical distinction: any tool that's miscategorized one won't be able to run, and any library that's miscategorized one won't be able to link!
== direnv ==
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:
<syntaxHighlight lang=bash>
nix-env -i direnv
</syntaxHighlight>
Now, you need to add one more file in the root of your repository named <code>.envrc</code> that contains only this:
<syntaxHighlight lang=bash>
use_nix
</syntaxHighlight>
Then depending on the shell you are using, you need to add a line in your configuration file. See the ''Setup'' section of the doc[https://direnv.net/]. For example, for Zsh put in your <code>~/.zshrc.local</code>:
<syntaxHighlight lang=bash>
eval "$(direnv hook zsh)"
</syntaxHighlight>
Then, still at the root of your repository, run:
<syntaxHighlight lang=bash>
$ direnv allow .
direnv: loading .envrc
direnv: using nix
[...]
+SIZE +SOURCE_DATE_EPOCH +STRINGS +STRIP +TEMP +TEMPDIR +TMP +TMPDIR +_PATH +buildInputs +builder +checkPhase +cmakeFlags +configureFlags +doCheck +enableParallelBuilding +name +nativeBuildInputs +out +postCheck +preCheck +preConfigure +propagatedBuildInputs +propagatedNativeBuildInputs +shell +src +stdenv +system +testInputs +version ~PATH
</syntaxHighlight>
Bonus: you can see all the variables set by the nix-shell :)
Now you can leave your project and the environment will be unloaded:
<syntaxHighlight lang=bash>
$ cd ..
direnv: unloading
</syntaxHighlight>
No need to use <code>direnv allow</code> anymore, the next time you go to your project the environment will be loaded!
More explanation and configuration tweaks can be found in the Direnv wiki [https://github.com/direnv/direnv/wiki/Nix].


== Troubleshooting ==
== Troubleshooting ==
Line 249: Line 209:
Similar to the Gsettings issue, icons can be added with XDG_DATA_DIRS:
Similar to the Gsettings issue, icons can be added with XDG_DATA_DIRS:
<pre> XDG_DATA_DIRS=...:${hicolor-icon-theme}/share:${gnome3.adwaita-icon-theme}/share</pre>
<pre> XDG_DATA_DIRS=...:${hicolor-icon-theme}/share:${gnome3.adwaita-icon-theme}/share</pre>
== See Also ==
* [[Direnv]]
* [[Command Shell#Using a different shell in nix-shell and nix develop]]
[[Category:Development]]
[[Category:Development]]
[[Category:nix]]
[[Category:nix]]