From 4bc159d6984d10f2f39cf2fc421a5ec15e3946f2 Mon Sep 17 00:00:00 2001 From: Nikos Papadakis Date: Sat, 30 Sep 2023 21:06:56 +0300 Subject: [PATCH] chore: switch to nix for development (closes #2) This commit add nix support for development shells. The developer can use `nix develop` to setup a complete and reproducable devleopment environment using Nix. --- .editorconfig | 8 ++++ .tool-versions | 2 - README.md | 63 +++++++++------------------- flake.lock | 96 +++++++++++++++++++++++++++++++++++++++++++ flake.nix | 43 +++++++++++++++++++ protoc-gen-elixir.nix | 28 +++++++++++++ 6 files changed, 195 insertions(+), 45 deletions(-) create mode 100644 .editorconfig delete mode 100644 .tool-versions create mode 100644 flake.lock create mode 100644 flake.nix create mode 100644 protoc-gen-elixir.nix diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..01be004 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,8 @@ +root = true +charset = utf-8 +end_of_line = lf + +[*.nix] +indent_style = space +indent_size = 2 +insert_final_newline = true diff --git a/.tool-versions b/.tool-versions deleted file mode 100644 index b9c63eb..0000000 --- a/.tool-versions +++ /dev/null @@ -1,2 +0,0 @@ -elixir 1.15.5 -erlang 26.0.2 diff --git a/README.md b/README.md index 25be6a0..a1db4df 100644 --- a/README.md +++ b/README.md @@ -2,53 +2,30 @@ [![status-badge](https://ci.nikos.gg/api/badges/nikos/prymn/status.svg)](https://ci.nikos.gg/nikos/prymn) -## Project dependencies +## Development -- Elixir 1.15 -- Erlang 24 or later -- Rust (recent version) -- protoc for protobufs version 3 +### Nix -## Setup +The best way to setup your development environment is with +[Nix](https://nixos.org/download). As a pre-requisite, you need to install Nix +and enable the experimental features **nix-command** and **flakes**. -Some basic steps to setup the project and its dependencies. +After installation, -- Install [asdf](https://github.com/asdf-vm/asdf) (curl is also required) - - Using git: `git clone https://github.com/asdf-vm/asdf.git ~/.asdf` - - or, using paru, `paru -S asdf-vm` - - Add asdf to path: `source /opt/asdf-vm/asdf.{bash,zsh,fish}`, or `~/.asdf/asdf.{bash,zsh,fish}` -- Install erlang - - `asdf plugin add erlang` - - `asdf install erlang 25` -- Install elixir - - `asdf plugin add elixir` - - `asdf install elixir 1.15` -- Install rust - - Optionally, set `CARGO_HOME` and `RUSTUP_HOME` to a XDG directory (e.g `~/.local/share/cargo` and `~/.local/share/rustup`) - - Arch: `pacman -S rustup` - - Other: `curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh` - - Make sure to add `$HOME/.cargo/bin` (or `$CARGO_HOME`) to `PATH` - - `rustup toolchain install stable` - - `rustup default stable` - - `rustup component add clippy` - - `rustup component add rust-analyzer` - - `rustup component add rustfmt` -- Install [protoc](https://grpc.io/docs/protoc-installation/) version 3 - - Arch: `pacman -S protobuf` - - Debian: `apt install -y protobuf-compiler` -- (Optional) Install [inotify-tools](https://github.com/inotify-tools/inotify-tools/wiki) - - Arch: `pacman -S inotify-tools` - - Debian: `apt install -y inotify-tools` -- Setup elixir - - Install hex: `mix local.hex` - - `cd app && mix escript.install hex protobuf` - - `asdf reshim` +- Clone the repo: `git clone git@git.nikos.gg:prymn/prymn.git && cd prymn` +- Run `nix develop` or `nix develop -c $SHELL` to use your shell +- Optionally, install [rust-analyzer](https://github.com/rust-lang/rust-analyzer/) + - With nix: `nix profile install nixpkgs#rust-analyzer` -## Preparing for development +### Manual -### Elixir app +Alternatively, you can install all the dependencies manually. Here's a (maybe +incomplete) list of packages to install: -- `cd app` -- Start the database: `docker-compose up -d` -- `mix setup` -- `mix phx.server` +- gcc +- inotify-tools +- Elixir (v1.16) +- Erlang (OTP 26) +- protobuf +- protoc-gen-elixir (via `mix escript.install hex protobuf`) +- Rust stable toolchain (v1.72) diff --git a/flake.lock b/flake.lock new file mode 100644 index 0000000..1c841f4 --- /dev/null +++ b/flake.lock @@ -0,0 +1,96 @@ +{ + "nodes": { + "flake-utils": { + "inputs": { + "systems": "systems" + }, + "locked": { + "lastModified": 1681202837, + "narHash": "sha256-H+Rh19JDwRtpVPAWp64F+rlEtxUWBAQW28eAi3SRSzg=", + "owner": "numtide", + "repo": "flake-utils", + "rev": "cfacdce06f30d2b68473a46042957675eebb3401", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "flake-utils", + "type": "github" + } + }, + "nixpkgs": { + "locked": { + "lastModified": 1695318763, + "narHash": "sha256-FHVPDRP2AfvsxAdc+AsgFJevMz5VBmnZglFUMlxBkcY=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "e12483116b3b51a185a33a272bf351e357ba9a99", + "type": "github" + }, + "original": { + "owner": "NixOS", + "ref": "nixpkgs-unstable", + "repo": "nixpkgs", + "type": "github" + } + }, + "nixpkgs_2": { + "locked": { + "lastModified": 1681358109, + "narHash": "sha256-eKyxW4OohHQx9Urxi7TQlFBTDWII+F+x2hklDOQPB50=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "96ba1c52e54e74c3197f4d43026b3f3d92e83ff9", + "type": "github" + }, + "original": { + "owner": "NixOS", + "ref": "nixpkgs-unstable", + "repo": "nixpkgs", + "type": "github" + } + }, + "root": { + "inputs": { + "nixpkgs": "nixpkgs", + "rust-overlay": "rust-overlay" + } + }, + "rust-overlay": { + "inputs": { + "flake-utils": "flake-utils", + "nixpkgs": "nixpkgs_2" + }, + "locked": { + "lastModified": 1696039808, + "narHash": "sha256-7TbAr9LskWG6ISPhUdyp6zHboT7FsFrME5QsWKybPTA=", + "owner": "oxalica", + "repo": "rust-overlay", + "rev": "a4c3c904ab29e04a20d3a6da6626d66030385773", + "type": "github" + }, + "original": { + "owner": "oxalica", + "repo": "rust-overlay", + "type": "github" + } + }, + "systems": { + "locked": { + "lastModified": 1681028828, + "narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=", + "owner": "nix-systems", + "repo": "default", + "rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e", + "type": "github" + }, + "original": { + "owner": "nix-systems", + "repo": "default", + "type": "github" + } + } + }, + "root": "root", + "version": 7 +} diff --git a/flake.nix b/flake.nix new file mode 100644 index 0000000..982e1e6 --- /dev/null +++ b/flake.nix @@ -0,0 +1,43 @@ +{ + description = "Prymn"; + + inputs = { + nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable"; + rust-overlay.url = "github:oxalica/rust-overlay"; + }; + + outputs = { self, nixpkgs, rust-overlay }: + let + #supportedSystems = [ "x86_64-linux" "aarch64-linux" "x86_64-darwin" "aarch64-darwin" ]; + system = "x86_64-linux"; + + overlays = [ + (final: prev: { + elixir = prev.beam.packages.erlang_26.elixir_1_15; + elixir-ls = prev.beam.packages.erlang_26.elixir-ls.override { elixir = final.elixir; }; + protoc-gen-elixir = prev.callPackage ./protoc-gen-elixir.nix { elixir = final.elixir; }; + }) + + (import rust-overlay) + ]; + + pkgs = import nixpkgs { + inherit system overlays; + }; + in + { + devShells.${system}.default = with pkgs; mkShell { + packages = [ + elixir + elixir-ls + protobuf + protoc-gen-elixir + rust-bin.stable.latest.default + ] ++ lib.optionals stdenv.isLinux [ inotify-tools ]; + + inputsFrom = [ + protoc-gen-elixir + ]; + }; + }; +} diff --git a/protoc-gen-elixir.nix b/protoc-gen-elixir.nix new file mode 100644 index 0000000..6ccf915 --- /dev/null +++ b/protoc-gen-elixir.nix @@ -0,0 +1,28 @@ +{ elixir, beamPackages, fetchFromGitHub }: + +beamPackages.mixRelease rec { + pname = "protoc-gen-elixir"; + + version = "v0.12.0"; + + inherit elixir; + + src = fetchFromGitHub { + owner = "elixir-protobuf"; + repo = "protobuf"; + rev = "ce9a031a5cae97336d4674670d313d54f1f80bf6"; + sha256 = "wLU3iM9jI/Zc96/HfPUjNvjteGryWos6IobIb/4zqpw="; + }; + + mixFodDeps = beamPackages.fetchMixDeps { + pname = "mix-deps-${pname}"; + inherit src version; + sha256 = "H7yiBHoxuiqWcNbWwPU5X0Nnv8f6nM8z/ZAfZAGPZjE="; + }; + + installPhase = '' + mix escript.build + mkdir -p $out/bin + mv ./protoc-gen-elixir $out/bin + ''; +}