5

Please explain it as you would to a 10-year-old, from the point after the Nix package manager is installed on a non-NixOS machine. For example, I am on a Mac, and there isn't even a ~/.config directory.

I found the following (probably) relevant resources, and I can't put the pieces together:


In the end I installed the latest Elixir with Erlang 21 as shown below, but I'm sure that this is not how it is supposed to be done.

$ # Cloned the `nixpkgs` repo from github and started the repl
$ # in the same the directory.
$
$ nix repl
Welcome to Nix version 2.0.4. Type :? for help.

nix-repl> :l .   # if in the cloned `nixpkgs` repo
Added 9182 variables.

nix-repl> pkgs.beam.packages.erlangR21.elixir
«derivation /nix/store/vcadn6d6wgk1yjlci458vy8jqv66wwdy-elixir-1.6.5.drv»

nix-repl> :q

$ nix-store --realise /nix/store/vcadn6d6wgk1yjlci458vy8jqv66wwdy-elixir-1.6.5.drv
toraritte
  • 6,300
  • 3
  • 46
  • 67
  • 1
    Based on the `nix-shell` recommendations below and the [BEAM Languages section](https://nixos.org/nixpkgs/manual/#sec-beam) in the Nixpkgs manual ([How to develop](https://nixos.org/nixpkgs/manual/#how-to-develop)), this also works: `nix-shell -A beam.packages.erlangR21.elixir` – toraritte Jul 17 '18 at 17:11
  • For Erlang 23: `nix-shell -A pkgs.beam.packages.erlangR23.erlang` – toraritte Oct 04 '20 at 16:10

3 Answers3

6

You are on the right track; there is no need to start over.

To install the package from nix repl you can use the :i command.

nix-repl> :i pkgs.beam.packages.erlangR21.elixir

This will install the package into your ~/.nix-profile where it will be in $PATH, so you can call it. It is equivalent to running

nix-env -iA nixpkgs.beam.packages.erlangR21.elixir

nix-store -r (or equivalently, nix-store --realise) is considered a very low-level tool. It can only create a symlink to a package and that is rarely what you want. It doesn't even create a garbage collection root by default, so if you garbage collect, the symlink will become broken.

Although nix-env -iA is a valid way of installing software, you may consider ~/.nix-profile global state and avoid it for that reason. It seems to me like elixir that is more tied to a project, rather than to your user. For example you may want to use distinct versions for some projects and it may make sense to share your development tools with others who work on a project. That is you can use nix-shell for. Here's an example of shell.nix.

Robert Hensing
  • 6,708
  • 18
  • 23
  • Thanks! I am still having trouble to figure out the right attributepaths... You were also right about declarative package management (NixOS -> `configuration.nix`, non-NixOS -> [`~/.config/nixpkgs/config.nix`](https://nixos.org/nixpkgs/manual/#sec-declarative-package-management), right?), but I got frustrated and just wanted to get at least one thing done. Thanks also for Susan Potter's slides! It makes much more sense now with usoban's answer. – toraritte Jul 17 '18 at 16:22
  • 1
    Yes, that is how you can install on non-NixOS. You could still [use `nix-env -i`](https://stackoverflow.com/questions/50802880/reproducible-nix-env-i-with-only-nix-no-nixos/50805575#50805575) if you like. For projects I usually don't install anything and use `nix-shell` instead, because I don't want my user profile to break other projects. – Robert Hensing Jul 19 '18 at 07:15
  • You're a mind reader! I was thinking about `buildEnv` last night (as I was comparing [usoban's answer](https://stackoverflow.com/a/51384383/1498178) and [Susan Potter's slides](https://www.slideshare.net/mbbx6spp/from-zero-to-production-nixos-erlang-erlang-factory-sf-2016/46)), saw that [it is a Perl script](https://github.com/NixOS/nixpkgs/tree/master/pkgs/build-support/buildenv) without any documentation (at least I couldn't find a description of it in the [Nixpkgs manual](https://nixos.org/nixpkgs/manual/)) and your linked answer shows exactly what it is for. – toraritte Jul 19 '18 at 15:03
5

You can create a dedicated shell for use with your project, and avoid installing the runtime globally/for current user.

Example of minimal nix shell for Elixir on Erlang/OTP 20:

The contents of default.nix file:

with import <nixpkgs> {};

stdenv.mkDerivation rec {
    name = "env";
    env = buildEnv { name = name; paths = buildInputs; };
    buildInputs = [
        beam.packages.erlangR20.elixir
        inotify-tools
    ];
}

Then in terminal, navigate to the directory where default.nix is saved, and invoke nix-shell. You should drop into a shell which has iex and mix available.

usoban
  • 5,428
  • 28
  • 42
  • Accepted Robert Hensing's answer, because it answered the question, but your answer is the one I will actually need (and Robert figured this out as well). I will ask this topic (i.e., setting up a dev environment with Nix) in another question, and your answer would perfectly fit there because it is straightforward and worked right away. Thank you! – toraritte Jul 17 '18 at 16:14
  • 1
    Just found the question [How to use Nix to setup a development environment?](https://stackoverflow.com/questions/44732370/how-to-use-nix-to-setup-a-development-environment) where the accepted answer has a many resources listed, but your answer shows immediately how to get down to business in simple terms. – toraritte Jul 17 '18 at 16:43
1

Glad I posted this question back then because the answers are terrific, but since then I realized that there is no real point in installing interpreters (especially because sometimes multiple versions are needed), so just use nix-shell on demand.

For the current version in the active channel:

nix-shell -p erlang

For other versions not in the current channel, a specific channel can be given:

nix-shell -I nixpkgs=channel:nixos-unstable -p erlangR22

Or add path to the Nix expression in your NixOS/nixpkgs clone:

$ nix-shell -I nixpkgs=~/clones/nixpkgs -p erlangR23

How to find packages with attribute paths on the console

$ nix-env -qaP 'erlang*'
# ...
nixos.erlangR20            erlang-20.3.8.9
nixos.erlangR21            erlang-21.3.8.3
nixos.erlang               erlang-22.1.7
# ...
$ nix-env -f ~/clones/nixpkgs/ -qaP 'erlang*'
# ...
nixos.erlangR20            erlang-20.3.8.9
nixos.erlangR21            erlang-21.3.8.3
nixos.erlang               erlang-22.1.7
# ...
=== >>> erlangR23            erlang-23.0.2  <<<====

This answer goes more into the details.

toraritte
  • 6,300
  • 3
  • 46
  • 67
  • [Call `nix-shell` with path (e.g., to use the latest `NixOS/nixpkgs` clone)](https://toraritte.github.io/posts/2020-07-15-nix-shell-with-specific-path.html) – toraritte Jul 15 '20 at 19:09