Home Explore Blog Models CI



nixpkgs

6th chunk of `doc/languages-frameworks/beam.section.md`
e54458ad2899e2f2e755f7967681336357760e696640b3870000000100000d24
    path = [ pkgs.bash ];
  };

  # in case you have migration scripts or you want to use a remote shell
  environment.systemPackages = [ release ];
}
```

## How to Develop {#how-to-develop}

### Creating a Shell {#creating-a-shell}

Usually, we need to create a `shell.nix` file and do our development inside the environment specified therein. Just install your version of Erlang and any other interpreters, and then use your normal build tools. As an example, with Elixir:

```nix
{
  pkgs ? import <nixpkgs> { },
}:

with pkgs;
let
  elixir = beam.packages.erlang_27.elixir_1_18;
in
mkShell { buildInputs = [ elixir ]; }
```

### Using an overlay {#beam-using-overlays}

If you need to use an overlay to change some attributes of a derivation, e.g. if you need a bugfix from a version that is not yet available in Nixpkgs, you can override attributes such as `version` (and the corresponding `hash`) and then use this overlay in your development environment:

#### `shell.nix` {#beam-using-overlays-shell.nix}

```nix
let
  elixir_1_18_1_overlay = (
    self: super: {
      elixir_1_18 = super.elixir_1_18.override {
        version = "1.18.1";
        sha256 = "sha256-AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=";
      };
    }
  );
  pkgs = import <nixpkgs> { overlays = [ elixir_1_18_1_overlay ]; };
in
with pkgs;
mkShell { buildInputs = [ elixir_1_18 ]; }
```

#### Elixir - Phoenix project {#elixir---phoenix-project}

Here is an example `shell.nix`.

```nix
with import <nixpkgs> { };

let
  # define packages to install
  basePackages = [
    git
    # replace with beam.packages.erlang.elixir_1_18 if you need
    beam.packages.erlang.elixir
    nodejs
    postgresql_14
    # only used for frontend dependencies
    # you are free to use yarn2nix as well
    nodePackages.node2nix
    # formatting js file
    nodePackages.prettier
  ];

  inputs = basePackages ++ lib.optionals stdenv.hostPlatform.isLinux [ inotify-tools ];

  # define shell startup command
  hooks = ''
    # this allows mix to work on the local directory
    mkdir -p .nix-mix .nix-hex
    export MIX_HOME=$PWD/.nix-mix
    export HEX_HOME=$PWD/.nix-mix
    # make hex from Nixpkgs available
    # `mix local.hex` will install hex into MIX_HOME and should take precedence
    export MIX_PATH="${beam.packages.erlang.hex}/lib/erlang/lib/hex/ebin"
    export PATH=$MIX_HOME/bin:$HEX_HOME/bin:$PATH
    export LANG=C.UTF-8
    # keep your shell history in iex
    export ERL_AFLAGS="-kernel shell_history enabled"

    # postges related
    # keep all your db data in a folder inside the project
    export PGDATA="$PWD/db"

    # phoenix related env vars
    export POOL_SIZE=15
    export DB_URL="postgresql://postgres:postgres@localhost:5432/db"
    export PORT=4000
    export MIX_ENV=dev
    # add your project env vars here, word readable in the nix store.
    export ENV_VAR="your_env_var"
  '';

in
mkShell {
  buildInputs = inputs;
  shellHook = hooks;
}
```

Initializing the project will require the following steps:

- create the db directory `initdb ./db` (inside your mix project folder)
- create the postgres user `createuser postgres -ds`
- create the db `createdb db`
- start the postgres instance `pg_ctl -l "$PGDATA/server.log" start`
- add the `/db` folder to your `.gitignore`
- you can start your Phoenix server and get a shell with `iex -S mix phx.server`

Title: Nix Development Shells: General Setup, Overlays, and Elixir Phoenix Project Configuration
Summary
The chunk concludes a service configuration, specifying `pkgs.bash` for the path and adding the release to `environment.systemPackages`. It then details "How to Develop" using `shell.nix` files, demonstrating setup for general Elixir environments. The document further explains Nix overlays for overriding package attributes, providing a `shell.nix` example for custom Elixir versions. The most extensive part is a comprehensive `shell.nix` for an Elixir Phoenix project. This configuration defines `basePackages` (e.g., Git, Elixir, Node.js, PostgreSQL), conditional `inputs`, and a detailed `shellHook` for configuring local `MIX_HOME`, `HEX_HOME`, `PGDATA`, and various Phoenix environment variables. Finally, it outlines steps for initializing the Phoenix project, including database setup and server startup.