Development environment with nix-shell: Difference between revisions

imported>Kendofriendo
m fix grammar
imported>Fliiiix
m Update ruby version to something recent and make the text match the example
Line 3: Line 3:
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.
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 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:
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 3.2 and not one provided by your system you can write:
<syntaxHighlight lang=nix>
<syntaxHighlight lang=nix>
{ pkgs ? import <nixpkgs> {} }:
{ pkgs ? import <nixpkgs> {} }:
   pkgs.mkShell {
   pkgs.mkShell {
     # nativeBuildInputs is usually what you want -- tools you need to run
     # nativeBuildInputs is usually what you want -- tools you need to run
     nativeBuildInputs = [ pkgs.buildPackages.ruby_2_7 ];
     nativeBuildInputs = with pkgs.buildPackages; [ ruby_3_2 ];
}
}
</syntaxHighlight>
</syntaxHighlight>
Line 18: Line 18:
{{Commands|$ nix-shell shell.nix}}
{{Commands|$ nix-shell shell.nix}}


Now you have ruby 2.3 available in your shell:
Now you have ruby 3.2 available in your shell:
<syntaxHighlight lang=bash>
<syntaxHighlight lang=bash>
$ ruby --version
$ ruby --version
ruby 2.3.7p456 (2018-03-28) [x86_64-linux]
ruby 3.2.2 (2023-03-30 revision e51014f9c0) [x86_64-linux]
</syntaxHighlight>
</syntaxHighlight>