Example repo to show off how I use Nix as build environment for Elixir / Phoenix projects.
Important Nix stuff is located withindefault.nix
&pkg/
folder.
This repo provides an easy environment setup by the usage of Nix.
Nix currently runs on Linux and macOS.
By using Nix, beside Nix nothing else needs to be installed manually.
Windows
Recommended also using Nix within a Linux Virtual Machine or WSL 2.
For maintaining simplicity, instructions for a manual setup isn't part of this readme.
Anyway, required dependencies:
For convenience an alias configuration exists for some of the following shell commands.
# Enter shell w/ development environment (no database)
# Alias: `app-env`
nix-shell --pure -A env
# Run commands without shell
nix-shell --pure -A env --run <command>
# Run commands in interactive shell
nix-shell --pure -A env --cmd <command>
# Enter shell w/ development environment and temporary PostgreSQL database
nix-shell --pure -A env_with_db
# Running all tests
# Alias: `app-test`
nix-shell --pure -A env_with_db --argstr MIX_ENV test --run 'mix test'
# Get Elixir mix deps
mix deps.get
# Get JS packages
npm install --prefix assets
# Start Phoenix server
mix phx.server
# Start Elixir application
mix run --no-halt
# Enter IEx
iex -S mix
# Run tests
mix test
# mix deps
nix-build -A mix_deps --option sandbox relaxed
# JS packages
nix-build -A node_modules --option sandbox relaxed
# Compile
nix-build -A mix_build --option sandbox relaxed
# Compile cacheable -> .nix/_build
nix-shell --pure -A env --command 'mix compile'
# Generate docs using `mix docs`
nix-build -A docs --option sandbox relaxed
# Build procution release using `mix release`
nix-build -A release --argstr MIX_ENV prod --option sandbox relaxed
# Update pinned Nix pkgs
elixir pkg/scripts/pkgs_update.exs
# Check outdated Mix deps & NPM packages
mix hex.outdated
npm outdated --prefix assets
# Update Mix deps & NPM packages
mix deps.update --all
npm update --prefix assets
# Removed unused deps from mix.lock
mix deps.clean --unlock --unused
# Load aliases into your shell
# You could also source this in your .profile
. pkg/scripts/alias.sh
Working with aliases
# List
alias
# Remove
unalias alias_name
# Remove all
unalias -a
direnv
let's you automatically load environment variables per directory defined in .envrc
.
Working with direnv
# Install via Nix
nix-env -iA nixpkgs.direnv
# Setup hook
# Add the following to your .profile
eval "$(direnv hook bash)"